【问题标题】:How to send a HTML Email from a asp.net Website如何从 asp.net 网站发送 HTML 电子邮件
【发布时间】:2011-12-22 04:48:30
【问题描述】:

在我的 Asp.net 网站中,我创建了新用户,我要做的就是从网站向用户发送电子邮件以及他们的登录详细信息。我创建了一个网络服务来发送电子邮件,它工作正常。我想在发送给用户的电子邮件中添加一些 HTML 样式。

所以我创建了一个 HTML 电子邮件格式,并将其作为 HTML 页面保存在项目内部的文件夹(资源)中。如何从资源文件夹中读取 HTML 页面并将其与“BODY”参数中的以下代码一起附加并将其发送到 webservices 方法。还有如何编辑 html 文件,以便在将其发送到 web 服务之前将用户登录详细信息包含在其中。谢谢

将值传递给 webservice 方法以发送电子邮件的 C# 代码

       string subject = "login details";
       //call the webservice to send email to the newly created user
       ServiceClient service = new ServiceClient();
       service.sendEmail(newuseremail, subject, BODY, message, myemail);

【问题讨论】:

标签: c# asp.net html smtp html-email


【解决方案1】:

对于如何阅读 html 页面 - 没有什么特别的,您必须将其作为一个简单的文本文件来处理,并使用 StreamReader 进行阅读。 为了能够对内容进行一些更改,请考虑使用一些可能不会出现在其他地方的 html 页面中的模式,例如:“$$USER$$”,并在读取文件后,将此类出现替换为用户名,或者任何你希望它被替换的东西。 为了能够从服务发送 html 电子邮件,您必须将 MailMessage 实例的属性(如果没有记错 IsBodyHtml)设置为 true。

仅此而已。

【讨论】:

    【解决方案2】:

    这是一个包含你需要的一切的类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Mail;
    
    /// <summary>
    /// Wrapper class for the System.Net.Mail objects
    /// </summary>
    public class SmtpMailMessage : IDisposable
    {
        #region declarations
    
        MailMessage Message;
        SmtpClient SmtpMailClient;
    
        #endregion
    
        #region constructors
    
        /// <summary>
        /// Default constructor for the SmtpMailMessage class
        /// </summary>
        public SmtpMailMessage()
        {
            //initialize the mail message
            Message = new MailMessage();
            Message.Priority = MailPriority.Normal;
            Message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;            
            Message.From = new MailAddress("xxx@abc.com");           
    
            //initialize the smtp client
            SmtpMailClient = new SmtpClient();
            SmtpMailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            SmtpMailClient.Host = "192.168.0.1";
            SmtpMailClient.Port = 25;
        }
    
        /// <summary>
        /// Parameterized constructor for the SmtpMailMessage class. Allows for override of the default
        /// SMTP host and port number
        /// </summary>
        /// <param name="HostIP">The IP address of the exchange server</param>
        /// <param name="PortNumber">The port number for ingoing and outgoing SMTP messages</param>
        public SmtpMailMessage(string HostIP, int PortNumber) : this()
        {
            //override the smtp host value
            SmtpMailClient.Host = HostIP;
    
            //override the smtp port value
            SmtpMailClient.Port = PortNumber;
        }
    
        #endregion
    
        #region subject / body
    
        /// <summary>
        /// The body content of the mail message
        /// </summary>
        public string Body
        {
            get
            {
                return Message.Body;
            }
            set
            {
                Message.Body = value;
            }
        }
    
        /// <summary>
        /// the subject of the mail message
        /// </summary>
        public string Subject
        {
            get
            {
                return Message.Subject;
            }
            set
            {
                Message.Subject = value;
            }
        }
    
        #endregion
    
        #region mail type
    
        /// <summary>
        /// Gets or sets a value that determines whether the mail message
        /// should be formatted as HTML or text
        /// </summary>
        public bool IsHtmlMessage
        {
            get
            {
                return Message.IsBodyHtml;
            }
            set
            {
                Message.IsBodyHtml = value;
            }
        }
    
        #endregion
    
        #region sender
    
        /// <summary>
        /// Gets or sets the from address of this message
        /// </summary>
        public string From
        {
            get
            {
                return Message.From.Address;
            }
            set
            {
                Message.From = new MailAddress(value);
            }
        }
    
        #endregion
    
        #region recipients
    
        /// <summary>
        /// Gets the collection of recipients
        /// </summary>
        public MailAddressCollection To
        {
            get
            {
                return Message.To;
    
            }
        }
    
        /// <summary>
        /// Gets the collection of CC recipients 
        /// </summary>
        public MailAddressCollection CC
        {
            get
            {
                return Message.CC;
            }
        }
    
        /// <summary>
        /// Gets the collection of Bcc recipients
        /// </summary>
        public MailAddressCollection Bcc
        {
            get
            {
                return Message.Bcc;
            }
        }
    
        #endregion
    
        #region delivery notification
    
        /// <summary>
        /// Gets or sets the delivery notification settings for this message
        /// </summary>
        public DeliveryNotificationOptions DeliveryNotifications
        {
            get
            {
                return Message.DeliveryNotificationOptions;
            }
            set
            {
                Message.DeliveryNotificationOptions = value;
            }
        }
    
        #endregion
    
        #region priority
    
        /// <summary>
        /// Gets or sets the Priority of this message
        /// </summary>
        public MailPriority PriorityLevel
        {
            get
            {
                return Message.Priority;
            }
            set
            {
                Message.Priority = value;
            }
        }
    
        #endregion
    
        #region send methods
    
        /// <summary>
        /// Sends the message anonymously (without credentials)
        /// </summary>
        public void Send()
        {
            SmtpMailClient.Send(Message);
        }
    
        /// <summary>
        /// Sends the message with authorization from a network account   
        /// </summary>
        /// <param name="Username">The Windows username of the authorizing user</param>
        /// <param name="Password">The Windows password of the authorizing user</param>
        /// <param name="Domain">The domain name of the network to which the authorizing user belongs</param>
        public void Send(string Username, string Password, string Domain)
        {
            //attach a network credential to this message using the information passed into the method
            SmtpMailClient.Credentials = new NetworkCredential(Username, Password, Domain);
    
            //send the message
            SmtpMailClient.Send(Message);
        }
    
        #endregion
    
        #region IDisposable implementation
    
        ~SmtpMailMessage()
        {
            Dispose(false);
        }
    
        public void Dispose()
        {
            Dispose(true);            
        }
    
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (Message != null)
                    Message.Dispose();
                Message = null;                
                SmtpMailClient = null;
            }
        }
    
        #endregion        
    }
    

    【讨论】:

      【解决方案3】:

      您希望将 HTML 文件作为字符串读取。我假设您可以在本地访问它。

      string strHTML=File.ReadAllText("myfile.html");
      

      这也是重复的:Send a email with a HTML file as body (C#)

      【讨论】:

        【解决方案4】:
        string BODY = String.Empty;
        string PageName = "[Path to resource]"; //example C:\DataSource\Website\DomainName\RousourceFolder\page.html
        
        BODY = new StreamReader(PageName).ReadToEnd();
        

        【讨论】:

          猜你喜欢
          • 2012-07-09
          • 1970-01-01
          • 1970-01-01
          • 2013-09-08
          • 1970-01-01
          • 2014-05-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多