使用C#中的SmtpClient发送电子邮件时出现问题

时间:2009-09-21 20:57:28

标签: c# asp.net-mvc

我有一个ASP.Net/MVC应用程序,我正在尝试发送HTML电子邮件。我这样做是通过读取带有令牌的HTML文件,然后替换令牌。那部分很好,生成的HTML正是我想要的,但是当我发送电子邮件时,我收到的内容看起来像 -

<style type=3D"text/css">=
=0D=0A.styleTitles=0D=0A{=0D=0Afont-weight:=bold;=0D=0A}=0D=0A 
.style1=0D=0A        {=0D=0A 

应该看起来像

    <style type="text/css">
    .styleTitles
    {
        font-weight: bold;
    }
    .style1
    {
        height: 15px;
    }

我在网上看了一下,似乎无法找到发送邮件的正确语法。我见过一些解决方案,但似乎都没有。

我目前的测试代码是 -

SmtpClient smtpclient = new SmtpClient();
MailMessage message = new MailMessage();

MailAddress SendFrom = new MailAddress("xxxx@abc.com");
MailAddress SendTo = new MailAddress("zzzz@gmail.com");
MailMessage MyMessage = new MailMessage(SendFrom, SendTo);

var plainView = AlternateView.CreateAlternateViewFromString(msgBody,null,"text/html");
plainView.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;
MyMessage.AlternateViews.Add(plainView);
MyMessage.IsBodyHtml = true;
MyMessage.Subject = subjectLine;
MyMessage.Body = msgBody;
smtpclient.Send(MyMessage);

任何建议?

6 个答案:

答案 0 :(得分:6)

也许是这样的:

var plainView = AlternateView.CreateAlternateViewFromString(msgBody, new ContentType("text/plain; charset=UTF-8"));

MyMessage.AlternateViews.Add(plainView);
MyMessage.BodyEncoding = Encoding.UTF8;
MyMessage.IsBodyHtml = true;
MyMessage.Subject = subjectLine;
MyMessage.Body = msgBody;

答案 1 :(得分:3)

尝试此更改:

plainView.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

答案 2 :(得分:3)

要将传输编码设置为8位,取自here,您必须:

message.Body = null;
using (AlternateView body =
AlternateView.CreateAlternateViewFromString(
    "Some Message Body",
    message.BodyEncoding,
    message.IsBodyHtml ? "text/html" : null))
{
body.TransferEncoding = 
    TransferEncoding.SevenBit;
message.AlternateViews.Add(body);
}

答案 3 :(得分:0)

这可能不是您需要的答案,但您是否考虑过使用XSLT翻译电子邮件?我正忙于发送电子邮件的项目,并且很高兴使用XSLT作为解决方案的一部分。还意味着将来模板可以很容易地以行业标准化的方式进行定制,也许您应该考虑进行更改?

答案 4 :(得分:0)

这很奇怪,但更简单的代码对我有用:

var message = new MailMessage(Email, mailTo);
message.IsBodyHtml = true;
message.SubjectEncoding = message.BodyEncoding = Encoding.UTF8;
message.Subject = "Subject";
message.Body = msgBody;
smtpclient.Send(message);

答案 5 :(得分:-1)

string emailMessage="a skjdhak kdkand"; 
MailMessage mail = new MailMessage();
                    mail.To.Add(obj_Artist.EmailAddress);
                    mail.From = new MailAddress(EmailList[0].FromEmail, "Sentric Music - Rights Management");
                       mail.Subject = (EmailList[0].Subject);

                    if (EmailList[0].BCC1 != null && EmailList[0].BCC1 != string.Empty)
                    {
                        mail.Bcc.Add(EmailList[0].BCC1);
                    }
                    if (EmailList[0].BCC2 != null && EmailList[0].BCC2 != string.Empty)
                    {
                        mail.Bcc.Add(EmailList[0].BCC2);
                    }
                    if (EmailList[0].CC1 != null && EmailList[0].CC1 != string.Empty)
                    {
                        mail.CC.Add(EmailList[0].CC1);
                    }
                    if (EmailList[0].CC2 != null && EmailList[0].CC2 != string.Empty)
                    {
                        mail.CC.Add(EmailList[0].CC2);`enter code here`
                    }


                    string Body = emailMessage;


                    mail.Body = Body;
                    mail.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
                    mail.IsBodyHtml = true;
                    AlternateView plainView = AlternateView.CreateAlternateViewFromString
                    (System.Text.RegularExpressions.Regex.Replace(Body, @"<(.|\n)*?>", string.Empty), null, "text/plain");
                    System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(Body, null, "text/html");
                    mail.AlternateViews.Add(plainView);
                    mail.AlternateViews.Add(htmlView);
                    SmtpClient smtp = new SmtpClient();
                    smtp.EnableSsl = true;
                    smtp.Send(mail);