none
Background color to Mail body RRS feed

  • Question

  • Hi,
    Is there one example to have background color to Mail body, to the way using SmtpMail?

    Many Thanks & Best Regards, Hua Min

    Monday, January 29, 2018 9:39 AM

Answers

  • Hello HuaMin,

    CoolDadTx is correct, which have a right direction to set the mailbody background color. A simple example like below.

      SmtpClient mailClient = new SmtpClient("[hostname]");
      mailClient.Credentials = new NetworkCredential("[username]", "[password]");
    
      MailMessage mailMessage = new MailMessage("[from]", "[to]");
      mailMessage.Body = "<body style=\"background-color:aqua\">a guy</body>";
      mailMessage.IsBodyHtml = true;
    
      mailClient.EnableSsl = true;
      mailClient.Send(mailMessage);

    Result.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jackson_1990 Tuesday, January 30, 2018 12:23 PM
    Tuesday, January 30, 2018 6:31 AM
    Moderator

All replies

  • The only way to do that is to use an HTML message (which not all email clients allow). For that it is raw HTML so you can set the body element's background color using standard styling or CSS rules. If the email client allows HTML (and styling) then it'll color the background accordingly.

    Do not use SmtpMail. It has been deprecated for a while. Use SmtpClient instead. Set the IsBodyHtml property on MailMessage to true to indicate you're sending an HTML email.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, January 29, 2018 3:51 PM
    Moderator
  • You can use AlternateView.CreateAlternateViewFromString

    Dim mail As New MailMessage()
    'mail.From,  mail.To, etc...
    Dim sHtml As String = "<html> <body bgcolor=""#cc33cc""> <font color=""#ffff00"">Test Mail with background color</font></body></html> "
    mail.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(sHtml, Nothing, Mime.MediaTypeNames.Text.Html))
    'then send mail with SmtpClient()

    Monday, January 29, 2018 5:11 PM
  • You can use AlternateView.CreateAlternateViewFromString

    with MediaTypeNames.Text.Html

    • Edited by Castorix31 Monday, January 29, 2018 5:14 PM
    Monday, January 29, 2018 5:13 PM
  • Hello HuaMin,

    CoolDadTx is correct, which have a right direction to set the mailbody background color. A simple example like below.

      SmtpClient mailClient = new SmtpClient("[hostname]");
      mailClient.Credentials = new NetworkCredential("[username]", "[password]");
    
      MailMessage mailMessage = new MailMessage("[from]", "[to]");
      mailMessage.Body = "<body style=\"background-color:aqua\">a guy</body>";
      mailMessage.IsBodyHtml = true;
    
      mailClient.EnableSsl = true;
      mailClient.Send(mailMessage);

    Result.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jackson_1990 Tuesday, January 30, 2018 12:23 PM
    Tuesday, January 30, 2018 6:31 AM
    Moderator