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

I'm trying to use Timer Jobs as part of a system to send out special periodic emails to users of our SharePoint site collections. The timer jobs work... except when it comes to actually sending the emails.

They can perform any other task as if they are the System Account, including creating and modifying list items. But when I try to send an email with it, I get the following error and stacktrace:

Mailbox unavailable. The server response was: 5.7.1 Client does not have permissions to send as this sender

at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)

Apart from the acquisition of the SPSite object (fetched from SPWebApplication.Sites), everything else of this routine is exactly the same as an email system I use within a workflow on the site collection (which uses SPWorkflowActivationProperties.Site). The same To, the same From (and thus the sender should be valid, which makes the error perplexing), and the same SMTP routine. We use System.Net.Mail to send the emails.

Below is the Execute(Guid) function of my Timer Job.

SPWebApplication oWebApp = this.Parent as SPWebApplication;
SPContentDatabase oConDB = oWebApp.ContentDatabases[targetInstanceId];

// Iterate through the site collections which the timer job is activated on
foreach (string str in oWebApp.Properties.Keys.OfType<string>().Where(k => k.EndsWith(key)))
{
    using (SPWeb targetWeb = oConDB.Sites[oWebApp.Properties[str].ToString()].RootWeb)
    {
        SPList taskList = targetWeb.Lists["Tasks"];

        // The actual code here uses a mediator class to build a System.Net.Mail.MailMessage with a valid recipient and sender. 
        // For the purposes of this question, I've expanded the code's class to show what happens on the System.Net.Mail level rather than through the mediator.

        System.Net.Mail.MailMessage message = System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
        message.From = "sharepoint@company.com";
        message.To.Add("tester@company.com");
        message.Subject = "TIMER ALERT JOB";
        message.Body = "If you receive this email, the Timer Job is working and you can start working on all that meaty, complex listitem-based do-everything system!"
        message.IsBodyHtml = true;

        System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(oWebApp.OutboundMailServiceInstance.Server.Address);
        smtpClient.UseDefaultCredentials = true;
        try { smtpClient.Send(message); }
        finally { smtpClient = null; message.Dispose(); }
    }        
}

Now, as mentioned above, the only difference in the code between the Timer Job above and the workflow version is how the SPSite object is fetched. Yet, only the Timer Job version will throw the error. Is there something else needed for sending email with a Timer Job, is there something I am missing? Is it as simple a matter as SPSecurity.RunWithElevatedPrivileges? The fact it works on the workflow (as well as anywhere else, this mail system is also used in some ASPX pages and Event Handlers without error) but not in the Timer Job leads me to believe there's something specific about Timer Jobs rather than it being a system configuration. Or, at least, that something about the Timer Job makes it different enough from the workflows and everything else to make the server configuration consider it invalid...

share|improve this question

2 Answers 2

up vote 5 down vote accepted

Timer jobs run under the farm account. Have you checked that this account has permissions on your mail server?

share|improve this answer
    
Where would I go about confirming the farm account's identity (I'm in charge of object model work, not so much the farm topology)? I'm under the impression that the Farm Account is the same as the System Account under which Workflows are run under, as well as SPSecurity.RunWithElevatedPrivileges, but if this is what is probably tripping me up, I should investigate. –  Grace Note Apr 15 '11 at 18:59
    
If you go to Administrative Tools-->Services and look for SharePoint Timer service (called "SharePoint 2010 Timer" on my machine - I don't have a 2007 farm running to check) you can see the account there. –  SPDoctor Apr 15 '11 at 19:06
    
The value for "Log On As" for the "Windows SharePoint Services Timer" service, which I assume will be my WSS 3.0 equivalent of your SP 2010 Timer, is the System Account. So it should have permissions on the mail server on the same bound that it does for the Workflows. –  Grace Note Apr 15 '11 at 19:09
    
Yes, that's the right service. Worth checking anyway. –  SPDoctor Apr 15 '11 at 19:14
    
Wasn't directly the cause of my specific issue, but farm account permissions were indeed at the root of it. The login should've been fine, but my development server wasn't allowed to properly access the mail server like the production server does. Something in the services for workflows gets around this issue, but the Timer service did not get affected. Once we incorporated proper access for the dev server, it worked! Thank you much. –  Grace Note Apr 15 '11 at 20:13

I would recommend using SPutility.Sendemail() when sending email from SharePoint. Allthough sending mail using System.Net.Mail would be fine as well if needing more low level control.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.utilities.sputility.sendemail.aspx

share|improve this answer

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.