在 C# 中使用 ProtonMail 发送电子邮件

Gic*_*ica 5 c# email smtp azure asp.net-core

我有一个在 Azure 应用服务上运行的应用程序。到目前为止,我一直在使用 Gmail SMTP 服务器发送电子邮件,并且运行良好。电子邮件服务与文档中的类似,配置如下所示:

"ApplicationEmail": {
    "EmailAddress": "template@gmail.com",
    "Password": "temppassword",
    "SmtpClient": "smtp.google.com",
    "Port": "587",
    "SSL": "true"
  }
Run Code Online (Sandbox Code Playgroud)

现在我尝试切换到 ProtonMail 服务器,我在那里有一个带有自定义域的帐户,最终,我尝试重新配置设置,如下所示:

 "ApplicationEmail": {
    "EmailAddress": "administration@mydomain.de",
    "Password": "randompassword",
    "SmtpClient": "smtp.protonmail.com",
    "Port": "587",
    "SSL": "true"
  }
Run Code Online (Sandbox Code Playgroud)

这当然不起作用,我在尝试发送电子邮件时收到此错误

   System.Net.Mail.SmtpException: Failure sending mail.
   System.Net.Sockets.SocketException (11001): No such host is known.
Run Code Online (Sandbox Code Playgroud)

我到处寻找解决方案,我发现的唯一方法是安装在后台本地运行并加密和解密电子邮件的 ProtonBridge,但这看起来不像一个选项,因为我无法将其集成到 Azure 应用服务中,因为就像 PaaS 而不是 Azure VM。如果我错了,请不要严格判断,我对 C#、Azure 还比较陌生:)

may*_*ʎɐɯ 5

我在 C# 中使用 Proton 电子邮件发送电子邮件的方式。无法使用免费版本。您至少需要升级到 Plus 及以上版本。

我假设你至少有 plus 版本,而不是安装 ProtonBridge。

使用您的帐户电子邮件和密码添加您的帐户:

ProtonBridge 添加帐户

完成后,技巧来了,点击邮箱配置(如上图所示):

ProtonBridge 邮箱配置

您的 ProtonBridge 将向您显示您的配置以及新密码,该密码与您主帐户使用的密码不同。该密码仅对安装了 ProtonBridge 的客户端有效。

您可以将这个新的用户名和密码用于本地 Outlook 客户端、其他客户端或用于编程(如 C# 中)。

这是一个工作示例:

internal static readonly string SmtpServer = "127.0.0.1";
internal static readonly string EmailAccount = "email@domain.tld";
internal static readonly string Password = "***SecretGeneratedByProtonBridge***";
internal static readonly int Port = 1025;
Run Code Online (Sandbox Code Playgroud)

您的使用情况:

ServicePointManager.ServerCertificateValidationCallback += ValidateCertificate;

var smtpServer = new SmtpClient(SmtpServer)
{
    Port = Port,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(EmailAccount, Password),
    EnableSsl = true
};

var mailMessage = CreateMessage();

smtpServer.Send(mailMessage);
Run Code Online (Sandbox Code Playgroud)

以及两种方法:

private static bool ValidateCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
    return true;
}

private static MailMessage CreateMessage()
{
    return new MailMessage
    {
        From = new MailAddress(EmailAccount),
        To =
        {
            "emailTo@domain.tld"
        },
        Subject = "System Monitor",
        IsBodyHtml = true,
        Body = "My Html message"
    };
}
Run Code Online (Sandbox Code Playgroud)

注意:在我的例子中,我为 ValidateCertificate 返回 true,但您可能需要查看此内容以获取更多详细信息: 无法建立 SSL/TLS 安全通道的信任关系 - SOAP