【问题标题】:Why do I not get exceptions when MailKit SmtpClient.Send() fails?为什么 MailKit SmtpClient.Send() 失败时我没有收到异常?
【发布时间】:2019-06-11 14:53:20
【问题描述】:

我想在 MailKit SmtpClient.Send() 无法发送到无效电子邮件时获取并处理异常。但它不会抛出任何异常。

我故意尝试发送到无效的电子邮件,以查看是否可以捕获任何异常。 MailKit SmtpClient 在发送失败时是否根本不会抛出任何异常?如果您想知道我为什么需要这个,我想向客户端应用程序返回一条错误消息,指出某些电子邮件无效。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using MyProject.Backend.Models;
using MailKit.Net.Smtp;
using MimeKit;
using MimeKit.Text;

namespace MyProject.Backend.Helpers
{   
    public class SMTPHelper
    {

        SMTPSettings smtpSettings;

        public SMTPHelper(SMTPSettings smtpSettings)
        {
            this.smtpSettings = smtpSettings;
        }

        public List<string> Send(List<PreparedEmailModel> preparedEmails)
        {

            using (var emailClient = new SmtpClient())
            {
                var failedEmails = new List<string>();

                //The last parameter here is to use SSL (Which you should!)
                emailClient.Connect(smtpSettings.Host, 587);

                //Remove any OAuth functionality as we won't be using it. 
                emailClient.AuthenticationMechanisms.Remove("XOAUTH2");

                emailClient.Authenticate(smtpSettings.CredentialsUser, smtpSettings.CredentialsPassword);

                foreach (var preparedEmail in preparedEmails)
                {
                    var message = new MimeMessage();
                    message.From.Add(new MailboxAddress(preparedEmail.FromEmail));
                    message.To.Add(new MailboxAddress(preparedEmail.ToEmail));

                    message.Subject = preparedEmail.Subject;
                    //We will say we are sending HTML. But there are options for plaintext etc. 
                    message.Body = new TextPart(TextFormat.Html)
                    {
                        Text = preparedEmail.Body
                    };

                    try
                    {
                        emailClient.Send(message);
                    }
                    catch (Exception e)
                    {
                        failedEmails.Add(preparedEmail.ToEmail);
                    }
                }

                emailClient.Disconnect(true);
                return failedEmails;
            }

        }

    }
}

预期:MailKit SmtpClient.Send() 在发送到无效的电子邮件地址时应生成异常。

结果:即使尝试使用无效的虚构电子邮件地址,也不会引发异常。

【问题讨论】:

  • 你在测试什么无效的电子邮件地址?
  • 例如“asdfdasf@asdasf.om”。

标签: c# .net exception smtp mailkit


【解决方案1】:

假设你去邮局。你在邮箱里放了一个信封,地址不存在。

邮箱是否会回击你,并说它不存在?没有。

邮局将它发送到某个地方,他们尝试并交付它,但失败了,最终它会退回给您。

电子邮件basically 的工作方式相同。您在发送时不知道目标电子邮件地址是否合法。

因此,您的代码按预期工作。

如果你非常关心这些东西,你需要bounce processing - 但反弹可能会在几天后到达,或者永远不会到达。这不是一份小工作(我在一家专门从事这类工作的公司工作)。

【讨论】:

    【解决方案2】:

    无效地址和不存在(错误)地址之间存在差异。

    如果您尝试向不符合电子邮件地址格式的地址发送电子邮件 (XXX@AnyDomain 例如 XXX@gmail.com) MailKit 会抛出异常。

    如果您发送到正确的格式但地址错误 MailKit 不会抛出任何异常

    【讨论】:

      猜你喜欢
      • 2019-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 2013-02-10
      • 2014-07-01
      相关资源
      最近更新 更多