C# 使用System.Net.Mail发邮件,如何捕捉油箱地址不正确时,显示失败

alex_peib 2009-04-14 06:22:58
各位:
我在C#中使用System.Net.Mail发邮件,当收件人邮箱地址不正确时,我使用Send发送时,怎么捕捉到发送失败的信息.谢谢指点!
...全文
336 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangjianliang01 2009-04-14
  • 打赏
  • 举报
回复
验证输入的正确性

public static bool isEmail( string inputEmail )
{
inputEmail = NulltoString( inputEmail );
string strRegex = @"^( [a-zA-Z0-9_\-\.]+ )@( ( \[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\. )|( ( [a-zA-Z0-9\-]+\. )+ ) )( [a-zA-Z]{2,4}|[0-9]{1,3} )( \]? )$";
Regex re = new Regex( strRegex );
if ( re.IsMatch( inputEmail ) )
return ( true );
else
return ( false );
}

验证邮件地址的正确性:

string[] host = ( address.Split( @ ) );
string hostname = host[1];

IPHostEntry IPhst = Dns.Resolve( hostname );
IPEndPoint endPt = new IPEndPoint( IPhst.AddressList[0], 25 );
Socket s= new Socket( endPt.AddressFamily, SocketType.Stream,ProtocolType.Tcp );
s.Connect( endPt );

//Attempting to connect
if( !Check_Response( s, SMTPResponse.CONNECT_SUCCESS ) )
{
s.Close( );
return false;
}

//HELO server
Senddata( s, string.Format( "HELO {0}\r\n", Dns.GetHostName( )) );
if( !Check_Response( s, SMTPResponse.GENERIC_SUCCESS ) )
{
s.Close( );
return false;
}

//Identify yourself
//Servers may resolve your domain and check whether you are listed in BlackLists etc.
Senddata( s, string.Format( "MAIL From: {0}\r\n","testexample@deepak.portland.co.uk" ) );
if( !Check_Response( s, SMTPResponse.GENERIC_SUCCESS ) )
{

s.Close( );
return false;
}

//Attempt Delivery ( I can use VRFY, but most SMTP servers only disable it for security reasons )
Senddata( s, address );
if( !Check_Response( s, SMTPResponse.GENERIC_SUCCESS ) )
{

s.Close( );
return false;
}
return ( true );
龙宜坡 2009-04-14
  • 打赏
  • 举报
回复
C# 使用System.Net.Mail发邮件,如何捕捉油箱地址不正确时,显示失败
fenglaijun 2009-04-14
  • 打赏
  • 举报
回复

//现在可以了邮件地址格式错误抛出FormatExectpion
//发送失败抛出SmtpExecption
try
{
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("fenglaijunx@qq.com");
System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress("fenglaijunx@126.com");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);
message.Body = "This mail send from C# application";

System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = "http://www.126.com";
client.Send(message);
}
catch (FormatException)
{
MessageBox.Show("电子邮件格式不符合要求!");
}
catch (System.Net.Mail.SmtpException)
{
MessageBox.Show("邮件发送失败!");

}
fenglaijun 2009-04-14
  • 打赏
  • 举报
回复
我不是楼主。
你的意思是地址格式正确但是这个地址不存在或者其他?
alex_peib 2009-04-14
  • 打赏
  • 举报
回复
楼主,象你这样在用send方法发送时,邮箱地址不正确,程序也不走catch这里
fenglaijun 2009-04-14
  • 打赏
  • 举报
回复

try
{
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("from126");
System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress("to126");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);
message.Body = "This mail send from C# application";
}
catch (FormatException)
{
MessageBox.Show("电子邮件格式不符合要求!");
}

111,098

社区成员

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

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

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