Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys ,I have a trouble in my application,

it is a feature to send email notification ,
i use smtp email ,
i have try to send email into a gmail ,and it was successfully ,
and the layout is well
,but when it send into outlook ,the layout is broken

this is my code

What I have tried:

public static void Email(string htmlString,string Email)
{
    try
    {
        MailMessage message = new MailMessage();
        SmtpClient smtp = new SmtpClient();
        message.From = new MailAddress("email@gmail.com");
        message.To.Add(new MailAddress(Email));
        message.Subject = "Assign Dokumen";
        message.IsBodyHtml = true; //to make message body as html
        message.Body = htmlString;
        smtp.Port = 587;
        smtp.Host = "smtp.gmail.com"; //for gmail host
        smtp.EnableSsl = true;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new NetworkCredential("email@gmailcom", "pass");
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.Send(message);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message.ToString());
    }
}



protected string ExportDatatableToHtml(DataTable dt)
{
    StringBuilder strHTMLBuilder = new StringBuilder();
    strHTMLBuilder.Append("<html >");
    strHTMLBuilder.Append("<head>");
    strHTMLBuilder.Append("</head>");
    strHTMLBuilder.Append("<body>");
    strHTMLBuilder.Append("<table style='background-color: #B8DBFD; solid #ccc' border='2px' cellpadding='1' cellspacing='1' bgcolor='lightyellow' style='font-family:Garamond; font-size:smaller'>");
    strHTMLBuilder.Append("<tr style='font-style:bold;' >");
    foreach (DataColumn myColumn in dt.Columns)
    {
        strHTMLBuilder.Append("<td >");
        strHTMLBuilder.Append(myColumn.ColumnName);
        strHTMLBuilder.Append("</td>");
    }
    strHTMLBuilder.Append("</tr>");
    strHTMLBuilder.Append("</th>");
   strHTMLBuilder.Append("<tbody>");
    foreach (DataRow myRow in dt.Rows)
    {
        strHTMLBuilder.Append("<tr >");
        foreach (DataColumn myColumn in dt.Columns)
        {
            strHTMLBuilder.Append("<td >");
            strHTMLBuilder.Append(myRow[myColumn.ColumnName].ToString());
            strHTMLBuilder.Append("</td>");
        }
        strHTMLBuilder.Append("</tr>");
        strHTMLBuilder.Append("</tbody>");
    }
    //Close tags.
    strHTMLBuilder.Append("</table>");
    strHTMLBuilder.Append("</body>");
    strHTMLBuilder.Append("</html>");
    string Htmltext = strHTMLBuilder.ToString();
    return Htmltext;
}
Posted
Updated 23-Jan-22 7:15am

hi,
change your port 587 into 25 ,it will fail then change the enablessl = false.It will work
 
Share this answer
 
Try adding the styling you want to each TD tag.

Outlook has styling for each element defined so TR styling is not inherited as you would expect.

If you load the email in Outlook via a browser, you can inspect those styles which are not an exact match to the Outlook fat client, but it will give you an idea of what is happening.

Also… in your TABLE tag, you are mixing old and new styling. Convert all of the old style attributes to be part of the style attribute. Border, padding, etc are all deprecated.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900