【问题标题】:Send email with html tags using mailgun使用 mailgun 发送带有 html 标签的电子邮件
【发布时间】: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 内容?

标签: c# email mailgun


【解决方案1】:

这成功了

mail.Body = new TextPart(MimeKit.Text.TextFormat.Html)
{
            Text = message,
};

【讨论】:

    【解决方案2】:

    您可以使用 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% 来动态引用它。

    以下链接包含步骤 - Mailgun Recipient Variables

    以上是 Python 中的示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-12
      • 2015-01-13
      • 2016-08-14
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      相关资源
      最近更新 更多