SPUtility.SendEmail

In my project I need to send mail to Administrator, while adding items to a list using Webpart. I have tried ASP.Net, System.Net namespace but I need to read all the server and mail settings from any config file. After I got sharepoint’s own mechanism to send mail.

SPUtility.SendEmail(SPContext.Current.Site.OpenWeb(), false, false, "admin@myserver.com", "Hello World", "This is a Sample Mail");

This will send a mail to “admin@myserver.com”, with “Hello World” as subject and “This is a Sample Mail” as body. You can send HTML emails by changing the third parameter to TRUE.

Update : You may need to use SPSecurity.RunWithElevatedPrivileges method to send mails.

Url :

http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx

This method enable users to Execute the specified method with Full Control rights even if the user does not otherwise have Full Control.

This entry was posted in .Net, sharepoint and tagged , , , , , , , , . Bookmark the permalink.

11 Responses to SPUtility.SendEmail

  1. Saurabh Jain says:

    hi i have used this code but html tags is not working . please help

  2. anuraj says:

    Hi Jain,

    Where you are using HTML tags? in message body? And how you come to know it is not working? I got the same issue, but it was the problem of the Outlook, try changing the mail display format.

    Anuraj

  3. morten says:

    Hi anuraj!
    My problem is that the SPUtility.SendEmail returns false and I don’t know how I can check why it is not sending anything. I have also tried to send mail using System.NET.Mail, and it also fails.

    Any suggestions ?

    Morten

  4. Frederik says:

    How to I change or set the display name of an e-mail send with the SPUtility.sendEmail ?

    I am looking for the receiver to get a mail with Frederik [f@k2c.dk] in the FROM field…

    Any suggestions?

    / Frederik

  5. anuraj says:

    Sorry Frederik. I don’t think we can do it. [:(]

  6. jafar says:

    I have almost the same problem SpUtility.SendEmail
    return True but the email is not sent and i could not find any thing in the Applog note I am using SpUtility.SendEmail from SPTimerJob

  7. Janey Wang says:

    Hoi , I think you might be able to do it. Please check http://www.myfatblog.co.uk/?p=79, I have tried it in my project and it works. :)

  8. Gunnar says:

    You should check SharePoint logs at information level to find out if there are messages that may lead you to source of your problem.

  9. anuraj says:

    Thanks Gunnar, thanks for the valuable information.

  10. Alex says:

    Hi jafar,
    you cannot use SPUtility.SendEmail() in a timer job, because SendEmail() uses SPContext that is not present in timerjobs.

  11. Drew says:

    I’ve been trying to get SharePoint 2007 to send email via timer job for several days now and am finally going out to the internetz for help. The timer job is installing a successfully running every 5 minutes. However, I am not receiving any emails. Here is the code that I am using.

    SmtpClient smtp = new SmtpClient(this.WebApplication.OutboundMailServiceInstance.Server.Address);

    smtp.UseDefaultCredentials = (smtp.Credentials == null);

    MailMessage message = new MailMessage(“email@email.com”, “email@email.com”);

    smtp.Send(message);

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>