Hi I used following code to send mail from Timer job, (Thanks to Mr Deepu for providing a sample template). After deploying it, I could able to find this job, in the monitoring section of the Central Admin. I did able to change the start and end time. And run the Job manually, however though the job shows that it run successfully, I did not get any mail.
Also is there any way I can just debug each step?
public override void Execute(Guid contentDbId)
{
string SiteListURL = "http://sok:123/Lists/Live/";
using (SPSite oSPSite = new SPSite(SiteListURL))
using (SPWeb oSPWeb = oSPSite.OpenWeb())
{
string emailTo;
MailInfo oMailInfo = new MailInfo(); // Where Mail Info is a class to send the Email
string EmailHtmlBody;
MailNotifier oMailNotifier = new MailNotifier("IP Address");//Give IP address
emailTo = "[email protected]";
if (!string.IsNullOrEmpty(emailTo) && emailTo != null)
{
EmailHtmlBody = "The Opportunity ";
oMailInfo.EMailTo = emailTo;
oMailInfo.MailFrom = "[email protected]";
oMailInfo.Subject = "Closure Expiry Alert";
oMailInfo.MailBody = EmailHtmlBody;
oMailNotifier.SendMail(oMailInfo); // Please find the SendMail function in the below class MailNotifier !!
}
}
}
The below Class is for Email configuration and sending it.
public struct MailInfo
{
public string MailBody;
public string EMailToCC;
public string EMailTo;
public string MailFrom;
public string Subject;
}
public class MailNotifier
{
private string _SMTPServerName;
public MailNotifier(string SMTPServerName)
{
if (string.IsNullOrEmpty(SMTPServerName))
throw new NullReferenceException("SMTPServerName NULL");
_SMTPServerName = SMTPServerName;
}
public bool SendMail(MailInfo oMailInfo)
{
System.Net.Mail.SmtpClient osmtp;
System.Net.Mail.MailMessage Mail;
bool Result = false;
try
{
Mail = new System.Net.Mail.MailMessage();
Mail.To.Add(oMailInfo.EMailTo);
Mail.CC.Add(oMailInfo.EMailToCC);
Mail.From = new System.Net.Mail.MailAddress(oMailInfo.MailFrom);
Mail.Body = oMailInfo.MailBody;
osmtp = new System.Net.Mail.SmtpClient(_SMTPServerName);
Mail.IsBodyHtml = true;
Mail.Subject = oMailInfo.Subject;
osmtp.Send(Mail);
Mail = null;
osmtp = null;
Result = true;
}
catch
{
Result = false;
}
finally
{
Mail = null;
osmtp = null;
}
return Result;
}
}
UPDATE: I tried with KAI's example, Even that is not sending any mail, Outgoing mail is formatted correctly and it is sending out mails!! I could able to see the Timer job from Farm and also I could able to see the last run time, and I even tried manual run.... No mails are sending!!
Simplified Code..
public class EMailAlertJob : SPJobDefinition
{
public const string EMAIL_ALERT_JOB_NAME = "Closure Date";
SPWebApplication _webApplication;
SPContentDatabase _contentDb;
public EMailAlertJob()
: base()
{
this.Title = EMAIL_ALERT_JOB_NAME;
}
public EMailAlertJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
: base(jobName, service, server, targetType)
{
this.Title = EMAIL_ALERT_JOB_NAME;
}
public EMailAlertJob(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
{
this.Title = EMAIL_ALERT_JOB_NAME;
}
public override void Execute(Guid contentDbId)
{
SPListItemCollection CampaignsToBeNotified = null;
string SiteListURL = "http://sok:1111/Lists/ListName/";
using (SPSite oSPSite = new SPSite(SiteListURL))
{
using (SPWeb oSPWeb = oSPSite.OpenWeb())
{
var headers = new StringDictionary();
headers.Add("subject", "Hi!");
// "text/html" to send rich html text in message body
headers.Add("content-type", "text/html");
headers.Add("to", "[email protected]");
SPUtility.SendEmail(oSPWeb, headers, "<b>Hi there!</b>");
}
}
}