Tuesday, April 5, 2011

send smtp mail including html to gmail account

Hi all,

I've found this small code that sends email to gmail users. I'd like the body of the mail to contain html (for example, decoding a link for it to hold different text than the url it's pointing to).

I am using c# .net 3.5. I've used these classes in my code:

  • MailMessage
  • SmtpClient

How can this be done?

Here's a copy of my code:

            MailMessage message = new MailMessage("me@gmail.com", WebCommon.UserEmail, "Test", context.Server.HtmlEncode("<html> <body> <a href='www.cnn.com'> test </a> </body> </html> "));
            System.Net.NetworkCredential cred = new System.Net.NetworkCredential("him@gmail.com", "myPwd");
            message.IsBodyHtml = true;

            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");

            smtp.UseDefaultCredentials = false;
            smtp.EnableSsl = true;
            smtp.Credentials = cred;
            smtp.Port = 587;

            smtp.Send(message);

Thanks!

From stackoverflow
  • Something like this should work:

    Note that MailMessage refers to System.Net.MailMessage. There is also System.Web.MailMessage, which I have never used and -as far as I know- is obsolete.

    MailMessage message = new MailMessage();
    // Very basic html. HTML should always be valid, otherwise you go to spam
    message.Body = "<html><body><p>test</p></body></html>"; 
    // QuotedPrintable encoding is the default, but will often lead to trouble, 
    // so you should set something meaningful here. Could also be ASCII or some ISO
    message.BodyEncoding = Encoding.UTF8;
    message.IsBodyHtml = true;
    // No Subject usually goes to spam, too
    message.Subject = "Some Subject";
    // Note that you can add multiple recipients, bcc, cc rec., etc. Using the 
    // address-only syntax, i.e. w/o a readable name saves you from some issues
    message.To.Add("someone@gmail.com");
    
    // SmtpHost, -Port, -User, -Password must be a valid account you can use to 
    // send messages. Note that it is very often required that the account you
    // use also has the specified sender address associated! 
    // If you configure the Smtp yourself, you can change that of course
    SmtpClient client = new SmtpClient(SmtpHost, SmtpPort) {
                Credentials = new NetworkCredential(SmtpUser, SmtpPassword),
                EnableSsl = enableSsl;
            };
    
            try {
                // It might be necessary to enforce a specific sender address, see above
                if (!string.IsNullOrEmpty(ForceSenderAddress)) {
                    message.From = new MailAddress(ForceSenderAddress);
                }
                client.Send(message);
            }
            catch (Exception ex) {
                return false;
            }
    

    For more sophisticated templating solutions that render the Body html rather than hard-codin it, there is, for example, the EMailTemplateService in MvcContrib which you can use as a guideline.

    vondip : ok, I am not sure why my code didn't work before. I copied your code here and it works great, thank you!
  • One way to do it is to create an alternate html view of the email:

    MailMessage message = new MailMessage();
    message.Body = //plain-text version of message
    //set up message...
    
    //create html view
    string htmlBody = "<html>...</html>";
    htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html");
    message.AlternateViews.Add(htmlView);
    
    //send message
    smtpClient.Send(message);
    

0 comments:

Post a Comment