jmail.MessageClass jm = new jmail.MessageClass();
///设置邮件编码方式
jm.Charset = "gb2312";
///是否将信头编码成ISO-8859-1字符集
jm.ISOEncodeHeaders = false;
///邮件发送人的邮箱地址
jm.From = 发件人邮箱;
///邮件发送人
jm.FromName = 发件人;
///邮件的主题
jm.Subject = 邮件主题;
///身份验证的用户名
jm.MailServerUserName = 邮箱名;
///身份验证密码
jm.MailServerPassWord = 邮箱密码;
///收件人地址,如果要添加多个收件人,则重复下面的语句
jm.AddRecipient(收件人, "", "");
///邮件内容
jm.Body = txtcontent.Text.ToString().Trim().Replace("'", "");
///邮件服务器
///发送邮件
jm.Send(邮件服务器, false);
当收件人邮箱不存在的时候会报错,报错为:
The message was undeliverable. All servers failed to receive the message?
///设置邮件编码方式
jm.Charset = "gb2312";
///是否将信头编码成ISO-8859-1字符集
jm.ISOEncodeHeaders = false;
///邮件发送人的邮箱地址
jm.From = 发件人邮箱;
///邮件发送人
jm.FromName = 发件人;
///邮件的主题
jm.Subject = 邮件主题;
///身份验证的用户名
jm.MailServerUserName = 邮箱名;
///身份验证密码
jm.MailServerPassWord = 邮箱密码;
///收件人地址,如果要添加多个收件人,则重复下面的语句
jm.AddRecipient(收件人, "", "");
///邮件内容
jm.Body = txtcontent.Text.ToString().Trim().Replace("'", "");
///邮件服务器
///发送邮件
jm.Send(邮件服务器, false);
当收件人邮箱不存在的时候会报错,报错为:
The message was undeliverable. All servers failed to receive the message?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;using System.Net.Mail;
using System.Management;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Net;
using System.Net.Sockets;namespace CheckMailbox
{
public partial class Form1 : Form
{
TcpClient tcpc;
NetworkStream s;
string strDomain;
byte[] bb;
int len;
string read;
string stringTosend;
byte[] arrayToSend;
bool flag;
int count=0; public Form1()
{
InitializeComponent();
} public string getMailServer(string strEmail,bool IsCheck)
{
strDomain=strEmail.Split('@')[1];
if(IsCheck==true)
this.textBoxShow.Text += "分离出邮箱域名:" + strDomain + "\r\n";
ProcessStartInfo info=new ProcessStartInfo(); //指定启动进程时使用的一组值。
info.UseShellExecute=false;
info.RedirectStandardInput=true;
info.RedirectStandardOutput=true;
info.FileName="nslookup";
info.CreateNoWindow=true;
info.Arguments="-type=mx "+strDomain;
Process ns=Process.Start(info); //提供对本地和远程进程的访问并使您能够启动和停止本地系统进程。
StreamReader sout=ns.StandardOutput; Regex reg = new Regex(@"mail exchanger = (?<mailServer>[^\s]+)");
string strResponse="";
while((strResponse=sout.ReadLine())!=null){
Match amatch=reg.Match(strResponse); // Match 表示单个正则表达式匹配的结果。 if (reg.Match(strResponse).Success)
{
return amatch.Groups["mailServer"].Value; //获取由正则表达式匹配的组的集合
}
}
return null;
} private void Connect(string mailServer)
{
try
{
tcpc.Connect(mailServer, 25);
s = tcpc.GetStream();
len = s.Read(bb, 0, bb.Length);
read = Encoding.UTF8.GetString(bb);
if (read.StartsWith("220") == true)
this.textBoxShow.Text += "连接服务器成功!" + "\r\n";
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
} private bool SendCommand(string command)
{
try
{ arrayToSend = Encoding.UTF8.GetBytes(command.ToCharArray());
s.Write(arrayToSend, 0, arrayToSend.Length);
len = s.Read(bb, 0, bb.Length);
read = Encoding.UTF8.GetString(bb);
this.textBoxShow.Text += "收到:"+read.Substring(0, len) + "\r\n";
}
catch (IOException e)
{
MessageBox.Show(e.ToString());
}
if(read.StartsWith("250"))
return true;
else
return false;
} public void checkEmail(string mailAddress)
{
Regex reg = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"); if (!reg.IsMatch(mailAddress)) {
timer1.Enabled = false;
this.textBoxShow.Text += "Email地址形式上就不对" + "\r\n";
this.labelResult.Text = "邮箱不存在!";
return;
}//Email地址形式上就不对
string mailServer=getMailServer(mailAddress,true);
if(mailServer==null)
{
timer1.Enabled = false;
this.textBoxShow.Text += "邮件服务器不存在!" + "\r\n";
this.labelResult.Text = "邮箱不存在!";
return;
//邮件服务器探测错误
}
this.textBoxShow.Text += "解析出域名" + strDomain + "的mx记录:" + mailServer + "\r\n";
tcpc=new TcpClient(); //为 TCP 网络服务提供客户端连接。
tcpc.NoDelay=true;
tcpc.ReceiveTimeout=3000;
tcpc.SendTimeout=3000;
bb = new byte[512]; try
{ string strResponse = "";
string strTestFrom = "[email protected]"; this.textBoxShow.Text += "连接服务器:" +mailServer+ "\r\n";
Connect(mailServer); string mailServer2 = getMailServer(strTestFrom,false);
stringTosend = "helo " + mailServer2 + "\r\n";
this.textBoxShow.Text += "发送:" + stringTosend;
flag= SendCommand(stringTosend); if (flag == false)
{
timer1.Enabled = false;
return;
} stringTosend = "mail from:<" + strTestFrom + ">" + "\r\n";
this.textBoxShow.Text += "发送:" + stringTosend;
flag= SendCommand(stringTosend); if (flag == false)
{
timer1.Enabled = false;
return;
}
stringTosend = "rcpt to:<" + mailAddress + ">" + "\r\n";
this.textBoxShow.Text += "发送:" + stringTosend;
flag= SendCommand(stringTosend); if (flag == true)
{
timer1.Enabled = false;
this.labelResult.Text = "";
this.labelResult.Text = "邮箱存在!";
}
else
{
timer1.Enabled = false;
this.textBoxShow.Text += "用户不存在!" + "\r\n";
this.labelResult.Text = "邮箱不存在!";
}
/*
stringTosend = "data" + "\r\n";
this.textBoxShow.Text += "发送:" + stringTosend;
flag = SendCommand(stringTosend);
stringTosend = "Subject:wuzhi"+"\r\n"+"\r\n"+"hehe,zhiwu,wanshanghao!" + "\r\n"+"."+"\r\n";
flag = SendCommand(stringTosend);
*/
}
catch (Exception ee)
{
MessageBox.Show( ee.ToString()); //发生错误或邮件服务器不可达
}
}
private void buttonCheck_Click(object sender, EventArgs e)
{
string strMailbox;
strMailbox = this.textBoxMailbox.Text.ToString();
timer1.Enabled = true;
this.labelResult.Text = "";
this.textBoxShow.Text = "";
this.textBoxShow.Text = "开始检测邮箱地址:" + strMailbox+"\r\n"; checkEmail(textBoxMailbox.Text.ToString());
} private void timer1_Tick(object sender, EventArgs e)
{
if (count == 3) count = 0;
this.labelResult.Text = "";
string proc="。";
for (int i = 0; i<=count; i++)
{
proc += "。";
}
this.labelResult.Text = "";
this.labelResult.Text = "检测中"+proc;
count++;
}
}
}