locked
SPUtility.SendEmail Function truncating htmlbody RRS feed

  • Question

  •  

    Hi All,

     

    I am sending an email to my sharepoint portal user through SPUtility.SendEmail Function. Its working fine but when ever my message body exceeds 2048 characters it gets truncated by the function.

    Is there any maximum lenght for email message body?

    Please help...

     

     

    Nitin Gupta

    Thursday, February 28, 2008 8:31 AM

Answers

  • Hi,

    I get rid of above error. I am using stringbuilder for body string and I use the AppendLine() method after every two line. After that every thing is working fine. We can also use SMTPClient to send email. Code for SMTP is as follow:

     System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                          message.From = new System.Net.Mail.MailAddress(list.ParentWeb.Site.WebApplication.OutboundMailSenderAddress);
                          message.To.Add(new System.Net.Mail.MailAddress(user.User.Email));
                          message.Subject = "email subject.";
                          message.Body = mailBody.ToString();
                          message.IsBodyHtml = true;
                          System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(web.Site.WebApplication.OutboundMailServiceInstance.Server.Address);
                          smtpClient.UseDefaultCredentials = true;
                         smtpClient.Send(message);
                          smtpClient = null;
                          message.Dispose();
    I hope this will help someone.
    • Marked as answer by Mike Walsh FIN Friday, February 11, 2011 9:17 PM
    Wednesday, December 16, 2009 1:23 PM
  • Hi Nitin,

     

    I would the use System.Net.Mail to send the email. The one provided in SPUtility is a little restrictive. You can use the OutboundMailSenderAddress property of SPWebApplication to get the SMTP address that the server is using.

     

    Cheers

     

    Friday, February 29, 2008 2:04 PM
  • I just ran into this issue as well, and discovered that SPUtility.SendEmail seems to cap the length of a single line.  I added newlines after each tag in the body of my email and it works like a charm.
    Monday, February 23, 2009 11:50 PM

All replies

  • Hi Nitin,

     

    I would the use System.Net.Mail to send the email. The one provided in SPUtility is a little restrictive. You can use the OutboundMailSenderAddress property of SPWebApplication to get the SMTP address that the server is using.

     

    Cheers

     

    Friday, February 29, 2008 2:04 PM
  • Have you got any other solution for this problem?

    I am also facing this issue, and unfortunatly i m not allowed to use "System.Net.Mail" this namespace in my org.
    Sunday, February 8, 2009 9:36 AM
  • I just ran into this issue as well, and discovered that SPUtility.SendEmail seems to cap the length of a single line.  I added newlines after each tag in the body of my email and it works like a charm.
    Monday, February 23, 2009 11:50 PM
  • Hi,

    I am facing same issue, can you please explain more about the new-line after each tag. This is "<BR>" html tag or something else?
    Tuesday, December 15, 2009 11:08 AM
  • By default, the message body cannot exceed 2048 characters. SendEMail will truncate all characters beyond 2048. The workaround is to ensure that no single line in your message body exceeds 2048 characters. Simply add newline characters (for <br> use &crlf or &amp;crlf) as needed to keep the lines under the 2048 character limit. 



    Thanks
    Ganesh Jat [My Blog | LinkedIn | Twitter ]
    Tuesday, December 15, 2009 12:36 PM
  • Hi,

    I get rid of above error. I am using stringbuilder for body string and I use the AppendLine() method after every two line. After that every thing is working fine. We can also use SMTPClient to send email. Code for SMTP is as follow:

     System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                          message.From = new System.Net.Mail.MailAddress(list.ParentWeb.Site.WebApplication.OutboundMailSenderAddress);
                          message.To.Add(new System.Net.Mail.MailAddress(user.User.Email));
                          message.Subject = "email subject.";
                          message.Body = mailBody.ToString();
                          message.IsBodyHtml = true;
                          System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(web.Site.WebApplication.OutboundMailServiceInstance.Server.Address);
                          smtpClient.UseDefaultCredentials = true;
                         smtpClient.Send(message);
                          smtpClient = null;
                          message.Dispose();
    I hope this will help someone.
    • Marked as answer by Mike Walsh FIN Friday, February 11, 2011 9:17 PM
    Wednesday, December 16, 2009 1:23 PM
  • SPUtility.SendMail has a limit on the number of characters in one line. There is no restriction on the overall number of characters. One line should not exceed 2048 characters.

    So, a simple thing is to break your string (htmlBody) in chunks of 2048 or less characters in one line. You can use a string function Substring(int start index, int length) to break your string into small chunks, use a for loop or anything you like. The important thing is how to break the string. It's by appending "\n" after each chunk. This works for me and should work for you.

    Example:

    htmlBody = htmlBody + "\n" + myNewChunk + "\n"; //something like this.

    Below is what works for me, its the original piece of code, only thing concerns you is the placement of "\n" within the string (try different combinations, it can frustrate you a little before it works):

    htmlBody = "\n" + htmlBody.Replace(EmailTemplateTokens.AllEmployees, "<p>" + replacementString + "\n" + EmailTemplateTokens.AllEmployees); 

    Hope this helps--
    Rehman.

    Rehman Gul
    Monday, June 28, 2010 5:38 AM
  • this worked for me...Thanks Ani...you are time saver for me.
    Friday, February 11, 2011 8:46 PM