【问题标题】:Which do i need Sendgrid or Mailkit - to send emails from azure webjob我需要哪个 Sendgrid 或 Mailkit - 从 azure webjob 发送电子邮件
【发布时间】:2018-01-11 04:59:57
【问题描述】:

我可以使用 Mailkit 通过组织 smtp 服务器从 azure webjob 发送电子邮件吗?还是我需要使用 Sendgrid?

Mine 是一个 .net core 1.1 控制台应用程序,然后我将其托管为 azure webjob。

由于某种原因,我无法使用组织 Smtp 服务器让我的网络作业与 Mailkit 一起工作。作业成功运行,不记录任何错误。但无法发送邮件..

这是我使用 mailkit 的代码

using (var client = new SmtpClient())
{
    try { 
    // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
    client.ServerCertificateValidationCallback = (s, c, h, e) => true;


        Logger.LogInformation("Ready to connect to smtp server");
        client.Connect(Constants.SMTP_HOST, 25, false);
        Logger.LogInformation("connected to smtp server");
        // Note: since we don't have an OAuth2 token, disable
        // the XOAUTH2 authentication mechanism.
        client.AuthenticationMechanisms.Remove("XOAUTH2");

        Logger.LogInformation("Ready to send email");
        client.Send(message);

    }
    catch (Exception ex) {
        Logger.LogError("An error occurred");
        Logger.LogError(ex.StackTrace);
    }
    finally {
        client.Disconnect(true);
    }
}

【问题讨论】:

  • @TomSun 我已经迷失了方向。我会尽快发布更新。谢谢。

标签: console-application .net-core sendgrid azure-webjobs mailkit


【解决方案1】:

我可以使用 Mailkit 通过组织 smtp 服务器从 azure webjob 发送电子邮件吗?

如果您使用组织 smtp 服务器,则应确保您的电子邮件服务器策略允许这样做。

我使用 google 电子邮件在 Azure WebJob 中测试 Mailkit。它在我这边正常工作。在此之前,我为我的 gmail 帐户打开 Allow less secure apps:

以下是演示代码。

var message = new MimeMessage();
            message.From.Add(new MailboxAddress("Tom Gmail", "xx@gmail.com"));
            message.To.Add(new MailboxAddress("Tom Hotmail", "xxx@hotmail.com"));
            message.Subject = "I am a mail subject";

            message.Body = new TextPart("plain")
            {
                Text = "I am a mail body."
            };

            using (var client = new SmtpClient())
            {

                client.Connect("smtp.gmail.com", 587);


                // Note: since we don't have an OAuth2 token, disable
                // the XOAUTH2 authentication mechanism.
                client.AuthenticationMechanisms.Remove("XOAUTH2");

                // Note: only needed if the SMTP server requires authentication
                client.Authenticate("sunguiguan@gmail.com", "@WSX3edc");

                client.Send(message);
                client.Disconnect(true);
            }

            Console.WriteLine("send mail successful");
            Console.Read();

或者我需要使用 Sendgrid 吗?

在我看来,SendGrid 也是一个不错的选择,我们也可以使用 Azure 上的免费价格层。我也在我这边测试它。它也可以正常工作。我们还可以从 azure official document 获得更多详细信息。

     var apiKey ="ApiKey";//System.Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
     var client = new SendGridClient(apiKey);
     var msg = new SendGridMessage()
     {
               From = new EmailAddress("xxx@hotmail.com", "Send Grid"),
                Subject = "Hello World from the SendGrid CSharp SDK!",
                PlainTextContent = "Hello, Email!",
                HtmlContent = "<strong>Hello, Email!</strong>"
            };
            msg.AddTo(new EmailAddress("xxxx@gmail.com", "Test User"));
            client.SendEmailAsync(msg).Wait();
    }

【讨论】:

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