通过亚马逊ses发送html模板

问题描述 投票:0回答:1

我正在使用下面的代码发送计划文本,但它在 html 模板中不起作用..

    static void Main(string[] args)
    {
        String username = "test";  // Replace with your SMTP username.
        String password = "test";  // Replace with your SMTP password.
        String host = "email-smtp.us-east-1.amazonaws.com";
        int port = 25;

        using (var client = new System.Net.Mail.SmtpClient(host, port))
        {
            client.Credentials = new System.Net.NetworkCredential(username, password);
            client.EnableSsl = true;

            client.Send
            (
                      "[email protected]",  // Replace with the sender address.
                      "[email protected]",    // Replace with the recipient address.
                      "Testing Amazon SES through SMTP",
                      "This email was delivered through Amazon SES via the SMTP end point."
            );
        }
c# .net amazon-web-services amazon-ses
1个回答
6
投票

您需要将 AlternateViews 与 .NET SmtpClient 一起使用才能发送 HTML 消息。邮件客户端将呈现它可以支持的适当视图。

这里是一个代码片段,演示了如何使用文本视图和 HTML 视图创建消息,然后通过 Amazon SES 发送消息。

        string host = "email-smtp.us-east-1.amazonaws.com";
        int port = 25;

        var credentials = new NetworkCredential("ses-smtp-username", "ses-smtp-password");
        var sender = new MailAddress("[email protected]", "Message Sender");
        var recipientTo = new MailAddress("[email protected]", "Recipient One");
        var subject = "HTML and TXT views";

        var htmlView = AlternateView.CreateAlternateViewFromString("<p>This message is <code>formatted</code> with <strong>HTML</strong>.</p>", Encoding.UTF8, MediaTypeNames.Text.Html);
        var txtView = AlternateView.CreateAlternateViewFromString("This is a plain text message.", Encoding.UTF8, MediaTypeNames.Text.Plain);

        var message = new MailMessage();
        message.Subject = subject;
        message.From = sender;
        message.To.Add(recipientTo);
        message.AlternateViews.Add(txtView);
        message.AlternateViews.Add(htmlView);

        using (var client = new SmtpClient(host, port))
        {
            client.Credentials = credentials;
            client.EnableSsl = true;

            client.Send(message);
        }
© www.soinside.com 2019 - 2024. All rights reserved.