Not able to send mail in a custom timer job

Hi,

I have implemented a custom timer job to send a mail when a list data field is equal to the current date.

I have debugged the code in VS and it is executed correctly if I am using it in a button click event of a webpart.

But problem is its not sending the mails while I am appling the same code as a timer job.


Can somebody help me out?


BR,

Sudhir
November 2nd, 2009 3:56pm

Are you sure that your timer job is executing?
Maybe use some logging to make sure that the code that you are using is definately being reached (no exceptions).

Also, what method are you using to send the email?
You should be using the SharePoint SPUtility.SendEmail() static method.
Free Windows Admin Tool Kit Click here and download it now
November 2nd, 2009 3:59pm

Hi,

Yes the timer job is working fine. I am adding a new item to a list, and its working fine thru the timer. But when I am adding the

SPUtility

.SendEmail(SPContext.Current.Web, false, false, To.ToString(), Subject, mailBody);

then theupdate is also not working. I am not getting any log. Can you tell me how can I log the error?

Br,
Sudhir

November 2nd, 2009 4:13pm

What happens when you debug the Timer Service on your dev box?

(Don't forget to hook into OWSTimer.exe and not W3P.exe when debugging!)
Free Windows Admin Tool Kit Click here and download it now
November 2nd, 2009 4:17pm

Oh, also make sure that the Outgoing Email Settings (Central Administration) have been configured correctly too!
November 2nd, 2009 4:17pm

Hi Sudhir,

I agree with Martin, you definetly need to log what happens inside the timer job.

I suggest to always use a try / catch block within the methods of your assembly, and trace out (using System.Diagnostics.Trace, http://msdn.microsoft.com/en-us/library/system.diagnostics.trace.aspxand its methods for example, TraceInformation http://msdn.microsoft.com/en-us/library/system.diagnostics.trace.traceinformation.aspx) the exceptions if happen. Also, it is good practice to trace out each entry and exit points. Use the WinDbg (http://msdn.microsoft.com/en-us/library/cc266321.aspx, http://windbg.dumpanalysis.org/) to catch the output.

You can redirect the trace output to file (see <listeners> Element for <trace> http://msdn.microsoft.com/en-us/library/zh3a61fw.aspx, and Trace Listeners http://msdn.microsoft.com/en-us/library/aa983739(VS.71).aspx)

You can even write your log messages to SharePoint Unified Logging System as described here:
SharePoint: Writing messages to ULS (Unified Logging System)
http://weblogs.asp.net/gunnarpeipman/archive/2009/02/25/sharepoint-writing-messages-to-uls-unified-logging-system.aspx

Peter

Free Windows Admin Tool Kit Click here and download it now
November 2nd, 2009 4:20pm

Hi,

Yes the email setting is fine or else it should not have work with the webpart. Is there anything different betweent Webpart and timer job for sending the mails ?

And I am not able to doing the debugging :( .
BR,
Sudhir
November 2nd, 2009 4:23pm

Hi Sudhir,

Yes Outgoing mail settings should be OK.

Some tips about debugging (see Debugging Feature receivers & timer jobs section):
SharePoint Debugging and Logging Tips and Tricks
http://www.andrewconnell.com/blog/archive/2008/06/11/SharePoint-Debugging-and-Logging-Tips-and-Tricks.aspx

Peter
Free Windows Admin Tool Kit Click here and download it now
November 2nd, 2009 4:27pm

Hi,

My timer job is working fine now.

The problem was SPUtility.SendEmail(SPContext.Current.Web, false, false, To.ToString(), Subject, mailBody);

It was not getting the value SPContext.Current.Web. I have changed it to myweb which i am getting from site.openweb().


Thank you all for reply.

BR,

Sudhir
  • Marked as answer by Sudhir8782 Tuesday, November 03, 2009 6:06 AM
November 3rd, 2009 9:06am

There's no context  inside Timer Jobs that's why SPWeb object needs to be instantiated. I strongly recommend using native .NET SmtpClient class inside timerjobs. "SPUtility.SendEmail" contains few "SPContext.Current" references and it can cause problems in certain scenarios.

Free Windows Admin Tool Kit Click here and download it now
February 27th, 2015 3:40am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics