使用 mailgun 发送带有 html 标签的电子邮件
Send email with html tags using mailgun
我有使用 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 标签似乎不适用于邮件。我怎样才能做到这一点?
成功了
mail.Body = new TextPart(MimeKit.Text.TextFormat.Html)
{
Text = message,
};
您可以使用 Mailgun 提供的收件人变量和 v: 功能。
例如下面是一个 JSON 字符串 -
r = '{
"foo@boo.com": {"FirstName": "Toby","LastName": "Moon"},
"yoo@too.com": {"FirstName": "Bobby","LastName": "Sun"}
}'
在调用 API 时将其分配给接收者变量。
res = requests.post(
"API url",
auth=("api", "API key"),
data={
"from": "<yourdomain@yourdomain.com">,
"to": ["List of Emails"],
"subject": "Test Email",
"recipient-variables": r,
'v:FirstName':'%recipient.FirstName%',
'v:LastName':'%recipient.LastName%',
})
print(res)
然后您可以在任何地方使用 %recipient.value% 来动态引用它。
以下 link 包含步骤 - Mailgun Recipient Variables
以上是Python中的例子。
我有使用 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 标签似乎不适用于邮件。我怎样才能做到这一点?
成功了
mail.Body = new TextPart(MimeKit.Text.TextFormat.Html)
{
Text = message,
};
您可以使用 Mailgun 提供的收件人变量和 v: 功能。
例如下面是一个 JSON 字符串 -
r = '{
"foo@boo.com": {"FirstName": "Toby","LastName": "Moon"},
"yoo@too.com": {"FirstName": "Bobby","LastName": "Sun"}
}'
在调用 API 时将其分配给接收者变量。
res = requests.post(
"API url",
auth=("api", "API key"),
data={
"from": "<yourdomain@yourdomain.com">,
"to": ["List of Emails"],
"subject": "Test Email",
"recipient-variables": r,
'v:FirstName':'%recipient.FirstName%',
'v:LastName':'%recipient.LastName%',
})
print(res)
然后您可以在任何地方使用 %recipient.value% 来动态引用它。
以下 link 包含步骤 - Mailgun Recipient Variables
以上是Python中的例子。