.NET發送郵件

1、使用System.Web.Mail命名空間發郵件服務器

      在.Net Framework 1.x 咱們須要使用 System.Web.Mail 命名空間下的類來進行發送郵件,可是功能比較弱,好比你的郵件服務器須要驗證才能發送郵件。當我用VS2008寫以下代碼時,會出現「System.Web.Mail.MailMessage 」過期的提示。
private void SendMail()
{
            System.Web.Mail.MailMessage mailMsg = new System.Web.Mail.MailMessage();
            mailMsg.From = "**@**"; //個人郵箱地址
            mailMsg.To = 「**@**」; //對方的郵箱地址
            mailMsg.Subject = "主題";
            mailMsg.BodyFormat = System.Web.Mail.MailFormat.Text;
            mailMsg.Body = "內容「;
            try
            {
                System.Web.Mail.SmtpMail.Send(mailMsg);
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }ide

}orm

2、使用System.Net.Mail 命名空間blog

.Net Framework 2.0 下,在 System.Net.Mail 命名空間中提供了對郵件操做的支持,他的功能更強大。好比你的郵件服務器須要驗證才能發送郵件。get

using System.Net.Mail;string

private void SendMail(string host)
{
        MailAddress from = new MailAddress("**@**"); //個人郵箱地址
        MailAddress to = new MailAddress("**@**");//對方的郵箱地址
        MailMessage msg = new MailMessage(from, to);
        msg.Subject = "主題";
        msg.Body = "內容";
        SmtpClient client = new SmtpClient(host);it

        try
            {
                client.Send(msg);io

            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }class

}cli

相關文章
相關標籤/搜索