Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SMTP客户端未转换内联HTML_C#_.net_Smtpclient - Fatal编程技术网

C# SMTP客户端未转换内联HTML

C# SMTP客户端未转换内联HTML,c#,.net,smtpclient,C#,.net,Smtpclient,我在使用SMTP/.NET邮件将内嵌HTML发送到电子邮件时遇到问题。我尝试过标准类和MailDefinition类,但我一直在内联原始代码,而不是翻译的页面 我知道我可能遗漏了一些小东西,下面是我的代码。谢谢 public static void SendMail(string toAddress, string subject, string messageBody) { MailDefinition message = new MailDefinitio

我在使用SMTP/.NET邮件将内嵌HTML发送到电子邮件时遇到问题。我尝试过标准类和MailDefinition类,但我一直在内联原始代码,而不是翻译的页面

我知道我可能遗漏了一些小东西,下面是我的代码。谢谢

     public static void SendMail(string toAddress,  string subject, string messageBody)
    {
        MailDefinition message = new MailDefinition();
        message.CC = MailCCAddress;
        message.From = "orders@test.com";
        message.Subject = subject;
        message.IsBodyHtml = true;
        ListDictionary replacements = new ListDictionary();

        System.Net.Mail.MailMessage fileMsg;
        fileMsg = message.CreateMailMessage(toAddress, replacements, messageBody, new System.Web.UI.Control());

        string _hostName = HostName;

        SmtpClient client = new SmtpClient(_hostName);
        client.Credentials = CredentialCache.DefaultNetworkCredentials;
        //client.Send(message);
        client.Send(fileMsg);
    }

如果这不起作用,那么可能是messageBody有问题。您可以尝试调试代码,看看messageBody是否不是正确的html,或者是否有一些字符被转义。

如果仍然有问题,您可能还希望设置
MailMessage
正文编码。比如说,

//Nick's code
MailMessage mail = new MailMessage("orders@test.com", toAddress, subject, messageBody);
mail.IsBodyHtml = true;

//set encoding
mail.BodyEncoding = Encoding.UTF8; //or SevenBit, etc, whatever is appropriate.

//send
SmtpClient client = new SmtpClient(_hostName);
client.Send(mail);
您还可以使用AlternateViews集合,它可以更好地控制内容的MIME类型(因此您可以将其指定为text/html等)。您还可以使用它来选择性地包括纯文本版本和HTMLversion,如下所示:

//create the mail message
MailMessage mail = new MailMessage("orders@test.com", toAddress) { Subject = subject };

//first we create the Plain Text part
AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don't support html", null, "text/plain");
//then we create the Html part
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<b>this is bold text, and viewable by those mail clients that support html</b>", null, "text/html");

//add both views
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);

//send the message as before
//创建邮件消息
MailMessage mail=新的MailMessage(“orders@test.com“,toAddress){Subject=Subject};
//首先,我们创建纯文本部分
AlternateView plainView=AlternateView.CreateAlternateView-FromString(“这是我的纯文本内容,可由不支持html的客户端查看”,null,“text/plain”);
//然后我们创建Html部分
AlternateView htmlView=AlternateView.createAlternateView-FromString(“这是粗体文本,可由支持html的邮件客户端查看”,null,“text/html”);
//添加两个视图
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);
//像以前一样发送消息
//create the mail message
MailMessage mail = new MailMessage("orders@test.com", toAddress) { Subject = subject };

//first we create the Plain Text part
AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don't support html", null, "text/plain");
//then we create the Html part
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<b>this is bold text, and viewable by those mail clients that support html</b>", null, "text/html");

//add both views
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);

//send the message as before