0

I am unable to send emails from SharePoint code behind. I've tried the following code:

public static void SendEmail(SPWeb spWeb, string Subject, string message, string ToName, string ToEmail)
{
    StringDictionary messageHeaders = new StringDictionary();
    messageHeaders.Add("from", "bpm@abfoods.com.pk");
    messageHeaders.Add("to", ToEmail);
    messageHeaders.Add("subject", Subject);
    messageHeaders.Add("content-type", "text/html");

    string Body = "";
    Body += "Dear " + ToName + ",";
    Body += "<br/>" + message + "<br/>Regards,<br/>Administrator";
    bool blnIsEmailServerSet = SPUtility.IsEmailServerSet(spWeb);
    bool result=false;
    try
    {
        SPSecurity.RunWithElevatedPrivileges(delegate ()
        {
            result = SPUtility.SendEmail(spWeb,false,false, ToEmail,Subject, message);
        });
    }
    catch (Exception ex)
    {

    }  
}

Unable to send email through SPutility.sendemail http://emailfromsharepoint.blogspot.com/2012/04/send-email-from-sharepoint.html

didn't help me.

blnIsEmailServerSet is returning true but result variable is returning false when trying to send email.

Kindly help.

Sendemail method is returning false, why???

enter image description here

enter image description here

UPDATE:

Also tried the following but didn't work:

public static void SendEmail(SPWeb spWeb,string Subject, string message, string ToName, string ToEmail)
    {

        StringDictionary messageHeaders = new StringDictionary();
        messageHeaders.Add("from", "bpm@abfoods.com.pk");
        messageHeaders.Add("to", ToEmail);
        messageHeaders.Add("subject", Subject);
        messageHeaders.Add("content-type", "text/html");

        string Body = "";
        Body += "Dear " + ToName + ",";
        Body += "<br/>" + message + "<br/>Regards,<br/>Administrator";
        bool blnIsEmailServerSet = SPUtility.IsEmailServerSet(spWeb);
        bool result=true;
        //try
        //{

            //SPSecurity.RunWithElevatedPrivileges(delegate ()
            //{
            //     result = SPUtility.SendEmail(spWeb,true,true, ToEmail,Subject, message);
            //});
            string webUrl = spWeb.Url;
            SPSecurity.RunWithElevatedPrivileges(delegate ()
            {
                using (SPSite site = new SPSite(webUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        result = SPUtility.SendEmail(web, messageHeaders, message);
                    }
                }
            });
        //}
        //catch (Exception ex)
        //{

        //}


    }
  • 1
    I've debugged the complete code, there's no any exception although SPUtility.SendEmail method is returning false which is being saved in result variable – Alina Anjum Jun 22 '18 at 8:36
  • What ToEmail value is? One or few? External addresses? – AlexZh Jun 22 '18 at 8:48
  • Do you receive other notifications (such as welcome to sp groups)? – AlexZh Jun 22 '18 at 8:54
  • @AlexZh there's only one email in ToEmail variable – Alina Anjum Jun 22 '18 at 8:56
  • @AlexZh No I didn't receive such notifications – Alina Anjum Jun 22 '18 at 8:57
0

Try the following code:

string webUrl = spWeb.Url;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(webUrl))
    {
        using (SPWeb web = site.OpenWeb())
        {
            //result = SPUtility.SendEmail(web, true, true, ToEmail, Subject, message);
            result = SPUtility.SendEmail(web, Subject, message);
        }
    }
});

UPD: why did you use false params? enter image description here

You may need to configure outgoing emails in your farm:

Configure outgoing email for a SharePoint Server farm

SharePoint 2013 Step by step: Setup for Outgoing Email Setting with SMTP virtual server (Part 2)

  • Can you please explain what is the difference between your answer and my code? – Alina Anjum Jun 22 '18 at 8:29
  • You need to recreate SPSite/SPWeb objects inside RunWithElevatedPrivileges scope (with new permissions). – AlexZh Jun 22 '18 at 8:31
  • tried your code, still did not receive any email :( – Alina Anjum Jun 22 '18 at 8:33
  • I did some changes in answer. – AlexZh Jun 22 '18 at 8:46
  • still didn't work. I've added the complete code in my update. – Alina Anjum Jun 22 '18 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.