检查smtp电子邮件是否成功 [英] Checking if an smtp email is successful

查看:241
本文介绍了检查smtp电子邮件是否成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我向无效的电子邮件地址发送电子邮件时,我试图捕获异常,但永远不会抛出异常。仅当From电子邮件地址错误时才会抛出它。有没有办法检查电子邮件是否有效。我的代码如下:


SmtpClient client = new SmtpClient (serverName );

I am trying to catch an exception when I send an email to an invalid email address, but the exception is never thrown. It is only thrown when the From email address is wrong. Is there any way to check if the email has not worked. My code is as follows:

SmtpClient client = new SmtpClient(serverName);

statusCode = 0;

statusCode = 0;

statusDescription = string .Empty;

statusDescription = string.Empty;

client.Credentials =( ICredentialsByHost CredentialCache .DefaultNetworkCredentials;

client.Credentials = (ICredentialsByHost)CredentialCache.DefaultNetworkCredentials;

MailMessage msg = new MailMessage ();

MailMessage msg = new MailMessage();

msg.To.Add(target.TargetEmail);

msg.To.Add(target.TargetEmail);

msg.From = new < font color ="#2b91af"size = 2> MailAddress fromEmailAddress );

msg.From = new MailAddress(fromEmailAddress);

msg.DeliveryNotificationOptions = DeliveryNotificationOptions .OnFailure;

msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

msg.Subject ="测试" ;

msg.Subject = "Test";

msg.IsBodyHtml = true ;

msg.IsBodyHtml = true;

msg.Body = transformedHTML;

msg.Body = transformedHTML;

{

client.Send(msg);

client.Send(msg);

}

catch SmtpException ex)

catch (SmtpException ex)

{

statusCode = ex.StatusCode;

statusCode = ex.StatusCode;

if (statusCode == SmtpStatusCode .MailboxUnavailable)

if (statusCode == SmtpStatusCode.MailboxUnavailable)

{

statusDescription = ex.Message.ToString();

statusDescription = ex.Message.ToString();

statusDescription + = " " + BusinessObjectResources .sr_InvalidEmailAddressDescription;

statusDescription += " " + BusinessObjectResources.sr_InvalidEmailAddressDescription;

}

else

else

{

statusDescription = ex.Message.ToString();

statusDescription = ex.Message.ToString();

}

rStatus = false ;

rStatus = false;

}

catch 例外 ex)

catch (Exception ex)

{

throw ex;

throw ex;

}

finally

finally

{

if (msg!= null

if (msg != null)

{

msg.Dispose();

msg.Dispose();

msg = null ;

msg = null;

}

}

推荐答案

Afaik,one方法是验证target.TargetEmail并检查它是否拥有@并通过解析字符串使用该格式看起来像一个有效的电子邮件地址,然后将其作为参数传递给msg.To()

另一种方式可能是检查你的发件箱/发送的项目,以验证它是否已被发送。

我没有遇到任何例外,因为如果你已配置outlook,To字段将接受包括数字在内的任何内容使用Windows传真服务从您的邮箱发送传真。

希望这会有所帮助。
Afaik, one way is to validate target.TargetEmail and check if it possess an @ and looks like a valid email address using the format by parsing the string and then pass it on as a parameter to msg.To()

Another way might be to check your outbox/sent items to verify if it's been dispatched or not after that.

I haven't come across any exceptions till date as the To field would accept anything including numbers if you have configured outlook to send fax from your mail box using windows fax services.

Hope this helps.


这篇关于检查smtp电子邮件是否成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆