HTML Email body getting corrupted with SPUtility.SendEmail

HTML Email body getting corrupted with SPUtility.SendEmail Tutorial

In this article I would like discuss about the issue “HTML Email body getting corrupted while sending email with SPUtility.SendEmail”.
Issue: Email body getting formatting issue:

 HTML Email body getting corrupted with SPUtility.SendEmail

HTML Email body getting corrupted with SPUtility.SendEmail

Expected email body:

HTML Email body getting corrupted with SPUtility.SendEmail Tutorial
HTML Email body getting corrupted with SPUtility.SendEmail Tutorial



I was trying to send email to project owner as soon as user complete the task with the below syntax:
SPUtility.SendEmail(SPContext.Current.Web, false, false, strEmailTo, strMailSubject , strMailBody);
Where strMailBody:

<div style=’font-family:Calibri;’>Hi Project Owner,<br />
<br /> The Task Owner – Krishna has submitted task vales successfully. Please review as per your availability<br /><br />
<table><tr><td style=’font-weight:bold’>Tasks for the Month: </td><td>201710</td>
<tr><td style=’font-weight:bold’>Report Type: </td><td>Rule Level</td></tr>
<tr><td style=’font-weight:bold’>Project Type: </td><td>Non-SFO</td></tr></table>
</br>Completed Task Details: <br />
<table border=’2′ style=’font-family:Calibri; font-size:smaller;border-collapse:collapse;border-color:rgb(195,195,195)’>
<tr style=’font-weight:bold’><td>RuleOwner</td><td>TotalRecordsAssigned</td><td>InprogressRecords</td><td>DraftRecords</td><td>CompletedRecords</td><td>Status</td></tr>
<tr style=’background-color:#52778d;color:white’><td>Glenn Jones</td><td>38</td><td>0</td><td>0</td><td>38</td><td>Completed</td></tr>
<tr><td>Task Owner 1</td><td>65</td><td>0</td><td>0</td><td>65</td><td>Completed</td></tr>
<tr><td>Task Owner 2</td><td>74</td><td>0</td><td>0</td><td>74</td><td>Completed</td></tr>
<tr><td>Task Owner 3</td><td>59</td><td>0</td><td>0</td><td>59</td><td>Completed</td></tr>
<tr><td>Task Owner 4</td><td>49</td><td>0</td><td>0</td><td>49</td><td>Completed</td></tr>
<tr><td>Task Owner 5</td><td>116</td><td>0</td><td>0</td><td>116</td><td>Completed</td></tr>
<tr><td>Task Owner 6</td><td>32</td><td>0</td><td>0</td><td>32</td><td>Completed</td></tr>
<tr><td>Task Owner 7</td><td>14</td><td>0</td><td>0</td><td>14</td><td>Completed</td></tr></table>
<br />InProgress Task Owners: <br />
<table border=’2′ style=’font-family:Calibri; font-size:smaller;border-collapse:collapse;border-color:rgb(195,195,195)’>
<tr style=’font-weight:bold’><td>RuleOwner</td><td>TotalRecordsAssigned</td><td>InprogressRecords</td><td>DraftRecords</td><td>CompletedRecords</td><td>Status</td></tr>
<tr><td>Task Owner 11</td><td>155</td><td>155</td><td>0</td><td>0</td><td>Inprogress</td></tr>
<tr><td>Task Owner 12</td><td>78</td><td>0</td><td>1</td><td>77</td><td>Inprogress</td></tr>
<tr><td>Task Owner 13</td><td>16</td><td>16</td><td>0</td><td>0</td><td>Inprogress</td></tr>
<tr><td>Task Owner 14</td><td>64</td><td>64</td><td>0</td><td>0</td><td>Inprogress</td></tr>
<tr><td>Task Owner 15</td><td>108</td><td>108</td><td>0</td><td>0</td><td>Inprogress</td></tr>
<tr><td>Task Owner 16</td><td>60</td><td>59</td><td>1</td><td>0</td><td>Inprogress</td></tr>
<tr><td>Task Owner 17</td><td>158</td><td>136</td><td>22</td><td>0</td><td>Inprogress</td></tr></table>
<br /> Thank you,</br>GS Team</div>

If we notice the SPUtility.SendEmail it supports only string for Email body parameter. String variable limitation is only 509 characters (MSDN Article of String limitations).

Since the String limitation is only 509 characters so it gets corrupt.

Solution:
We have to use the classic .net c# SMTP code / syntax to send the email with this kind of email body (more than 509 characters length).
Below is the code snippet for the same.

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(AdminEmail);
message.CC.Add(“contoso@gmail.com”);
message.Subject = strMailSubject;
message.From = new System.Net.Mail.MailAddress(“noreply@contoso.com”);
message.Body = strMessage.ToString();
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(<<SMTP Server configured in Central admin>>);
smtp.Send(message);

Hope this helps !!!!!!

Similar SharePoint 2013 Tutorials


Leave a Reply