gpt4 book ai didi

c# - 通过 amazon ses 发送 html 模板

转载 作者:太空宇宙 更新时间:2023-11-03 19:14:37 26 4
gpt4 key购买 nike

我正在使用下面的代码发送计划文本,但它在 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
(
"sales@imagedb.com", // Replace with the sender address.
"rohit@imagedb.com", // Replace with the recipient address.
"Testing Amazon SES through SMTP",
"This email was delivered through Amazon SES via the SMTP end point."
);
}

最佳答案

您需要使用 AlternateView使用 .NET SmtpClient 发送 HTML 消息。邮件客户端将呈现它可以支持的适当 View 。

下面的代码片段演示了如何使用 TextView 和 HTML View 创建消息,然后通过 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("sender@example.com", "Message Sender");
var recipientTo = new MailAddress("recipient+one@example.com", "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);
}

关于c# - 通过 amazon ses 发送 html 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17966553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com