验证在C#中的电子邮件地址 [英] Validating an email Address in C#

查看:630
本文介绍了验证在C#中的电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用C#中使用下列code发送一封电子邮件。

  MailMessage电子邮件=新MailMessage();
      mail.From =新MailAddress(fromAddress,的friendlyName);
      mail.To.Add(的toAddress);
      mail.CC.Add(ccAddress);

      //设置内容
      mail.Subject = Emailsubject的;
      mail.Body = emailHeader +\ N+ emailBody;

      //发送消息
      SmtpClient SMTP =新SmtpClient(ServerAddress);
      smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
      mail.IsBodyHtml = TRUE;
      smtp.Send(邮件);
 

现在的的toAddress字符串,我的功能临危可能包含一个单独的地址,也可能有许多,以逗号分隔的地址。

现在的问题是,如果多个逗号分隔的地址,其中一个或两个的可能是错误的电子邮件地址的格式。

所以,当我尝试使用这个code发送一封电子邮件,我得到的异常:

指定的字符串不是需要一个电子邮件地址的形式。

有没有什么办法来验证逗号分隔的电子邮件地址? 我在某处读到过验证的电子邮件地址的唯一方法是发送电子邮件给它,因为正规EX pressions验证电子邮件addr​​eess更是出奇地巨大。

另外,我有过设计无法控制,或者怎么说地址字符串来我的功能,我不能添加的电子邮件验证的用户界面,让我很无奈有...

我的问题是,该邮件不会被传递到所有的,由逗号分隔的字符串的地址,即使只有若干的地址是错误的格式。

有没有什么办法可以正确验证的电子邮件地址中。NET?有没有一种方法来筛去坏的电子邮件地址和发送邮件到只有好的?<​​/ P>

解决方案

您可以只拆分的逗号电子邮件串并验证使用简单(或巨大)的电子邮件正则表达式的每个电子邮件地址。或者,尝试创建一个 MailAddress 对象;它支持地址的一些基本验证了。

I am trying to send an email using c# using the following code.

      MailMessage mail = new MailMessage();
      mail.From = new MailAddress(fromAddress, friendlyName);
      mail.To.Add(toAddress);
      mail.CC.Add(ccAddress);

      //set the content
      mail.Subject = emailSubject;
      mail.Body = emailHeader + "\n" + emailBody;

      //send the message
      SmtpClient smtp = new SmtpClient(ServerAddress);
      smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
      mail.IsBodyHtml = true;
      smtp.Send(mail);

Now the "toAddress" string that my function recieves might contain a single address, or it might have many, comma delimited addresses.

Now the problem is that, in case of multiple comma delimited addresses, one or two of them might be of the wrong email address format.

So when I try to send an email using this code, I get the exception:

"The specified string is not in the form required for an e-mail address."

Is there any way to validate the comma delimited email addresses? I had read somewhere that the only way to validate an email address is to send an email to it, because the regular expressions to validate an email addreess can be surprisingly huge.

Also, I have no control over the design, or on how that address string comes to my function,I can't add the email validation in the UI, so I am helpless there...

My problem is that the email will not be delivered to ALL the addresses in the comma delimited string, even though only SOME of the addresses are of the wrong format.

Is there any way to properly validate email addresses in .NET? Is there a way to weed out the bad email addresses and send the mail to only the good ones?

解决方案

You could just split the email string on the comma and validate each email address using a simple (or huge) email regex. Or, try creating a MailAddress object; it supports some basic validation of the address too.

这篇关于验证在C#中的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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