【问题标题】:HTML message through EmailMessage in asp.net在 asp.net 中通过 EmailMessage 的 HTML 消息
【发布时间】:2015-07-16 20:56:10
【问题描述】:

这个问题应该很简单,但基本上我正在寻找一种使用asp.net中的Email消息类发送html电子邮件的方法。在我的调试站点上,邮件客户端是一个弹出服务器,所以我只使用 SmtpClient 并使用 MailMessage 类添加 AlternateView 发送。实时站点使用 Exchange 服务器,因此我必须添加 Exchange 凭据并使用 EMailMessage 类发送,但电子邮件消息没有定义 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
        }

【问题讨论】:

  • 为什么不使用 Exchange 的 SMTP 服务器?
  • 一方面,他们希望它来自与其他纯文本电子邮件模块中的其他类相同的 no_reply_programs。其次,我有代码中列出的 Exchange 凭据,但我没有 SMTP 密码。我会觉得奇怪的是,EmailMessage 无法发送 html,所以我确定有某种方法我一辈子都找不到它
  • 根据智能感知,EmailMessage.Body 也有一个 BodyType 属性,但是 MSDN 没有关于 EmailMessage.Body.BodyType 的文档有没有人知道这个属性的 HTML 赋值 vvalue 所以我可以把 msg.Body .BodyType = 赋值; msg.Body = 身体;
  • 找到了,赋值是 HTML 所以 msg.Body.BodyType = "HTML" 有效。如果您先引入 HTML,它似乎也会自动分配。

标签: c# html asp.net email


【解决方案1】:

这将是这样的:

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

【讨论】:

    猜你喜欢
    • 2016-04-28
    • 2013-07-02
    • 2023-03-31
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    相关资源
    最近更新 更多