Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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 = "abc@company.com";

                if (!string.IsNullOrEmpty(emailTo) && emailTo != null)
                {
                    EmailHtmlBody = "The Opportunity ";


                    oMailInfo.EMailTo = emailTo;

                    oMailInfo.MailFrom = "ab1@comapny.com";
                    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", "abc@Company.com");
                SPUtility.SendEmail(oSPWeb, headers, "<b>Hi there!</b>");

            }
    }
        }
share|improve this question

2 Answers 2

up vote 8 down vote accepted

To debug timer job you need to attach to SharePoint Timer process (after each code deployment you also need restart this service to make sure that it picks up your recent code updates, you can restart it using command prompt - net stop SPTimerV4 and net start SPTimerV4)

About sending mail - make sure that your mail server configured and working (write a sample console app to test it), also you can try using standard sharepoint method for sending emails - SPUtility.SendEmail. Sample of using it:

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", "some@address.com");
SPUtility.SendEmail(web, headers, "<b>Hi there!</b>");

To use SPUtility.SendEmail you need "Configure outgoing e-mail settings" (under System Settings) in central admin (setup correct Outbound SMTP server address).

share|improve this answer
    
Thanks, Kai Let me check with this console code :) –  Jithu Dec 30 '11 at 7:39
    
HI Kai I did try creating ur code, however I did not able to use SPUtility from Console, I did not get anything to Resolve as well.... Again I used the above code to check the Timer Job. Nothing happened. I have set outgoing mail in the farm level, hope that we do not have separate mail setting for each Site collection. –  Jithu Dec 30 '11 at 8:03
    
Why did you not able to use SPUtility.SendEmail? May be exception is thrown? If your mail server and sharepoint settings configured correctly, code above should work. –  Kai Dec 30 '11 at 8:21
    
Try to create a webpart and implement the above code... –  Strider Dec 30 '11 at 8:26
    
Easy and quickly way to check if your sharepoint mail system working is to add yourself to a sharepoint group on some site (make sure "Send welcome e-mail to the new users" checkbox is checked). If all correct, you receive message, if not, an error will appear ("The user or users have been added successfully, but there was an error in sending the e-mail message....") –  Kai Dec 30 '11 at 8:39

Can you try debugging the solution? Add a Debugger.break() statement in your code where you need to debug. Deploy the timer job solution to the farm again and when you run the timer job, there will be a debugger window poping up to select your visual studio solution. Select your solution and within few seconds, it will start debugging.

Try debugging and find the statement where the problem exist.

Good luck.

share|improve this answer
    
Thanks, Deepu bayya, Let me try this!! –  Jithu Jan 3 '12 at 6:39
    
It did not hit, Execute method!! –  Jithu Jan 3 '12 at 7:20
    
Do you reset timer process after you deployed your dll into GAC? You must to do it each time when you deploy your dll. –  Kai Jan 3 '12 at 7:45
    
As Kai suggested, do a restart on timer service after deployment. Did it work then? –  Deepu Nair Jan 3 '12 at 9:10
    
Since I am doing it from vs 2010. I directly right click the project and deploy. I believe the deploying of dll will do automatically!! –  Jithu Jan 3 '12 at 10:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.