Reputation:
I have this aspx function which send email. Unfortunetely, it does not render html tag on gmail or outlook, but on my android phone looks fine. How to fix it? thanks.
protected void SendMail()
{
var fromAddress = "[email protected]";
var toAddress = "[email protected]";
const string fromPassword = "****";
var subject = "subject";
string body = " <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\">";
body += "<HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">";
body += "</HEAD><BODY>";
body += "<b>some text some text</b><br></br>";
body += "</BODY></HTML>";
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
smtp.Send(fromAddress, toAddress, subject, body);
}
EDITED: So here is what i have now. But the compiler gives this error: Line 392: mail.From = fromAddress; Compiler Error Message: CS0029: Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAddress'
var fromAddress = "device@***";
var toAddress = "*****@gmail.com";
const string fromPassword = "******";
var subject = "New Project Started!";
string body = "<b>TA:</b>";
MailMessage mail= new MailMessage();
mail.From = fromAddress;
mail.To = toAddress;
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = body;
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
smtp.Send(mail);
EDITED: My working solution:
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
var fromAddress = "****"; // mail Address from where you send the mail
var toAddress = "****";
const string fromPassword = "***"; //Password of your gmail address
var subject = "New Job Created!";
// set smtp-client with basicAuthentication
SmtpClient mySmtpClient = new SmtpClient("smtp.gmail.com");
mySmtpClient.Port = 587;
mySmtpClient.EnableSsl = true;
mySmtpClient.UseDefaultCredentials = false;
System.Net.NetworkCredential basicAuthenticationInfo = new
System.Net.NetworkCredential(fromAddress, fromPassword);
mySmtpClient.Credentials = basicAuthenticationInfo;
// add from,to mailaddresses
MailAddress from = new MailAddress(fromAddress, "");
MailAddress to = new MailAddress(toAddress, "Devision");
MailMessage myMail = new System.Net.Mail.MailMessage(from, to);
// set subject and encoding
myMail.Subject = subject;
myMail.SubjectEncoding = System.Text.Encoding.UTF8;
// set body-message and encoding
myMail.Body = "<b>TEST</b>";
myMail.BodyEncoding = System.Text.Encoding.UTF8;
// text or html
myMail.IsBodyHtml = true;
mySmtpClient.Send(myMail);
Upvotes: 0
Views: 82
Reputation: 18127
Create MailMessage
Class. The key thing is to specify that the Body of the is HTML this is done with property IsBodyHtml
MailMessage mail= new MailMessage();
mail.From = fromAddress;
mail.To = toAddress;
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = body;
//other stuff
smtp.Send(mail);
Upvotes: 1