Como enviar um e-mail formatado em HTML? [duplicado]
esta pergunta já tem uma resposta aqui:
- enviar um e-mail HTML via C# com o SmtpClient 6 respostas
Eu poderia ser capaz de deixar a aplicação web enviar e-mails automáticos usando o escalonador de Tarefas do Windows. Agora eu quero enviar um e-mail formatado HTML usando o seguinte método que eu escrevi para enviar e-mails.
a minha Código atrás:
protected void Page_Load(object sender, EventArgs e)
{
SmtpClient sc = new SmtpClient("mail address");
MailMessage msg = null;
try
{
msg = new MailMessage("[email protected]",
"[email protected]", "Message from PSSP System",
"This email sent by the PSSP system");
sc.Send(msg);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (msg != null)
{
msg.Dispose();
}
}
}
Como fazer isso? Eu só quero colocar um texto em negrito com um link e talvez uma imagem no e-mail.
3 answers
A configuração isBodyHtml
Para true
permite-lhe usar marcas de HTML no corpo da mensagem:
msg = new MailMessage("[email protected]",
"[email protected]", "Message from PSSP System",
"This email sent by the PSSP system<br />" +
"<b>this is bold text!</b>");
msg.IsBodyHtml = true;
A melhor maneira de enviar um e-mail formatado html
Este código será em " cliente.htm"
<table>
<tr>
<td>
Dealer's Company Name
</td>
<td>
:
</td>
<td>
#DealerCompanyName#
</td>
</tr>
</table>
Leia o ficheiro HTML com o System. IO. File. ReadAllText.obtenha todo o código HTML na variável string.
string Body = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("EmailTemplates/Customer.htm"));
Substitua o texto Em Particular pelo seu valor personalizado.
Body = Body.Replace("#DealerCompanyName#", _lstGetDealerRoleAndContactInfoByCompanyIDResult[0].CompanyName);
Ligue para a função SendEmail (string Body) e faça o procedimento para enviar e-mail.
public static void SendEmail(string Body)
{
MailMessage message = new MailMessage();
message.From = new MailAddress(Session["Email"].Tostring());
message.To.Add(ConfigurationSettings.AppSettings["RequesEmail"].ToString());
message.Subject = "Request from " + SessionFactory.CurrentCompany.CompanyName + " to add a new supplier";
message.IsBodyHtml = true;
message.Body = Body;
SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = true;
smtpClient.Host = ConfigurationSettings.AppSettings["SMTP"].ToString();
smtpClient.Port = Convert.ToInt32(ConfigurationSettings.AppSettings["PORT"].ToString());
smtpClient.EnableSsl = true;
smtpClient.Credentials = new System.Net.NetworkCredential(ConfigurationSettings.AppSettings["USERNAME"].ToString(), ConfigurationSettings.AppSettings["PASSWORD"].ToString());
smtpClient.Send(message);
}
msg.BodyFormat = MailFormat.Html;
E depois pode usar html no seu corpo
msg.Body = "<em>It's great to use HTML in mail!!</em>"