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

标签 c# azure email asp.net-core smtp

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

"ApplicationEmail": {
    "EmailAddress": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9cddcd4c9d5d8cddcf9ded4d8d0d597dad6d4" rel="noreferrer noopener nofollow">[email protected]</a>",
    "Password": "temppassword",
    "SmtpClient": "smtp.google.com",
    "Port": "587",
    "SSL": "true"
  }

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

 "ApplicationEmail": {
    "EmailAddress": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2746434a4e494e54535546534e4849674a5e43484a464e49094342" rel="noreferrer noopener nofollow">[email protected]</a>",
    "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 应用程序中服务,因为它就像 PaaS,而不是 Azure VM。如果我错了,请不要严格判断,我对 C#、Azure 还比较陌生:)

最佳答案

我在 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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e58088848c89a5818a88848c8bcb918981" rel="noreferrer noopener nofollow">[email protected]</a>";
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 =
        {
            "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ffaf2fef6f3cbf0dffbf0f2fef6f1b1ebf3fb" rel="noreferrer noopener nofollow">[email protected]</a>"
        },
        Subject = "System Monitor",
        IsBodyHtml = true,
        Body = "My Html message"
    };
}

Note: in my case I return true for ValidateCertificate but you might need to look at this for more details: Could not establish trust relationship for SSL/TLS secure channel -- SOAP

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

相关文章:

c# - C#/.NET 中的仅命名空间类可见性?

c# - 嵌入图像在浏览器中显示时未在 Outlook 中显示?

html - 只获取 HTML 正文,没有标签

php - 检查数据库 php 中是否已存在值

c# - 读取 excel 文件而无需将整个文件保存在服务器中

c# - UWP 新应用程序 View

Azure FTP 到实时网页

c# - 检查context.Request.Body的条件是azure api管理策略中的JArray或JObject

visual-studio - Azure 模拟器连接错误

c# - 如何在一种方法上编写不同案例的断言测试,并且在与不同的响应案例进行比较时,它们都通过了?