locked
Cannot send mail in event receiver RRS feed

  • Question

  • how to send mail? install smtp server is a must??
     the code is fine in windows application, but can not work in event receiver.
    
    public static bool SendMail(string gMailAccount, string password, string to, string subject, string message)
      {
       try
       {
        NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password);
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(gMailAccount);
        msg.To.Add(new MailAddress(to));
        msg.Subject = subject;
        msg.Body = message;
        msg.IsBodyHtml = false;
        SmtpClient client = new SmtpClient("smtp.gmail.com");
        client.EnableSsl = true;
        client.UseDefaultCredentials = false;
        client.Credentials = loginInfo;
        client.Send(msg);
    
        return true;
       }
       catch (Exception)
       {
        return false;
       }
    
      }
    

     

    thanks in advance.

    Sunday, March 13, 2011 11:59 AM

Answers

  •  

    > install smtp server is a must??

    Is email for Alerts currently working? If so then all you need is SPUtility.SendEmail. As Sundar mentioned, the event receiver runs with the user's context and they won't have rights to use SendEmail so you will need to wrap that code in RunWithElevatedPrivileges.

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
     bool result = SPUtility.SendEmail(web, appendHtmlTag, htmlEncode, toAddress, subject, message);
    });
    
    

     

    If you don't have working email in SharePoint then your code should work, once wrapped in SPSecurity.RunWithElevatedPrivileges. You will need to find out what credentials will be needed as they probably won't be those of the current logged in user or the SharePoint accounts.

     

     


    Mike Smith TechTrainingNotes.blogspot.com
    • Marked as answer by Seven M Friday, March 25, 2011 2:38 AM
    Sunday, March 13, 2011 10:44 PM
  • By default the event receiver runs in the context of the user who's creating/updating/deleting list item. You need an SMTP server or other any e-mail compatible server to trigger e-mails.

    Can you add Trace Statements and use DebugView tool to see what's happening inside SendMail method ?

    Is it permission issue ? (or) Is it SMTP server not available ?


    Sundar Narasiman
    • Marked as answer by Seven M Friday, March 25, 2011 2:38 AM
    Sunday, March 13, 2011 10:24 PM

All replies

  • By default the event receiver runs in the context of the user who's creating/updating/deleting list item. You need an SMTP server or other any e-mail compatible server to trigger e-mails.

    Can you add Trace Statements and use DebugView tool to see what's happening inside SendMail method ?

    Is it permission issue ? (or) Is it SMTP server not available ?


    Sundar Narasiman
    • Marked as answer by Seven M Friday, March 25, 2011 2:38 AM
    Sunday, March 13, 2011 10:24 PM
  •  

    > install smtp server is a must??

    Is email for Alerts currently working? If so then all you need is SPUtility.SendEmail. As Sundar mentioned, the event receiver runs with the user's context and they won't have rights to use SendEmail so you will need to wrap that code in RunWithElevatedPrivileges.

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
     bool result = SPUtility.SendEmail(web, appendHtmlTag, htmlEncode, toAddress, subject, message);
    });
    
    

     

    If you don't have working email in SharePoint then your code should work, once wrapped in SPSecurity.RunWithElevatedPrivileges. You will need to find out what credentials will be needed as they probably won't be those of the current logged in user or the SharePoint accounts.

     

     


    Mike Smith TechTrainingNotes.blogspot.com
    • Marked as answer by Seven M Friday, March 25, 2011 2:38 AM
    Sunday, March 13, 2011 10:44 PM