簡體   English   中英

如何將內容類型設置為html和純文本

[英]How to set content type to html and plain text

通過AWS使用以下代碼發送的郵件為普通文本格式。郵件格式應為html格式,其中應包含設置內容類型的選項。嘗試過谷歌搜索但未找到答案。

using System;

namespace AmazonSESSample
{
    class Program
    {
        static void Main(string[] args)
        {
            const String FROM = "SENDER@EXAMPLE.COM";   // Replace with your "From" address. This address must be verified.
            const String TO = "RECIPIENT@EXAMPLE.COM";  // Replace with a "To" address. If your account is still in the
                                                        // sandbox, this address must be verified.

            const String SUBJECT = "Amazon SES test (SMTP interface accessed using C#)";
            const String BODY = "This email was sent through the Amazon SES SMTP interface by using C#.";

            // Supply your SMTP credentials below. Note that your SMTP credentials are different from your AWS credentials.
            const String SMTP_USERNAME = "YOUR_SMTP_USERNAME";  // Replace with your SMTP username. 
            const String SMTP_PASSWORD = "YOUR_SMTP_PASSWORD";  // Replace with your SMTP password.

            // Amazon SES SMTP host name. This example uses the us-west-2 region.
            const String HOST = "email-smtp.us-west-2.amazonaws.com";

            // Port we will connect to on the Amazon SES SMTP endpoint. We are choosing port 587 because we will use
            // STARTTLS to encrypt the connection.
            const int PORT = 587;

            // Create an SMTP client with the specified host name and port.
            using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT))
            {
                // Create a network credential with your SMTP user name and password.
                client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);

                // Use SSL when accessing Amazon SES. The SMTP session will begin on an unencrypted connection, and then 
                // the client will issue a STARTTLS command to upgrade to an encrypted connection using SSL.
                client.EnableSsl = true;

                // Send the email. 
                try
                {
                    Console.WriteLine("Attempting to send an email through the Amazon SES SMTP interface...");
                    client.Send(FROM, TO, SUBJECT, BODY);
                    Console.WriteLine("Email sent!");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("The email was not sent.");
                    Console.WriteLine("Error message: " + ex.Message);
                }
            }

            Console.Write("Press any key to continue...");
            Console.ReadKey();
        }
    }
}

SmtpClient接受MailMessage類型的參數,該參數相對靈活一些。

像這樣的東西應該可以工作(我還沒有實際測試過,所以如果不能通過復制+粘貼操作,我深表歉意):

using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT)) 
{
    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("fromfield@example.com",
                                          "tofield@example.com",
                                          "subject",
                                          "<i><strong>body</strong></i>");
    message.IsBodyHtml = true;
    client.Send(message);
}

希望能幫助到你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM