使用c#发送邮件错误 [英] Mail Sending Error using c#

查看:76
本文介绍了使用c#发送邮件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



我在向多个收据发送邮件时遇到问题,

如果任何一个电子邮件ID失败或是错误的,没有或其他邮件没有交付。



示例代码。



< pre lang =cs> MailMessage m = new MailMessage();
SmtpClient sc = new SmtpClient();
m.From = new MailAddress( a@dot.com Vijay Kumar);
string sMailIds = a @ dot。 COM,b @ dot.com,b @ dot.com;
if (sMailIds.Trim()!= string .Empty)
{
string [] mailid = sMailIds.Split(' );
尝试
{
foreach string mail in mailid)
{
m.To.Add( new MailAddress(mail, Test));
}
m.Subject = 测试;
m.Body = test;
m.IsBodyHtml = true ;
sc.Host = mail.adityabirla.com;
sc.Credentials = new System.Net.NetworkCredential( abc@dot.com xyz);
sc.Send(m);
}
catch (例外情况)
{
}
}

解决方案

我假设发生的事情是MailAddress的创建引发异常 - 但你不能告诉因为你正在吞下异常并忽略它。永远不要那样做!记录下来,这样当你遇到这样的问题时,你实际上可以告诉他发生了什么......



所以,更改你的catch块来报告问题,或记录它,并查看异常是什么,然后可能需要检查 mail 变量的有效性,然后再尝试将其创建为MailAddress,或者添加一个try-catch阻止实际创建每个MailAdress - 然后报告哪一个有问题,以便您可以在数据库中更正。





那只是一个样本家伙,我将采取所有异常处理,我只是问一个解决方案,如果其中任何一个失败,如何处理休息邮件的失败。



只是想忽略失败的id和rest id应该收到邮件,但实际上并没有唠叨。


Dear Friends,

I am facing a issue while sending mail to multiple receiptant,
if any one of email id failed or is wrong, none or rest of the mails are not getting delivered.

sample code.

MailMessage m = new MailMessage();
           SmtpClient sc = new SmtpClient();
           m.From = new MailAddress("a@dot.com", "Vijay Kumar");
           string sMailIds = "a@dot.com,b@dot.com,b@dot.com";
           if (sMailIds.Trim() != string.Empty)
           {
               string[] mailid = sMailIds.Split(',');
               try
               {
                   foreach (string mail in mailid)
                   {
                      m.To.Add(new MailAddress(mail, "Test"));
                   }
                   m.Subject = "Test";
                   m.Body = "test";
                   m.IsBodyHtml = true;
                   sc.Host = "mail.adityabirla.com";
                   sc.Credentials = new System.Net.NetworkCredential("abc@dot.com", "xyz");
                   sc.Send(m);
               }
               catch (Exception ex)
               {
               }
           }

解决方案

I assume that what is happening is that the creation of the MailAddress is throwing an exception - but you can''t tell because you are swallowing the exception and ignoring it. Never do that! Log it somewhere so that when you get a problem like this you can actually tell what the heck happened...

So, change your catch block to report a problem, or log it, and look at what the exception is, then probably you need to either check the mail variable for validity before you try to create it as a MailAddress, or add a try-catch block around the actual creation of each MailAdress - then report which one is faulty so you can correct in in your database.


That''s just a sample dude, i''ll take all exception handling, i am just asking a solution that how to take care failure of rest mail message if any one of them fails.

Just want to ignore failed id and rest ids should receive the mail, but its not happing actually.


这篇关于使用c#发送邮件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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