C#通用郵件發(fā)送類分享
此類的功能包括發(fā)送郵件,郵箱格式是否正確,和在不發(fā)送郵件的情況下判斷郵箱用戶名和密碼是否正確,鑒于POP檢查郵箱用戶名和密碼出現(xiàn)錯(cuò)誤情況返回結(jié)果的延遲,用異步線程解決此問(wèn)題,見代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Web;
using System.Net;
using System.Text.RegularExpressions;
using System.Net.Sockets;
using System.IO;
using System.Collections;
using System.Threading;
namespace Com.Web
{
/// <summary>
/// 郵箱類
/// </summary>
public class CheckEmailInfo
{
public string server { get; set; }//服務(wù)器
public string user { get; set; }//用戶名
public string pwd { get; set; }//密碼
}
/// <summary>
/// SendEmail通用類,通過(guò)smtp服務(wù)器發(fā)送郵件
/// </summary>
public class SendEmail
{
public Dictionary<string, string> smtpServer;
public Dictionary<string, string> popServer;
public SendEmail()
{
IniSmtpServer();
IniPopServer();
}
/// <summary>
/// 初始化常用smtpServer,用于綁定下拉選擇菜單
/// </summary>
private void IniSmtpServer()
{
smtpServer = new Dictionary<string, string>();
smtpServer.Add("網(wǎng)易163郵箱", "smtp.163.com");
smtpServer.Add("網(wǎng)易vip.163郵箱", "smtp.vip.163.com");
smtpServer.Add("網(wǎng)易126郵箱", "smtp.126.com");
smtpServer.Add("網(wǎng)易188郵箱", "smtp.188.com");
smtpServer.Add("新浪郵箱", "smtp.sina.com");
smtpServer.Add("雅虎郵箱", "smtp.mail.yahoo.com");
smtpServer.Add("搜狐郵箱", "smtp.sohu.com");
smtpServer.Add("TOM郵箱", "smtp.tom.com");
smtpServer.Add("Gmail郵箱", "smtp.gmail.com");
smtpServer.Add("QQ郵箱", "smtp.qq.com");
smtpServer.Add("QQ企業(yè)郵箱", "smtp.biz.mail.qq.com");
smtpServer.Add("139郵箱", "smtp.139.com");
smtpServer.Add("263郵箱", "smtp.263.com");
}
/// <summary>
/// 初始化常用popServer,用于綁定下拉選擇菜單
/// </summary>
private void IniPopServer()
{
popServer = new Dictionary<string, string>();
popServer.Add("網(wǎng)易163郵箱", "pop3.163.com");
popServer.Add("網(wǎng)易vip.163郵箱", "pop3.vip.163.com");
popServer.Add("網(wǎng)易126郵箱", "pop3.126.com");
popServer.Add("網(wǎng)易188郵箱", "pop3.188.com");
popServer.Add("新浪郵箱", "pop3.sina.com");
popServer.Add("雅虎郵箱", "pop3.mail.yahoo.com");
popServer.Add("搜狐郵箱", "pop3.sohu.com");
popServer.Add("TOM郵箱", "pop.tom.com");
popServer.Add("Gmail郵箱", "pop.gmail.com");
popServer.Add("QQ郵箱", "pop.qq.com");
popServer.Add("QQ企業(yè)郵箱", "pop.biz.mail.qq.com");
popServer.Add("139郵箱", "pop.139.com");
popServer.Add("263郵箱", "pop.263.com");
}
/// <summary>
/// 發(fā)送郵件功能
/// </summary>
/// <param name="fromEmail">登錄郵箱</param>
/// <param name="password">登錄密碼</param>
/// <param name="user">郵件昵稱</param>
/// <param name="title">郵件標(biāo)題</param>
/// <param name="toEmail">郵件地址</param>
/// <param name="email">郵件內(nèi)容</param>
/// <param name="smtpServer">smtp服務(wù)器</param>
public bool SendMessage(string fromEmail,string password, string user, string title, string toEmail, string email,string smtpServer)
{
try
{
SmtpClient smtp = new SmtpClient(); //實(shí)例化一個(gè)SmtpClient
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //將smtp的出站方式設(shè)為 Network
smtp.EnableSsl = false;//smtp服務(wù)器是否啟用SSL加密
smtp.Host = smtpServer;//指定 smtp 服務(wù)器
smtp.Credentials = new NetworkCredential(fromEmail, password);
MailMessage mm = new MailMessage(); //實(shí)例化一個(gè)郵件類
mm.Priority = MailPriority.High; //郵件的優(yōu)先級(jí),分為 Low, Normal, High,通常用 Normal即可
mm.From = new MailAddress(fromEmail, user, Encoding.GetEncoding(936));
mm.CC.Add(new MailAddress(toEmail, "", Encoding.GetEncoding(936)));
mm.Subject = title; //郵件標(biāo)題
mm.SubjectEncoding = Encoding.GetEncoding(936);
mm.IsBodyHtml = true; //郵件正文是否是HTML格式mm.BodyEncoding = Encoding.GetEncoding(936);
mm.Body = email;
smtp.Send(mm);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 檢查郵箱是否正確的委托
/// </summary>
delegate bool MyDelegate(object checkEmailInfo);
/// <summary>
/// 利用異步方式檢查郵箱賬號(hào)和密碼是否正確
/// </summary>
public bool CheckUser(string server, string user, string pwd)
{
MyDelegate myDelegate = new MyDelegate(CheckUser);
CheckEmailInfo checkEmailInfo = new CheckEmailInfo();
checkEmailInfo.server = server;
checkEmailInfo.user = user;
checkEmailInfo.pwd = pwd;
IAsyncResult result = myDelegate.BeginInvoke(checkEmailInfo, null, null);
Thread.Sleep(1000);//主線程1秒后檢查異步線程是否運(yùn)行完畢
if (result.IsCompleted)
{ return myDelegate.EndInvoke(result); }//如果錯(cuò)誤的郵箱和密碼,函數(shù)將會(huì)運(yùn)行很慢
else
{ return false; }
}
/// <summary>
/// 判斷用戶郵箱賬號(hào)和密碼是否正確
/// </summary>
/// <param name="server">PopServer地址</param>
/// <param name="user">用戶名</param>
/// <param name="pwd">密碼</param>
private bool CheckUser(object checkEmailInfo)
{
CheckEmailInfo checkInfo = (CheckEmailInfo)checkEmailInfo;
TcpClient sender = new TcpClient(checkInfo.server, 110);//pop協(xié)議使用TCP的110端口
Byte[] outbytes;
NetworkStream ns;
StreamReader sr;
string input;
string readuser = string.Empty;
string readpwd = string.Empty;
try
{
ns = sender.GetStream();
sr = new StreamReader(ns);
sr.ReadLine();
//檢查用戶名和密碼
input = "user " + checkInfo.user+ "\r\n";
outbytes = Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
readuser = sr.ReadLine();
input = "pass " + checkInfo.pwd + "\r\n";
outbytes =Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
readpwd = sr.ReadLine();
if (readuser.Substring(0, 3) == "+OK" && readpwd.Substring(0, 3) == "+OK")
{ return true; }
else
{ return false; }
}
catch
{
return false;
}
}
/// <summary>
/// 判斷郵箱格式是否正確
/// </summary>
/// <param name="email">郵箱地址</param>
public bool IsEmail(string email)
{
string paterner = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
if (!Regex.IsMatch(email, paterner))
{ return false;}
else
{return true;}
}
}
}
相關(guān)文章
C#開發(fā)Android百度地圖手機(jī)應(yīng)用程序(多地圖展示)
這篇文章主要介紹了C#開發(fā)Android百度地圖手機(jī)應(yīng)用程序(多地圖展示)的相關(guān)資料,需要的朋友可以參考下2016-02-02
C#獲取系統(tǒng)當(dāng)前IE版本號(hào)
這篇文章主要為大家詳細(xì)介紹了C#獲取系統(tǒng)當(dāng)前IE版本號(hào),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
C#實(shí)現(xiàn)Array,List,Dictionary相互轉(zhuǎn)換
這篇文章介紹了C#實(shí)現(xiàn)Array,List,Dictionary互相轉(zhuǎn)換的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
datagridview實(shí)現(xiàn)手動(dòng)添加行數(shù)據(jù)
這篇文章主要介紹了datagridview實(shí)現(xiàn)手動(dòng)添加行數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
C# IQueryable及IEnumerable區(qū)別解析
這篇文章主要介紹了C# IQueryable及IEnumerable區(qū)別解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
c#使用windows服務(wù)更新站點(diǎn)地圖的詳細(xì)示例
這篇文章主要介紹了c#使用windows服務(wù)更新站點(diǎn)地圖的詳細(xì)示例,需要的朋友可以參考下2014-04-04

