我有一个在 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 将向您显示您的配置以及新密码,该密码与您在主帐户中使用的密码不同。该密码仅对安装了 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/