【发布时间】:2021-07-09 10:23:01
【问题描述】:
我有使用 mailgun 发送电子邮件的代码:
public async Task SendMail(string replyTo, string to, string cc, string subject, string message)
{
var smtpServer = config[ConfigurationSMTP_Server];
var smtpPort = Convert.ToInt32(config[Configuration_SMTP_Port]);
var smtpUsername = config[Configuration.SMTP_Username];
var smtpPassword = config[Configuration.SMTP_Password];
MimeMessage mail = new MimeMessage();
mail.From.Add(new MailboxAddress(String.Empty, smtpUsername));
mail.ReplyTo.Add(new MailboxAddress(String.Empty, replyTo));
mail.To.Add(new MailboxAddress(String.Empty, to));
mail.Cc.Add(new MailboxAddress(String.Empty, cc));
mail.Subject = subject;
mail.Body = new TextPart("plain")
{
Text = message,
};
using (var client = new SmtpClient())
{
client.Connect(smtpServer, smtpPort, false);
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate(smtpUsername, smtpPassword);
await client.SendAsync(mail);
client.Disconnect(true);
}
}
它可以工作,但 br、p html 标签似乎不适用于邮件。我怎样才能做到这一点?
【问题讨论】:
-
我对 Mailgun 不熟悉,但你确定 TextPart 是正确的吗?您是否应该使用 HTML 属性来制作消息的 html 内容?