C# asp.net中通过EmailMessage发送的HTML消息

C# asp.net中通过EmailMessage发送的HTML消息,c#,html,asp.net,email,C#,Html,Asp.net,Email,这个问题应该很简单,但基本上我正在寻找一种使用asp.net中的email message类发送html电子邮件的方法。在我的调试站点上,邮件客户端是一个pop服务器,所以我只使用SmtpClient,并使用MailMessage类添加一个AlternateView发送。live站点使用Exchange服务器,因此我必须添加Exchange凭据并使用EMailMessage类发送,但Email message未定义AlternateView。我在谷歌上很难找到这个答案。以下是我获取HTML页面和

这个问题应该很简单,但基本上我正在寻找一种使用asp.net中的email message类发送html电子邮件的方法。在我的调试站点上,邮件客户端是一个pop服务器,所以我只使用SmtpClient,并使用MailMessage类添加一个AlternateView发送。live站点使用Exchange服务器,因此我必须添加Exchange凭据并使用EMailMessage类发送,但Email message未定义AlternateView。我在谷歌上很难找到这个答案。以下是我获取HTML页面和发送电子邮件的两种方法:

 private void emailUserPass(UserInfo info)
        {
            try
            {
                string body = string.Empty;
                using (StreamReader welcomeEmailReader = new StreamReader(Server.MapPath("~/path")))
                {
                    body = welcomeEmailReader.ReadToEnd();
                }
                body = body.Replace("{ID}",info.ID.ToString());
                body = body.Replace("{Password}", info.Password);
                List<string> to = new List<string>(); to.Add(info.Email);

                SendEMail("New Login", body, to, new List<string>(), new List<string>());
            }
            catch(Exception ex)
            {
                ShowError("Could not send the e-mail - error: " + ex.Message);
            }
        }



        public static void SendEMail(string subject, string body, List<string> to, List<string> cc, List<string> bcc)
        {
            #if DEBUG
                SmtpClient client = new SmtpClient("smtpout.secureserver.net", 25);
                client.Credentials = new System.Net.NetworkCredential("login", "password");
                MailMessage msg = new MailMessage("email", "email");
                msg.Subject = subject;
                body = body.Replace("{To}","To " + string.Join(",", to));
                body = body.Replace("{CC}","CC " + string.Join(",", cc));
                body = body.Replace("{BCC}", "BCC " + string.Join(",", bcc));
                AlternateView plain = AlternateView.CreateAlternateViewFromString(body, null, "text/plain");
                AlternateView normal = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
                msg.AlternateViews.Add(plain);
                msg.AlternateViews.Add(normal);
                client.Send(msg);
#else
            ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
                ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
                ExchangeService exService = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

                exService.UseDefaultCredentials = false;
                exService.Credentials = new NetworkCredential("No_reply_programs", "blah", "blah");
                exService.Url = new Uri("ExchangeURL");

                body = body.Replace("{To}",string.Empty);
                body = body.Replace("{CC}",string.Empty);
                body = body.Replace("{BCC}",string.Empty);

                EmailMessage msg = new EmailMessage(exService);
                AlternateView plain = AlternateView.CreateAlternateViewFromString(body, null, "text/plain");
                AlternateView normal = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
                msg.AlternateViews.Add(plain);
                msg.AlternateViews.Add(normal);
                msg.Subject = subject;
                foreach (string address in to)
                    msg.ToRecipients.Add(address);
                foreach (string address in cc)
                    msg.CcRecipients.Add(address);
                foreach (string address in bcc)
                    msg.BccRecipients.Add(address);
                msg.BccRecipients.Add("BccRecipient");
                msg.Send();
#endif
        }
private void emailUserPass(UserInfo)
{
尝试
{
string body=string.Empty;
使用(StreamReader-welcomeEmailReader=newstreamreader(Server.MapPath(“~/path”))
{
body=welcomeEmailReader.ReadToEnd();
}
body=body.Replace(“{ID}”,info.ID.ToString());
body=body.Replace(“{Password}”,info.Password);
List to=新列表();to.Add(info.Email);
SendEMail(“新登录”,正文,收件人,新列表(),新列表());
}
捕获(例外情况除外)
{
淋浴错误(“无法发送电子邮件-错误:+ex.Message”);
}
}
公共静态无效发送电子邮件(字符串主题、字符串正文、列表收件人、列表抄送、列表密件抄送)
{
#如果调试
SmtpClient=新的SmtpClient(“smtpout.secureserver.net”,25);
client.Credentials=新系统.Net.NetworkCredential(“登录”、“密码”);
MailMessage msg=新的MailMessage(“电子邮件”、“电子邮件”);
msg.Subject=主语;
body=body.Replace(“{To}”,“To”+string.Join(“,”,To));
body=body.Replace(“{CC}”,“CC”+string.Join(“,”,CC));
body=body.Replace(“{BCC}”,“BCC”+string.Join(“,”,BCC));
AlternateView plain=AlternateView.CreateAlternateView-FromString(正文,null,“文本/普通”);
AlternateView normal=AlternateView.createAlternateView-FromString(正文,null,“文本/html”);
msg.AlternateViews.Add(普通);
msg.AlternateViews.Add(正常);
client.Send(msg);
#否则
ServicePointManager.Expect100Continue=true;
ServicePointManager.SecurityProtocol=SecurityProtocolType.Ssl3;
ServicePointManager.ServerCertificateValidationCallback=委托(对象s、X509Certificate证书、X509Chain链、SslPolicyErrors SslPolicyErrors){return true;};
ExchangeService exService=新的ExchangeService(ExchangeVersion.Exchange2007_SP1);
exService.UseDefaultCredentials=false;
exService.Credentials=newnetworkcredential(“无应答程序”、“诸如此类”、“诸如此类”);
Url=newURI(“ExchangeURL”);
body=body.Replace(“{To}”,string.Empty);
body=body.Replace(“{CC}”,string.Empty);
body=body.Replace(“{BCC}”,string.Empty);
EmailMessage msg=新的EmailMessage(exService);
AlternateView plain=AlternateView.CreateAlternateView-FromString(正文,null,“文本/普通”);
AlternateView normal=AlternateView.createAlternateView-FromString(正文,null,“文本/html”);
msg.AlternateViews.Add(普通);
msg.AlternateViews.Add(正常);
msg.Subject=主语;
foreach(到中的字符串地址)
msg.ToRecipients.Add(地址);
foreach(cc中的字符串地址)
msg.CcRecipients.Add(地址);
foreach(密件抄送中的字符串地址)
msg.bccreciients.Add(地址);
msg.bccreciients.Add(“bccreciient”);
msg.Send();
#恩迪夫
}

这将类似于:

EmailMessage msg = new EmailMessage(exService);
//assing body 1st:
msg.Body= "string in Html format with tags and stuff";
msg.Body.BodyType = BodyType.HTML;
...

为什么不使用Exchange的SMTP服务器呢?首先,他们希望它与模块中的其他纯文本电子邮件或其他类来自相同的no_reply_程序。其次,我有代码中列出的Exchange凭据,但没有SMTP密码。我会觉得很奇怪,EmailMessage无法发送html,所以我确信在meAlso的整个生命周期中我都无法找到它。根据intellisense,Body有一个BodyType属性,但MSDN没有关于EmailMessage.Body.BodyType的文档。有人知道这个属性的html的赋值吗只能将msg.Body.BodyType=赋值;msg.Body=Body;找到它时,赋值为HTML,因此msg.Body.BodyType=“HTML”起作用。如果您首先引入HTML,它似乎也会自动分配。