none
SPUtility.SendEmail() character missing SharePoint 2010 RRS feed

  • Question

  • i am facing a strange issue.

    i using SPUtility.SendEmail() to send mail. some time mail format work fine. some time goes wrong.

    What Wrong happened

    some time from the whole body some characters are missing. i heared that there is a character limitation (i.e. 2048). but it happened it should happen always not some time. another case why it removing characters in middle not last characters.


    Thanks and Regards

    Er.Pradipta Nayak
    Visit my Blog
    ISHIR

    Wednesday, November 21, 2012 10:13 AM

Answers

  • The method I used came out of testing and inspecting the source code of the email once it was delivered to the Outlook mailbox.

    If you have a look at the method signature for SPUtility.SendEmail(web, headers, body), the headers property is a list of key/values (as you know), which still doesn't give you complete control over the construction of the html within the message.

    If you use reflection to look at the methods used, SendEmail(SPWeb web, StringDictionary messageHeaders, string messageBody) is quite different from SendEmail(SPWeb web, bool fAppendHtmlTag, bool fHtmlEncode, string to, string subject, string htmlBody).


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    Wednesday, November 21, 2012 12:30 PM

All replies

  • I've had issues with SPUtility.SendEmail() when constructing HTML emails where random things happen with characters and formating when the email is viewed (esspecially in Outlook). The way I got around it was to add the HTML headers myself, like this:

    var body = String.Format("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html><head>" +
                                             "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=8\" /><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />" +
                                                                                  "{0}</head><body>{1}</body></html>", AddStyles(), emailBody);

    Then, call SPUtility.SendEmail with the AppendHTMLTag set to false, and HTMLEncode set to false, like this:

    SPUtility.SendEmail(web, false, false, recipient, subject, body)

    Emails were formatted correctly after that.

    I blogged about it here: http://matthewyarlett.blogspot.co.uk/2012/10/inconsistent-css-styles-applied-to.html 


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    Wednesday, November 21, 2012 10:41 AM
  • thanks for your reply..

    so is there any difference in 

    SPUtility.SendEmail(oWeb.Site.RootWeb, headers, mailBody)  and SPUtility.SendEmail(oWeb.Site.RootWeb,false,false,mailTo,mailSubject, mailBody,false) ?

    right now i am using SPUtility.SendEmail(oWeb.Site.RootWeb, headers, mailBody) . some time i faced such issue not always.


    Thanks and Regards

    Er.Pradipta Nayak
    Visit my Blog
    ISHIR

    Wednesday, November 21, 2012 11:55 AM
  • The method I used came out of testing and inspecting the source code of the email once it was delivered to the Outlook mailbox.

    If you have a look at the method signature for SPUtility.SendEmail(web, headers, body), the headers property is a list of key/values (as you know), which still doesn't give you complete control over the construction of the html within the message.

    If you use reflection to look at the methods used, SendEmail(SPWeb web, StringDictionary messageHeaders, string messageBody) is quite different from SendEmail(SPWeb web, bool fAppendHtmlTag, bool fHtmlEncode, string to, string subject, string htmlBody).


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    Wednesday, November 21, 2012 12:30 PM