例如:123@abcdef123.com这个邮箱格式是正确的,但是这个邮箱是不存在的.用asp.net发送邮件到这个邮箱就会出错,程序不能继续往下执行.有没有可以忽略不存在邮箱的方法?
在发送时加个异常判断就好了啊
SmtpClient client = new SmtpClient();
//注册的邮箱和密码,例如gmail:
client.Credentials = new System.Net.NetworkCredential("gmail address", "email password");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
try
{
client.Send(mailMessage);
}
catch (System.Net.Mail.SmtpException ex)
{
}
这样就可以忽略了
我的邮件发送是for循环遍历发送的,其中有一些邮箱是不存在的,执行到不存在的邮箱地址,for循环就终止了,不能继续向下执行,我想知道有没有办法,让for循环在遇到异常后仍能忽略异常继续执行下一条循环?
如下所示代码,数组中的前两个邮箱地址都是不存在的,第三个是存在的,但是程序遇到异常跳出了循环,第三个邮箱也接收不到信息了.
public void SetBind()
{
try
{
string[] maillist = new string[] { "123123123123@qqmark.com", "fkasldfjkasjdflk@qqmark.com", "792657655@qq.com" };
for (int i = 0; i < 3; i++)
{
SendSMTPEMail("smtp.exmail.qq.com", "mark@qq.com", "mark123",maillist[i].ToString(), "不存在邮箱测试!", "test");
}
}
catch (Exception e)
{
Label1.Text = e.Message.ToString();
}
}
public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
{
System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
client.Port = 25;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
}
@棋怜:
try
{
string[] maillist = new string[] { "123123123123@qqmark.com", "fkasldfjkasjdflk@qqmark.com", "792657655@qq.com" };
for (int i = 0; i < 3; i++)
{
SendSMTPEMail("smtp.exmail.qq.com", "mark@qq.com", "mark123",maillist[i].ToString(), "不存在邮箱测试!", "test");
}
}
catch (Exception e)
{
Label1.Text = e.Message.ToString();
}
改为:
string[] maillist = new string[] { "123123123123@qqmark.com", "fkasldfjkasjdflk@qqmark.com", "792657655@qq.com" };
for (int i = 0; i < 3; i++)
{
try
{
SendSMTPEMail("smtp.exmail.qq.com", "mark@qq.com", "mark123",maillist[i].ToString(), "不存在邮箱测试!", "test");
}
catch (Exception e)
{
Label1.Text = e.Message.ToString();
}
}
@artwl: thank you,可以搞定!
顺便问一下,asp.net是否可以验证邮箱是否真实存在,或者是否能得到smtp的返回状态.
@棋怜: 这个好像不行的,一般的做法是发激活邮件