使用C#发送ProtonMail电子邮件

5

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

"ApplicationEmail": {
    "EmailAddress": "template@gmail.com",
    "Password": "temppassword",
    "SmtpClient": "smtp.google.com",
    "Port": "587",
    "SSL": "true"
  }

现在我正在尝试切换到ProtonMail服务器,我在那里有一个带有自定义域名的账户,最终,我尝试按照以下方式重新配置设置:

 "ApplicationEmail": {
    "EmailAddress": "administration@mydomain.de",
    "Password": "randompassword",
    "SmtpClient": "smtp.protonmail.com",
    "Port": "587",
    "SSL": "true"
  }

当然这行代码是无效的,当我尝试发送邮件时出现了这个错误。
   System.Net.Mail.SmtpException: Failure sending mail.
   System.Net.Sockets.SocketException (11001): No such host is known.

我到处寻找解决方案,唯一的方法是安装在后台运行并加密和解密电子邮件的ProtonBridge,但那似乎不是一个选项,因为我无法将其集成到Azure App Service中,因为它类似于PaaS而不是Azure虚拟机。如果我错了,请不要苛刻评判,因为我对C#、Azure相对较新 :)

3个回答

5
我使用C#发送Proton电子邮件的方式。免费版本无法使用,您需要升级至Plus及以上版本。
我假设您至少拥有Plus版本,然后安装ProtonBridge。
使用您的账户电子邮件和密码添加您的账户:

ProtonBridge Add account

完成后,关键在这里,点击邮件配置(如上图所示):

ProtonBridge Mailbox configuration

你的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;

您的使用:

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);

而这两种方法:

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"
    };
}

注意:在我的情况下,我为ValidateCertificate返回true,但您可能需要查看以下详细信息:无法建立 SSL/TLS 安全信道的信任关系 -- SOAP

4

ProtonMail没有公开的SMTP服务器,您需要设置ProtonBridge。


1
这个错误意味着主机smtp.protonmail.com不存在。
根据我在知识库中看到的,是的,您似乎是正确的,需要设置ProtonMail桥接程序才能通过SMTP发送ProtonMail帐户的邮件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接