Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send smtp mail including html to gmail account

Tags:

c#

.net

gmail

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("[email protected]", 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("[email protected]", "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!

like image 856
vondip Avatar asked Mar 13 '10 18:03

vondip


1 Answers

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("[email protected]");

// 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.

like image 172
mnemosyn Avatar answered Oct 08 '22 13:10

mnemosyn