我如何捕获内部异常

时间:2012-02-27 02:29:22

标签: c# exception smtp sendmail inner-exception

我的发送邮件代码无效。它没有发送任何电子邮件或返回任何错误。我没有从下面的代码中得到一个Exception。如何设置以捕获以下代码中可能发生的常见错误?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Net.Mail; 
using System.Net;

namespace DataAccess 
{ 
    public class DLSendMails 
    {
        public string SendCommentMails(String MemberEmail, int
        { 
            try 
            {
                using (SmtpClient client =
                         new SmtpClient("smtpout.secureserver.net"))
                { 
                    client.Credentials =
                     new NetworkCredential("webmaster@mysite.com", "pwd"); 

                    //client.Credentials
                    //         = CredentialCache.DefaultNetworkCredentials;  
                    //client.DeliveryMethod
                    //         = SmtpDeliveryMethod.Network;  

                    string to = MemberEmail; 

                    var mail = new MailMessage(); 
                    mail.From = new MailAddress("webmaster@mysite.com",
                                                "mysite.com"); 
                    mail.To.Add(to);

                    mail.Subject = "New Comment recieved at mysite.com"; 
                    mail.Body = "You can read your comment and access your "
                              + "profile page by using this link.\n"
                              + "http://www.mysite.com/Member.aspx?UserID="
                              + PicID; 
                    mail.IsBodyHtml = true; 

                    client.Send(mail); 
                    return "sent mail";
                }
            } 
            catch (Exception ex) 
            { 
                // exception handling  
                return ex.ToString(); 
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先,我不确定它是否是故意的,但在您的客户端.Credentials代码您输入的是“passowrd”而不是“password”。这是不是一个错字?

此外,在此相关帖子c# SmtpClient class not able to send email using gmail中,用户在设置新的NetworkCredentials之前表示您必须将UseDefaultCredentials 声明为false。另外,在我在这里链接的同一篇文章中,它说可能是防火墙干扰的可能性?

我希望这可能会有所帮助,但如果不是,我肯定会继续阅读它,因为我想了解更多关于此事的信息!祝你好运!

相关问题