Sending Emails in Sharepoint

Sending Emails in Sharepoint

What is the good practice for sending emails from my sharepoint webparts and/or customized features /or through code.

You can  use the normal Smtp Client Class of System.Net.Mail namespace  to send email or use the built in Utilities (Microsoft.SharePoint.Utilities.SPUtility )

 

SharePoint SPUtility class has SendEmail() method  to send email to any email address. By default, SPUtility.SendEmail() method take the ‘From address’ from Outgoing E-Mail Settings in Central administration. If it is not set , you can use SPUtility.IsEmailServerSet method to check if server is configured with SMTP mail settings or not.

SPUtility.SendEmail() method which by default picks the ‘From address’ from Central administartion-> Operations-> Outgoing E-Mail Settings.

using Microsoft.SharePoint.Utilities;
SPUtility.SendEmail(SPContext.Current.Web, false, false,
     "toemailaddress@mail.com", "subject",
     "body");

SPUtility sendemail method has some limitations like:
  • It don’t Provide support for attachments
  • The from address will be the “Outbound Sender Address” configured in the Central administration .
  • Message body should not exceed 2048 characters.

If you want to send mail with attachment and long mail, use the System.Net.Mail class methods

 

in both case normal Smtp Client Class of System.Net.Mail namespace  to send email or use the built in Utilities (Microsoft.SharePoint.Utilities.SPUtility ) , we need to configure Web Application Outgoing E-Mail Settings for that particular Web Application in which your site is hosted in Central Administration

send mail by Sharepoint Server

Send mail by Sharepoint Server

 

Example:

send mail by  Microsoft.SharePoint.Utilities.SPUtility 
 using (SPSite objSPSite = new SPSite(objSPSiteId))
            {
                using (objSPWeb = objSPSite.AllWebs[properties.OpenWeb().ID])
                {
                    bool Mailresult = SPUtility.SendEmail(objSPWeb, true, true, "emailid@techcontents.com", "test Mail.", "Tech Content");
                }
            } 

In some cases, you may need to run this code with elevated privileges:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
bool Mailresult= SPUtility.SendEmail(objSPWeb, true, true, “emailid@techcontents.com”, “test Mail.”, “Tech Content”);

});

SendEmail method returns a boolean, indicating if sending the email was successful or not.

 

Example:

send mail by   SmtpClient Class of System.Net.Mail namespace 

   using (SPSite objSPSite = new SPSite(objSPSiteId))
            {
                using (objSPWeb = objSPSite.AllWebs[properties.OpenWeb().ID])
                {
                    
                    string smtpServer = objSPSite.WebApplication.OutboundMailServiceInstance.Server.Address;
                    string smtpFrom = objSPSite.WebApplication.OutboundMailSenderAddress;
                    MailMessage mailMessage = new MailMessage(smtpFrom, "demo@techcontents.com");
                    mailMessage.Body = "Test Body";
                    mailMessage.Subject = "Test Subject";
                    SmtpClient objSmtpClient = new SmtpClient();
                    objSmtpClient.Send(mailMessage);
                }
           }

We need put  these entry in web.config :

<system.net>

<mailSettings>
<smtp from=”def@mno.com”>
<network host=”localhost”/>.
</smtp>
</mailSettings>
</system.net>

Note: while on event receiver to send the mail by SPUtility class, you may get some issue, event receiver runs in the context of an HTTP request. It is known that SPUtility.SendEmail has issues with this. A common solution is to set HttpContext.Current to null while sending the email:

HttpContext.Current = null;
SPUtility.SendEmail(currt.Web, headers, bodyText.ToString());

Leave a Reply

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

7 + 9 =


All Rights Reserved 2019 | DesignzRush (OPC) Pvt. Ltd.
Inline
Inline