3

unable to send a mail through system.net api and sputility.sendmeail() .

I tried the below code for sending mail through the above. but it failed to send mail. But if I tried with PowerShell approach , I am able to send mails without any issues.

SPutility code :

    try
        {

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite msite = new SPSite("http://sharepointuat:400/sites/dms"))
                {
                    using (SPWeb mweb = msite.OpenWeb())
                    {
                        string from = "dmsadmin@mydomain.in";

                        string to = "user11@incompany.com;user2201@incompany.com";
                        string cc = "user33@incompany.com";
                        string bcc = "";
                        string subject = "hello sendmail from SP 2010  THRU SPUTILITY";
                        string body = "hello sendmail from SP 2010  THRU SPUTILITY";

                        string emailSummary;
                        var messageHeaders = new StringDictionary();

                        messageHeaders.Add("to", to);
                        messageHeaders.Add("from", from);
                        messageHeaders.Add("subject", subject);
                        messageHeaders.Add("cc", cc);
                        messageHeaders.Add("bcc", bcc);
                        string mimeType = "text/html";
                        messageHeaders.Add("content-type", mimeType);


                        bool sendMail = SPUtility.SendEmail(mweb, messageHeaders, body)


                      //[Microsoft.SharePoint.Utilities.SPUtility]::SendEmail($web,0,0,$email,$subject,$body)



                    }
                }
            });
        }
        catch (Exception exp11)
        {
            Console.WriteLine(exp11.Message + "__"  + exp11.StackTrace);
            throw exp11;
        }

Powershell code which is working:

 #add-pssnapin "microsoft.sharepoint.powershell"
   $email = "a@in.com;s@in.com"
   $subject = "mail.domain test subject1111"
    $body = "Test body2222 sending mail to S n p"
    $site = New-Object Microsoft.SharePoint.SPSite                
       "http://sharepointuat:400/sites/DMS"
        $web = $site.OpenWeb()
         [Microsoft.SharePoint.Utilities.SPUtility]::SendEmail($web,0,0,
            $email,$subject,$body)

The above PowerShell code is working fine.

I am in need of sending mail through sputility, C# & SharePoint SSOM.

Can anyone help , whether I am missing any parameter missing from my C# code?

  • The documentation for SPUtility.SendEmail method says "This method supports sending email messages only to valid SharePoint user email addresses.", msdn.microsoft.com/en-us/library/ms460489.aspx, are the addresses in TO header valid SharePoint user email addresses? – SlackerAmit Sep 29 '16 at 17:11
  • Yes.of course. The strange thing is that, it works in powershell. I have checked with correct valid user email ids only. Any other settings I need to do? – SaMolPP Sep 29 '16 at 17:14
  • Is this a console/windows app code or a web app code running on IIS? – SlackerAmit Sep 29 '16 at 17:17
  • For testing purposes I created a simple console application. But my actual environment is visual web part targeting in SP 2010 env – SaMolPP Sep 29 '16 at 17:21
  • even i tried with the system.net smtp.sendmail() as well. that also failed in sending the mail to target users! So is there any issue with smtp server? i for the sputility object,i have correctly mentioned/configured the outgoing emailsettings in my central admin. thats the reason thru powershell, i am able to send mails,but why sputility is not taking/reading the outgoing mail settings. v.strange issue. – SaMolPP Sep 29 '16 at 18:21
3

By default, SPUtility.SendEmail() method picks the From address from Outgoing E-Mail Settings in Central administration. Use SPUtility.IsEmailServerSet method to check if server is configured with SMTP mail settings.

You should use "," instead of ";" in the to field in the header.

Try below code:

using (SPSite oSPSite = new SPSite("https://sharepoint.com"))  //Site collection URL
            {
                using (SPWeb oSPWeb = oSPSite.OpenWeb("News"))  //Subsite URL
                {
                    StringDictionary headers = new StringDictionary();

                    headers.Add("from", "dmsadmin@mydomain.in");
                    headers.Add("to", "user11@incompany.com,user2201@incompany.com");
                    headers.Add("cc","user33@incompany.com");
                    headers.Add("subject", "Welcome to the SharePoint");
                    headers.Add("fAppendHtmlTag","True"); //To enable HTML format

                    System.Text.StringBuilder strMessage = new System.Text.StringBuilder();
                    strMessage.Append("Message from CEO:");

                    strMessage.Append("<span style='color:red;'> Make sure you have completed the survey! </span>");
                    SPUtility.SendEmail(oSPWeb, headers, strMessage.ToString());

                }
            }

Read more:

  • This worked well. Once caveat here. I found that in my implementation (certainly there are differences between the farms) -- when I included from in the headers dictionary, the SendEmail method returns true but the message is not delivered. However, by removing / commenting out that one line. It works for me. Hope that helps. – Glenn Ferrie Mar 27 '18 at 18:09

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.