2016-08-25 2 views
-1

Я отправляю электронную почту из своего приложения, но когда я получаю электронное письмо в своем поле, формат прерывается. Как я могу отображать или создавать шаблон электронной почты с помощью Tittle, Body и Signature.Как отобразить правильный формат электронной почты при отправке писем

Мой HTML электронный дизайн ниже код,

  var _mail = new MailMessage(); 
      { 

       SmtpClient smtp = new SmtpClient(); 
       smtp.Port = 25; 
       smtp.EnableSsl = false; 
       smtp.Host = host; 
       smtp.Timeout = 8900000; 
       smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
       smtp.UseDefaultCredentials = false; 
       smtp.Credentials = new System.Net.NetworkCredential(Username, Password); 
       _mail.From = new MailAddress(FromEmailAddress); 
       _mail.To.Add(ToEmailAddress); 
       _mail.Subject = "New Claim Booked"; 
       _mail.Body = EmailBody(EmailClaimBookedClass.EmailClaimBookedText("", "")); 
       smtp.Send(_mail); 
      } 

    public static string EmailBody(string EmailBody) 
    { 
     int i = 0; 

     StringBuilder EB = new StringBuilder(); 
     EB.Length = 0; 
     EB.AppendLine("<html><head>"); 
     EB.AppendLine(GetHtmlStyle()); 
     EB.AppendLine("</head><body>"); 
     EB.AppendLine("<table class='tksa_table' width='100%' border='0'>"); 
     EB.AppendLine("<tr><td colspan='3'><font color='#000000'>" + EmailBody.ToString() + "</font></td></tr>"); 

     EB.AppendLine("<tr><td colspan='3'>&nbsp;</td></tr>"); 
     EB.AppendLine("</table>"); 
     EB.AppendLine("<table class='tksa_table' width='100%' border='0'>"); 
     EB.AppendLine("<tr><td colspan='3'>&nbsp;</td></tr>"); 
     EB.AppendLine("<tr><td colspan='3' valign='top'>Kind Regards</td></tr>"); 
     EB.AppendLine("<tr><td colspan='3' valign='top'>DD International</td></tr>"); 
     EB.AppendLine("<tr><td colspan='3' valign='top'>Customer Services Department</td></tr>"); 
     EB.AppendLine("<tr><td colspan='3' valign='top'> phone:+ (015) 00000 00</td></tr>"); 
     EB.AppendLine("</table>"); 
     EB.AppendLine("</body></html>"); 

     return EB.ToString(); 
    } 

Html стиль

private static string GetHtmlStyle() 
    { 
     StringBuilder HtmlStyle = new StringBuilder(); 
     HtmlStyle.Length = 0; 
     HtmlStyle.AppendLine("<style type='text/css'>"); 
     HtmlStyle.AppendLine(".tksa_table tr td"); 
     HtmlStyle.AppendLine("{"); 
     HtmlStyle.AppendLine("font-family: Arial, Verdana, MS Sans Serif;"); 
     HtmlStyle.AppendLine("font-size: 13px;"); 
     HtmlStyle.AppendLine("color: black;"); 
     HtmlStyle.AppendLine("}"); 
     HtmlStyle.AppendLine(".tksa_table tr th"); 
     HtmlStyle.AppendLine("{"); 
     HtmlStyle.AppendLine("font-family: Arial, Verdana, MS Sans Serif;"); 
     HtmlStyle.AppendLine("font-size: 13px;"); 
     HtmlStyle.AppendLine("color: black;"); 
     HtmlStyle.AppendLine("font-weight: bold;"); 
     HtmlStyle.AppendLine("}"); 
     HtmlStyle.AppendLine("a:link, a:visited"); 
     HtmlStyle.AppendLine("{"); 
     HtmlStyle.AppendLine("font-size: 13px;"); 
     HtmlStyle.AppendLine("font-family: Arial, Verdana, MS Sans Serif;"); 
     HtmlStyle.AppendLine("}"); 
     HtmlStyle.AppendLine("a:hover"); 
     HtmlStyle.AppendLine("{"); 
     HtmlStyle.AppendLine("font-size: 13px;"); 
     HtmlStyle.AppendLine("font-family: Arial, Verdana, MS Sans Serif;"); 
     HtmlStyle.AppendLine("}"); 
     HtmlStyle.AppendLine("a:active"); 
     HtmlStyle.AppendLine("{"); 
     HtmlStyle.AppendLine("font-size: 13px;"); 
     HtmlStyle.AppendLine("font-family: Arial, Verdana, MS Sans Serif;"); 
     HtmlStyle.AppendLine("}"); 
     HtmlStyle.AppendLine("</style>"); 
     return HtmlStyle.ToString(); 
    } 

результаты я получаю на мой Входящие

<html><head> 
    <style type='text/css'> 
    .tksa_table tr td 
    { 
    font-family: Arial, Verdana, MS Sans Serif; 
    font-size: 13px; 
    color: black; 
} 
.tksa_table tr th 
{ 
    font-family: Arial, Verdana, MS Sans Serif; 
    font-size: 13px; 
    color: black; 
    font-weight: bold; 
    } 
a:link, a:visited 
{ 
font-size: 13px; 
font-family: Arial, Verdana, MS Sans Serif; } a:hover { 
font-size: 13px; 
font-family: Arial, Verdana, MS Sans Serif; } a:active { 
font-size: 13px; 
font-family: Arial, Verdana, MS Sans Serif; } </style> 

</head><body> 
<table class='tksa_table' width='100%' border='0'> <tr>           <td colspan='3'> <font color='#000000'>This is my testing email from my App</font></td></tr> <tr> <td colspan='3'>&nbsp;</td></tr> </table> <table class='tksa_table' width='100%' border='0'> <tr><td colspan='3'>&nbsp;</td></tr> <tr><td colspan='3' valign='top'>Kind Regards</td></tr> <tr><td colspan='3' valign='top'>DD International</td></tr> <tr><td colspan='3' valign='top'>Customer Services Department</td></tr> <tr><td colspan='3' valign='top'> phone:+ (015) 00000 00</td></tr> </table> </body> 
</html> 
+0

Боковой вопрос: Почему вы подстригать 'длину StringBuilder's до 0 перед их использованием? – Filburt

ответ

2

Сообщение, которое вы отправляете является открытым текстом.

Вам необходимо включить флаг HTML:

_mail.IsBodyHtml = true; 
+0

Спасибо, что работает отлично. теперь мой вопрос заключается в том, как я могу добавить приветствие, например (Дорогой пользователь), и, наконец, он немного задерживает получение электронной почты или, может быть, мой прогноз. –

+0

@mavhungukhodani Это должен быть другой вопрос –

Смежные вопросы