1.實現原理:
- 微軟封裝好的MailMessage類:主要處理發送郵件的內容(如:收發人地址、標題、主體、圖片等等)
- 微軟封裝好的SmtpClient類:主要處理用smtp方式發送此郵件的配置信息(如:郵件服務器、發送端口號、驗證方式等等)
- SmtpClient主要進行了三層的封裝:Socket --> TcpClient --> SmtpClient
2.構建幫助工具類:
using System;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;
public class MyEmail
{
private MailMessage mMailMessage; //主要處理發送郵件的內容(如:收發人地址、標題、主體、圖片等等)
private SmtpClient mSmtpClient; //主要處理用smtp方式發送此郵件的配置信息(如:郵件服務器、發送端口號、驗證方式等等)
private int mSenderPort; //發送郵件所用的端口號(htmp協議默認為25)
private string mSenderServerHost; //發件箱的郵件服務器地址(IP形式或字符串形式均可)
private string mSenderPassword; //發件箱的密碼
private string mSenderUsername; //發件箱的用戶名(即@符號前面的字符串,例如:hello@163.com,用戶名為:hello)
private bool mEnableSsl; //是否對郵件內容進行socket層加密傳輸
private bool mEnablePwdAuthentication; //是否對發件人郵箱進行密碼驗證
///<summary>
/// 構造函數
///</summary>
///<param name="server">發件箱的郵件服務器地址</param>
///<param name="toMail">收件人地址(可以是多個收件人,程序中是以“;"進行區分的)</param>
///<param name="fromMail">發件人地址</param>
///<param name="subject">郵件標題</param>
///<param name="emailBody">郵件內容(可以以html格式進行設計)</param>
///<param name="username">發件箱的用戶名(即@符號前面的字符串,例如:hello@163.com,用戶名為:hello)</param>
///<param name="password">發件人郵箱密碼</param>
///<param name="port">發送郵件所用的端口號(htmp協議默認為25)</param>
///<param name="sslEnable">true表示對郵件內容進行socket層加密傳輸,false表示不加密</param>
///<param name="pwdCheckEnable">true表示對發件人郵箱進行密碼驗證,false表示不對發件人郵箱進行密碼驗證</param>
public MyEmail(string server, string toMail, string fromMail, string subject, string emailBody, string username, string password, string port, bool sslEnable, bool pwdCheckEnable)
{
try
{
mMailMessage = new MailMessage();
mMailMessage.To.Add(toMail);
mMailMessage.From = new MailAddress(fromMail);
mMailMessage.Subject = subject;
mMailMessage.Body = emailBody;
mMailMessage.IsBodyHtml = true;
mMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
mMailMessage.Priority = MailPriority.Normal;
this.mSenderServerHost = server;
this.mSenderUsername = username;
this.mSenderPassword = password;
this.mSenderPort = Convert.ToInt32(port);
this.mEnableSsl = sslEnable;
this.mEnablePwdAuthentication = pwdCheckEnable;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
///<summary>
/// 郵件的發送
///</summary>
public void Send()
{
try
{
if (mMailMessage != null)
{
mSmtpClient = new SmtpClient();
//mSmtpClient.Host = "smtp." + mMailMessage.From.Host;
mSmtpClient.Host = this.mSenderServerHost;
mSmtpClient.Port = this.mSenderPort;
mSmtpClient.UseDefaultCredentials = false;
mSmtpClient.EnableSsl = this.mEnableSsl;
if (this.mEnablePwdAuthentication)
{
System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
//mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
//NTLM: Secure Password Authentication in Microsoft Outlook Express
mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
}
else
{
mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
}
mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
mSmtpClient.Send(mMailMessage);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
///<summary>
/// 添加附件
///</summary>
///<param name="attachmentsPath">附件的路徑集合,以分號分隔</param>
public void AddAttachments(string attachmentsPath)
{
try
{
string[] path = attachmentsPath.Split(';'); //以什么符號分隔可以自定義
Attachment data;
ContentDisposition disposition;
for (int i = 0; i < path.Length; i++)
{
data = new Attachment(path[i], MediaTypeNames.Application.Octet);
disposition = data.ContentDisposition;
disposition.CreationDate = File.GetCreationTime(path[i]);
disposition.ModificationDate = File.GetLastWriteTime(path[i]);
disposition.ReadDate = File.GetLastAccessTime(path[i]);
mMailMessage.Attachments.Add(data);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
3.控制台相關代碼
static void Main(string[] args)
{
try
{
//smtp.163.com
//string senderServerIp = "123.125.50.133";
//smtp.gmail.com
//string senderServerIp = "74.125.127.109";
//smtp.qq.com
string senderServerIp = "smtp.qq.com";//qq郵箱的smtp地址
//string senderServerIp = "smtp.sina.com";
string toMailAddress = "1277529182@qq.com";//收件人的郵箱地址
string fromMailAddress = "1277529182@qq.com";//發件人的郵箱地址
string subjectInfo = "Test";//主題
string bodyInfo = "This is test email";//內容
string mailUsername = "1277529182";//郵箱的用戶名
string mailPassword = "111"; //發送郵箱的密碼(如果是QQ郵箱需要配置客戶端密碼)
string mailPort = "25";//端口
//string attachPath = "E:\\123123.txt; E:\\haha.pdf";//添加的附件
MyEmail email = new MyEmail(senderServerIp, toMailAddress, fromMailAddress, subjectInfo, bodyInfo, mailUsername, mailPassword, mailPort, true, true);
//email.AddAttachments(attachPath);
email.Send();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
4.需要身份驗證的郵箱SMTP,例如QQ郵箱,則需要配置客戶端密碼:
開啟SMTP服務
最后使用生成的授權碼填入到【發送郵箱的密碼】
注意:這里說的【發送郵箱的密碼】是指代碼中mailPassword 值把“111”改為你的授權密碼
溫馨提醒:為了你的帳戶安全,更改QQ密碼以及獨立密碼會觸發授權碼過期,需要重新獲取新的授權碼登錄。
下面附上騰訊郵箱授權碼如何設置文檔:
http://service.mail.qq.com/cgi-bin/help?subtype=1&&no=1001256&&id=28