首页 新闻 搜索 专区 学院

发送邮件报GeneralFailure异常

0
悬赏园豆:5 [待解决问题]

发送邮件,捕获到的异常SmtpException,具体的异常信息如下

服务器提交了协议冲突 服务器响应为: GeneralFailure 在 System.Net.Mail.SendMailAsyncResult.End(IAsyncResult result)
在 System.Net.Mail.SmtpClient.SendMailCallback(IAsyncResult result)

GeneralFailure此错误信息的官方解读为:

The transaction could not occur. You receive this error when the specified SMTP host cannot be found.

详见:https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpstatuscode?view=netframework-4.8

代码:

  /// <summary>
        /// Smtp异步发送邮件
        /// </summary>
        /// <param name="mailfrom"></param>
        /// <param name="mailpwd"></param>
        /// <param name="host"></param>
        /// <param name="mail"></param>
        /// <returns></returns>
        private void SmtpSendAsync(SmtpSendEmailModel smtpSendEmailModel)
        {
            PublicClass.BaseTool.RecordLog("Comming smtpSendAsync:" + JsonConvert.SerializeObject(smtpSendEmailModel));
            SmtpClient smtp = new SmtpClient(host, Convert.ToInt32(port));
            smtp.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd);
            smtp.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
            try
            {
                BaseTool.RecordLog("smtp.SendAsync---Start");
                //smtpSendEmailModel.mail 为一个 MailMessage 对象
                smtp.SendAsync(smtpSendEmailModel.mail, smtpSendEmailModel);
                BaseTool.RecordLog("smtp.SendAsync---Success");
            }
            catch (Exception ex)
            {
                BaseTool.RecordLog("异步发送邮件报异常,异常信息为:" + ex.Message);
                BaseTool.RecordLog("异步发送邮件报异常,堆栈信息为:" + ex.StackTrace);
                StringBuilder sb = new StringBuilder();
                if (smtpSendEmailModel.mail != null && smtpSendEmailModel.mail.To.Count > 0)
                {
                    foreach (MailAddress item in smtpSendEmailModel.mail.To)
                    {
                        sb.Append(item.Address + ",");
                    }
                }
                Sys_ErrorLog se = new Sys_ErrorLog();
                se.ErrorMessage = "smtp发送邮件报异常:" + ex.GetType() + ",收件人:" + sb + "。" + ex.Message +
                                  (ex.InnerException != null ? ex.InnerException.Message : "" + ex.StackTrace) + "|" +
                                  smtpSendEmailModel.mail.Subject;
                if (ex is SmtpException)
                {
                    se.ErrorMessage += ((SmtpException)ex).StatusCode;
                    SmtpException smtpException = (SmtpException)ex;
                    string exMessage = smtpException.Message;
                    string exStackTrace = smtpException.StackTrace;
                    SmtpStatusCode exStatusCode = smtpException.StatusCode;
                    log.Info("发送异常,Message:" + exMessage);
                    log.Info("发送异常,StackTrace:" + exStackTrace);
                    log.Info("发送异常,StatusCode:" + exStatusCode);
                }
                se.UpdateTime = DateTime.Now;
                se.Memo = JsonConvert.SerializeObject(smtpSendEmailModel.mail.To);
                se.Save();
            }
            BaseTool.RecordLog("smtp.SendAsync---Finish");
        }

        /// <summary>
        /// 异步方式发送电子邮件 回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            try
            {
                PublicClass.BaseTool.RecordLog("Comming SendCompletedCallback AsyncCompletedEventArgs e is:" + JsonConvert.SerializeObject(e));
                SmtpSendEmailModel data = (SmtpSendEmailModel)e.UserState;
                StringBuilder sb = new StringBuilder();
                if (data != null && data.mail.To.Count > 0)
                {
                    foreach (MailAddress item in data.mail.To)
                    {
                        sb.Append(item.Address + ",");
                    }
                }
                Email email = new Email();
                email.MailFrom = data.mail.From.Address;
                email.MailTo = sb.ToString();
                email.Subject = data.mail.Subject;
                email.Body = data.mail.Body;
                email.SendTime = DateTime.Now;
                PublicClass.BaseTool.RecordLog("Email is:" + JsonConvert.SerializeObject(email));
                if (e.Cancelled)
                {
                    log.Info("回调,发送失败,中途退出。");
                    email.IsOK = 0;
                    email.Save();
                }
                if (e.Error != null)
                {
                    log.Info("回调,异常。");
                    Exception ex = e.Error;
                    StringBuilder sbs = new StringBuilder();
                    if (data.mail != null && data.mail.To.Count > 0)
                    {
                        foreach (MailAddress item in data.mail.To)
                        {
                            sbs.Append(item.Address + ",");
                        }
                    }
                    Sys_ErrorLog se = new Sys_ErrorLog();
                    se.ErrorMessage = "smtp发送邮件报异常:" + ex.GetType() + ",收件人:" + sbs + "。" + ex.Message +
                                      (ex.InnerException != null ? ex.InnerException.Message : "" + ex.StackTrace) + "|" +
                                      data.mail.Subject;
                    if (ex is SmtpException)
                    {
                        se.ErrorMessage += ((SmtpException)ex).StatusCode;
                        SmtpException smtpException = (SmtpException)ex;
                        string exMessage = smtpException.Message;
                        string exStackTrace = smtpException.StackTrace;
                        SmtpStatusCode exStatusCode = smtpException.StatusCode;
                        log.Info("回调,Message:" + exMessage);
                        log.Info("回调,StackTrace:" + exStackTrace);
                        log.Info("回调,StatusCode:" + exStatusCode);
                    }
                    se.UpdateTime = DateTime.Now;
                    se.Save();
                    email.IsOK = 0;
                    email.Save();
                    //同步再发送一次
                    SmtpSend(data);
                }
                else
                {
                    email.IsOK = 1;
                    email.Save();
                }
            }
            catch (Exception ex)
            {
                log.Info("回调异常,异常信息为:" + ex.Message);
                log.Info("回调异常,堆栈信息为:" + ex.StackTrace);
            }
            finally
            {
                log.Info("回调_finish");
            }
        }
Naylor的主页 Naylor | 初学一级 | 园豆:13
提问于:2019-10-23 15:03
< >
分享
所有回答(1)
0

信息量太少,只能看到SmtpException,
可能的原因:一、配置文件中缺少spring.mail.properties.smtp. ssl.enable=true;二、邮件设置中没把smtp服务没开启;三、端口冲突

無腳鳥 | 园豆:645 (小虾三级) | 2019-10-23 15:26

吧代码贴出来了,ssl 已经添加上去了。 明天再看看是否还有发送失败的情况。

支持(0) 反对(0) Naylor | 园豆:13 (初学一级) | 2019-10-24 18:04

@Naylor: 好的

支持(0) 反对(0) 無腳鳥 | 园豆:645 (小虾三级) | 2019-10-24 18:05
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册