c# - 使用 Google SMTP 在 Azure Webjob 中发送电子邮件

标签 c# azure smtpclient azure-webjobs

我编写了一个简单的控制台应用程序,用于使用 WebJobs 从 Azure 发送测试电子邮件,但是虽然作业表示已成功运行,但电子邮件从未到达。该应用程序在本地运行时运行良好。

这是代码:

    static void SendMail()
    {

        var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5835213f35393134183f35393134763b3735" rel="noreferrer noopener nofollow">[email protected]</a>", "*********")
        };

        MailAddress to = new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f0849f959d91999cb09a91949482958383de9e9584de9e8a" rel="noreferrer noopener nofollow">[email protected]</a>");
        MailAddress from = new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6d0b1f020008000c04012d070c09091f081e1e43030819430317" rel="noreferrer noopener nofollow">[email protected]</a>");
        MailMessage mail = new MailMessage(from, to);

        mail.Subject = "Test Email";

        mail.Body =
            ("This is a test @\n");

        Console.WriteLine("Sending email...");
        smtp.Send(mail);

        Console.WriteLine();
        }
    }

我不确定如何在 Azure 中进行调试,因此不知道发生了什么。

谁能看出我做错了什么。

干杯, 凯文。

最佳答案

默认情况下,Webjobs 会查找 Main 函数签名作为入​​口点。它就像您的常规控制台应用程序。如果您放置任何 Console.Write 行,它也会输出。

public class Program
{
    public static void Main(string[] args)
    {
        //your code...
    }
}

转到您的WebApp Blade > 所有设置并向下滚动,您应该有一个Webjobs项目,其中加载了您的Webjob,现在双击在 LOGS 列的链接上:

enter image description here

现在,您应该能够切换输出并查看网络作业的控制台输出。

enter image description here

如果执行过程中出现任何错误,它们应该就在那里。

如果您看不到日志链接,请尝试打开W​​eb应用程序(顶部工具栏)中的“工具”部分,滚动到Kudu并打开它。

enter image description here

然后转到“工具”> Webjobs 仪表板。您的工作应该列在那里。

enter image description here

编辑

看到报错后,问题是Azure环境和你的电脑不一样。 Google 可能会根据来自未知来源的访问来阻止使用。检查您的Less secure settings并尝试关闭。如果失败,请在from a different timezone中启用登录。虽然我建议使用其他服务来发送邮件,例如 MailGunSendGrid ,甚至Amazon SES .

关于c# - 使用 Google SMTP 在 Azure Webjob 中发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35717374/

相关文章:

azure - 是否可以使用 Windows 服务应用程序创建 Azure 服务总线中继?

由于 BCC,C# SmtpClient.Send() 很慢

c# - 3 个表上令人困惑的 MySql 连接

c# - 无法卸载 NuGet 包

c# - 为什么在编译 typeModel 时某些内置类型不起作用?

python - 具有 Azure AD SSO 的 Snowflake python 连接器

azure - AzCopy 高级模式匹配/最后 x 个 blob

c# - 如何使用 OpenXML 在 powerpoint 幻灯片中插入形状

Java SMTP 邮件 - 连接超时错误

c# - 如何从 Windows Phone 8 应用程序发送电子邮件?