今天在收到一個任務,希望能通過程式給使用者發郵件,我想這樣的功能應該在網上資料很多了,不過自己一直都沒有去嘗試寫過。網上資料查一下,總結一下,代碼編寫一下,測試一下,很快很順利的把發郵件的程式寫好了。下面分享一下給大家(很多代碼都是簡寫,只記錄重點):
首先,在頁面加入一個Button按鈕控制項:
<asp:Button ID="點擊發郵件" runat="server" Text="Button" onclick="Button1_Click"/>
然後在Button的觸發事件裡寫發郵件要調用到的代碼:
protected void Button1_Click(object sender, EventArgs e)
{
Email em = new Email();
em.UserName = "寄件者"; //可以自己規定寄件者
em.EmailSendUserName="寄件者地址"; //可以自己規定郵件地址
em.EmailSendPass="寄件者密碼"; //可以自己規定密碼
em.Host = "主機地址"; //可以自己規定主機,如Gmail的是smtp.gmail.com
em.Port = "連接埠"; //可以自己規定連接埠,如0x24b
em.EmailToUserName = "收件者地址"; //可以自己規定郵件地址
em.Subject = "郵件標題"; //可以自己規定標題
em.Body = "郵件內容"; //可以自己規定內容
int i = em.SendMailUseGmail();
if (i > 0)
{
Response.Write("ok");
}
else
{
Response.Write("fail");
}
}
可以看到上面調用到了Email這個類,所以我們重點的地方就這個Email類,其中,主要調用SendMailUseGmail()這個發郵件的方法:
View Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
namespace EmailTest
{
public class Email
{
///<summary>
/// 發送地址
///</summary>
protected string _emailSendUserName = "";
public string EmailSendUserName
{
get
{
return _emailSendUserName;
}
set
{
_emailSendUserName = value;
}
}
///<summary>
/// 發送密碼
///</summary>
protected string _emailSendPass = "";
public string EmailSendPass
{
get
{
return _emailSendPass;
}
set
{
_emailSendPass = value;
}
}
///<summary>
/// 內送郵件地址
///</summary>
private string _emailToUserName = "";
public string EmailToUserName
{
get
{
return _emailToUserName;
}
set
{
_emailToUserName = value;
}
}
private string _subject = "";
public string Subject
{
get
{
return _subject;
}
set
{
_subject = value;
}
}
private string _body = "";
public string Body
{
get
{
return _body;
}
set
{
_body = value;
}
}
///<summary>
/// 發送人名稱
///</summary>
protected string _userName = "";
public string UserName
{
get
{
return _userName;
}
set
{
_userName = value;
}
}
protected string _host = "";
public string Host
{
get
{
return _host;
}
set
{
_host = value;
}
}
protected int _port = 0;
public int Port
{
get
{
return _port;
}
set
{
_port = value;
}
}
public int SendMailUseGmail()
{
/*
* msg.To.Add("b@b.com");
* msg.To.Add("b@b.com");
* msg.To.Add("b@b.com");可以發送給多人
* msg.CC.Add("c@c.com");
* msg.CC.Add("c@c.com");可以抄送給多人
*/
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
//接受人郵件地址 可多個
msg.To.Add(_emailToUserName);
msg.From = new MailAddress(_emailSendUserName, _userName, System.Text.Encoding.UTF8);
/* 上面3個參數分別是寄件者地址(可以隨便寫),寄件者姓名,編碼*/
msg.Subject = _subject;//郵件標題
msg.SubjectEncoding = System.Text.Encoding.UTF8;//郵件標題編碼
msg.Body = _body;//郵件內容
msg.BodyEncoding = System.Text.Encoding.UTF8;//郵件內容編碼
msg.IsBodyHtml = true;//是否是HTML郵件
msg.Priority = MailPriority.High;//郵件優先順序
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential(_emailSendUserName, _emailSendPass);
//上述寫你的GMail郵箱和密碼
client.Host = _host;
client.Port = _port;//Gmail使用的連接埠
client.EnableSsl = true;//經過ssl加密
//object userState = msg;
int i = 0;
try
{
client.Send(msg);
//client.SendAsync(msg, userState);
i = 1;
}
catch (System.Net.Mail.SmtpException ex)
{
i = -1;
}
return i;
}
}
}
沒有意外的話,以上的代碼應該可以實現簡單的郵件發送了,如果要完善以上的功能的話,還是有很大的空間的,希望能給大家一些啟發。