这个问题应该非常简单,但基本上我正在寻找一种使用asp.net中的电子邮件消息类发送html电子邮件的方法。在我的调试站点上,邮件客户端是一个pop服务器,因此我只使用SmtpClient并使用MailMessage类添加AlternateView发送。实时站点使用Exchange服务器,因此我必须添加Exchange凭据并使用EMailMessage类发送,但电子邮件消息未定义AlternateView。我在Google上找到这个答案的时候非常困难。这是我获取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
}
答案 0 :(得分:0)
这将是:
EmailMessage msg = new EmailMessage(exService);
//assing body 1st:
msg.Body= "string in Html format with tags and stuff";
msg.Body.BodyType = BodyType.HTML;
...