邮件发送怎么样检测邮箱是否真实存在

天上下雨 2009-07-25 11:41:29
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?
...全文
906 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qingniaoIT 2009-07-27
  • 打赏
  • 举报
回复
看了楼上的所有方法, 应该都是不行的吧,个人认为目前还没有一种很好的办法来快速检测邮箱地址是否存在, 虽然帖主已经结贴了, 但还是大家来讨论这个问题。
天上下雨 2009-07-25
  • 打赏
  • 举报
回复
谢谢了
yuanmanguo 2009-07-25
  • 打赏
  • 举报
回复
以前的一段代码

using System;
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 = "755191381@qq.com";

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++;
}
}
}



天上下雨 2009-07-25
  • 打赏
  • 举报
回复
我只要求放、当邮箱不存在的时候我不执行发送邮件的命令!
abosorbin 2009-07-25
  • 打赏
  • 举报
回复
路过,顶下~
zgke 2009-07-25
  • 打赏
  • 举报
回复
这个要看对方POP3服务是否即使返回油箱是否存在的邮件了.
winner2050 2009-07-25
  • 打赏
  • 举报
回复
如果能检测邮箱是否真实存在的话,就没有人出售邮件列表。

用穷举法列举几百亿的邮箱。
天上下雨 2009-07-25
  • 打赏
  • 举报
回复
连发2贴,都给分的

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧