发送电子邮件的Smtp例外 [英] Smtp exception for sending emails

查看:327
本文介绍了发送电子邮件的Smtp例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在用户尝试注册时向用户发送电子邮件。以下是我的代码。



I am trying to send emails to the user when a user tries to register.The following is my code.

public static bool SendEmail(string fromEmail, string fromName, string toEmail, string toName, string subject, string body)
   {
       bool hasSent = false;
       CDS.Framework.ErrorManager frameworkError = new CDS.Framework.ErrorManager(Common.ApplicationName);
       frameworkError.recordDebug("Sending Email");

       if (IsEmail(toEmail))
       {
           try
           {
               MailMessage objMessage = new MailMessage();

               objMessage.From = new MailAddress(fromEmail, fromName);
               objMessage.To.Add(new MailAddress(toEmail, toName));
               objMessage.Subject = subject;
               objMessage.Body = body;
               objMessage.Priority = System.Net.Mail.MailPriority.Normal;

               CDS.Framework.EmailManager objEmail = new CDS.Framework.EmailManager(Common.ApplicationName);

              objEmail.isBodyHtml = false;
               objEmail.sendEmail(objMessage);

               frameworkError.recordDebug("Sending Email - Sent - True");
               hasSent = true;
           }
           catch (Exception ex)
           {
               frameworkError.recordError(ex);
               hasSent = false;
           }
       }
       return hasSent;
   }





当我运行我的代码时,在catch语句中获取异常为Smtp Exception

和内部异常表示发送邮件失败。



任何人都可以帮我解决这个问题。



when i run my code get an exception at the catch statement as "Smtp Exception"
and the inner exception says as "Failure sending mail".

Can anyone please help me solve this issue.

推荐答案

您好,



您收到电子邮件的原因太多了。



要获得有关发送邮件失败原因的更多信息,请尝试捕获 SmtpException [ ^ ],并注意SmtpException.StatusCode Property [ ^ ] ...因为这很可能包含失败原因的描述。



要捕获SmtpException,请在现有的Exception catch块之前添加一个catch块:

Hi,

There are too many possible reasons why you emails are not getting sent.

To get more information as to why there is a failure sending your messages, try trapping the SmtpException[^], and pay attention to the SmtpException.StatusCode Property [^]... as this will mostly likely contain a description of the failure reason.

To catch the SmtpException, add a catch block before your existing Exception catch block:
try
           {
               MailMessage objMessage = new MailMessage();

               objMessage.From = new MailAddress(fromEmail, fromName);
               objMessage.To.Add(new MailAddress(toEmail, toName));
               objMessage.Subject = subject;
               objMessage.Body = body;
               objMessage.Priority = System.Net.Mail.MailPriority.Normal;

               CDS.Framework.EmailManager objEmail = new CDS.Framework.EmailManager(Common.ApplicationName);

              objEmail.isBodyHtml = false;
               objEmail.sendEmail(objMessage);

               frameworkError.recordDebug("Sending Email - Sent - True");
               hasSent = true;
           }
           catch (SmtpException smtpex)
           {
               frameworkError.recordError(smtpex);
               hasSent = false;
           }
           catch (Exception ex)
           {
               frameworkError.recordError(ex);
               hasSent = false;
           }





...希望它有所帮助。



... hope it helps.


这篇关于发送电子邮件的Smtp例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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