asp.net發郵件的幾種方法小結

來源:互聯網
上載者:User


MailMessage

提供屬性和方法來建立一個郵件訊息對象。通常可以先構建好MailMessage對象,然後設定它的屬性的方式來構建郵件程式。

 

常用的屬性:

From -- 發送郵件的地址
To -- 接受郵件的地址
Subject -- 郵件的標題
Priority -- 郵件的優先順序(有效值為High,Low,Normal)
Attachments -- 返回一個集合,代表附件
Bcc -- 密送地址
Cc -- 抄送地址
Body -- 擷取或是設定電子郵件訊息的內容
BodyFormat -- 擷取或是設定MailFormat的枚舉值,此值指定訊息體郵件的格式(Html格式、Text格式)
Bodyencoding -- 指定訊息的編碼方式編碼(主要有Base64,UUencode)
UrlContentBase :在HTML格式郵件中的URL編碼方式
UrlContentLocation:郵件資訊的優先順序(High, Medium,Low)

 

SmtpMail

負責發送郵件的SMTP協議,提供屬性和方法通過使用windows 2000 CDOSYS 的訊息組件的聯合資料對象來發送郵件訊息。

SmtpMail類用到的是Send方法,它的目的就是發送郵件,有兩個重載方法。

 

1. SmtpMail.Send("發送郵件的地址","接受郵件的地址","郵件的標題","郵件訊息的內容") 這個方法很簡單,不適合發送帶附件的郵件。


2. SmtpMail.Send(MailMessage) 此方法複雜、靈活,適合發送附件,而且可以設定MailMessage對象的各種屬性值。 如果我們用ASP.NET寫一個郵件發送的程式,那麼首先應該如何得到SMTP。有兩種方法:第一種方法調用目前知名的郵件服務提供者的SMTP,比如新浪、搜狐、網易的免費電子郵箱的SMTP;第二種方法是自己裝一個SMTP虛擬伺服器,這個在安裝IIS時一起裝上去的。

 

MailAttachment

與郵件附件有關的對象類,主要用來提供屬性和方法來建立一個郵件附件對象。

建構函式

建立一個附件對象
MailAttachment  objMailAttachment = new MailAttachment( "d:\test。txt" );//發送郵件的附件

調用形式

 代碼如下 複製代碼

MailMessage objMailMessage= new MailMessage();
objMailMessage.Attachments.Add( objMailAttachment );//將附件附加到郵件訊息對象中


封裝的郵件發送類

 
C# 代碼   複製

 代碼如下 複製代碼

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;
using System.Text;


public class SendMail
...{
    public SendMail()
    ...{

    }

    private string _host;

    /**//// <summary>
    /// 伺服器位址
    /// </summary>
    public string Host
    ...{
        get ...{ return _host; }
        set ...{ _host = value; }
    }

    private int _port;

    /**//// <summary>
    /// 伺服器連接埠
    /// </summary>
    public int Port
    ...{
        get ...{ return _port; }
        set ...{ _port = value; }
    }

    private string _smtpUsername;

    /**//// <summary>
    /// 發送人信箱使用者名
    /// </summary>
    public string SmtpUsername
    ...{
        get ...{ return _smtpUsername; }
        set ...{ _smtpUsername = value; }
    }

    private string _sendemail;

    /**//// <summary>
    /// 發送人郵箱帳號(只接收加密串,發郵件時解密)
    /// </summary>
    public string SendEmail
    ...{
        get
        ...{
            return _sendemail;
        }
        set ...{ _sendemail = value; }
    }

    private string _replyToEmail;
    /**//// <summary>
    /// 回複人郵箱帳號
    /// </summary>
    public string ReplyToEmail
    ...{
        get ...{ return _replyToEmail; }
        set ...{ _replyToEmail = value; }
    }

    private string _replyUserName;
    /**//// <summary>
    /// 回複人使用者名稱
    /// </summary>
    public string ReplyUserName
    ...{
        get ...{ return _replyUserName; }
        set ...{ _replyUserName = value; }
    }

    private string _getemail;

    /**//// <summary>
    /// 收件者郵箱帳號
    /// </summary>
    public string GetEmail
    ...{
        get ...{ return _getemail; }
        set ...{ _getemail = value; }
    }

    private string _smtpPassword;
    /**//// <summary>
    /// 發送人郵箱密碼(只接收加密串,發郵件時解密)
    /// </summary>
    public string SmtpPassword
    ...{
        get ...{ return _smtpPassword; }
        set ...{ _smtpPassword = value; }
    }

    private string _content;
    /**//// <summary>
    /// 郵件內容
    /// </summary>
    public string Content
    ...{
        get ...{ return _content; }
        set ...{ _content = value; }
    }

    private string _title;
    /**//// <summary>
    /// 郵件標題
    /// </summary>
    public string Title
    ...{
        get ...{ return _title; }
        set ...{ _title = value; }
    }

    private string[] _cc = null;
    /**//// <summary>
    /// 抄送郵箱
    /// </summary>
    public string[] cc
    ...{
        get ...{ return _cc; }
        set ...{ _cc = value; }
    }

    private string[] _bcc = null;
    /**//// <summary>
    /// 密送郵箱
    /// </summary>
    public string[] bcc
    ...{
        get ...{ return _bcc; }
        set ...{ _bcc = value; }
    }

    /**//// <summary>
    ///發送郵件
    /// </summary>
    /// <returns>返回是否成功</returns>
    public bool Send()
    ...{
        try
        ...{
            MailMessage objMailMessage;
            objMailMessage = new MailMessage(SendEmail, _getemail, _title, _content);
            if (!string.IsNullOrEmpty(_replyToEmail) && !string.IsNullOrEmpty(_replyUserName))
            ...{
                MailAddress reply = new MailAddress(_replyToEmail, _replyUserName);
                objMailMessage.ReplyToList.Add(reply);
            }
            objMailMessage.BodyEncoding = Encoding.GetEncoding(936);
            objMailMessage.IsBodyHtml = true;
            if (cc != null && cc.Length > 0)
            ...{
                foreach (string ccAddress in cc)
                ...{
                    objMailMessage.CC.Add(new MailAddress(ccAddress));
                }
            }
            if (bcc != null && bcc.Length > 0)
            ...{
                foreach (string bccAddress in bcc)
                ...{
                    objMailMessage.Bcc.Add(new MailAddress(bccAddress));
                }
            }

            SmtpClient client = new SmtpClient(this._host, this._port);
            if (!String.IsNullOrEmpty(this.SmtpUsername) && !String.IsNullOrEmpty(this.SmtpPassword))
            ...{
                client.Credentials = new NetworkCredential(this.SmtpUsername, this.SmtpPassword);
            }
            client.EnableSsl = false;

            client.Send(objMailMessage);
            objMailMessage.Dispose();
            return true;
        }
        catch
        ...{
            return false;
        }
    }
}
 

調用方法及步驟:

1、建立SendMail類的一個對象,並依次給 這個對象發送郵件必須的參數,

2、調用 Send()方法。

 

郵件的附件功能,自己也可以根據上面的介紹擴充 SendMail類。這裡不在舉例。

 

在ASP.NET利用原生SMTP虛擬伺服器的SMTP來發送郵件

首先說一下SMTP配置。
        (1)右鍵點擊“SMTP虛擬伺服器”選擇“屬性”->在“常規”選項卡中設定“IP地址(P)”,我設定的是192.168.1.100。
        (2)選擇“訪問”選項卡,點擊“中繼”,選上“僅以下列表”(預設是被選上的),點擊“添加”,在“單台電腦”中加入192.168.1.100。
         提示,如果沒有完成(2),則會出現大家常見的一種錯誤提示:伺服器拒絕了一個或多個收件者地址。伺服器響應為: 550 5.7.1 Unable to relay for scucj@126.com (友情提示一下:錯誤中的郵件地址有所不同) 然後開始核心代碼,其實和方法(一)的差不多。與(一)的主要區別在於:1.SMTP的不同,2.objMailMessage.From中本方法可以隨便填寫,但是(一)中別隨便填寫那麼利用ASP.NET(C#)發送郵件的核心代碼如下: 
   

 代碼如下 複製代碼
      /核心代碼開始
         using System.Web.Mail;
         MailMessage objMailMessage;
         MailAttachment objMailAttachment;
         // 建立一個附件對象
         objMailAttachment = new MailAttachment( "d:\test.txt" );//發送郵件的附件
         // 建立郵件訊息
         objMailMessage = new MailMessage();
         objMailMessage.From = "mysina@sina.com";//源郵件地址
         objMailMessage.To = "scucj@126.com";//目的郵件地址,也就是發給我哈
         objMailMessage.Subject = "郵件發送標題:你好";//發送郵件的標題
         objMailMessage.Body = "郵件發送標內容:測試一下是否發送成功!";//發送郵件的內容
         objMailMessage.Attachments.Add( objMailAttachment );//將附件附加到郵件訊息對象中
         //SMTP地址
         SmtpMail.SmtpServer = "192.168.1.100";
         //開始發送郵件
         SmtpMail.Send( objMailMessage ); 

         以上兩種方法介紹到這裡。最簡單的利用上面方法是在頁面添加一個伺服器按鈕,把除引用的語句放到按鈕單擊事件中去。當然,別忘記了引用的語句放在最上面。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.