[英]Sending Emails With ProtonMail in c#
我有一个在 Azure 应用服务上运行的应用程序。 到目前为止,我一直在使用 Gmail SMTP 服务器发送电子邮件,并且运行良好。 Email 服务类似于文档中的服务配置如下所示:
"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"
}
哪个当然不起作用,我在尝试发送 email 时遇到此错误
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 email 发送 email 的方式。 无法使用免费版本。 您至少需要升级到 Plus 及更高版本。
我假设你至少有 plus 版本,而不是安装 ProtonBridge。
使用您的帐户 email 和密码添加您的帐户:
完成后,技巧来了,点击邮箱配置(如上图所示):
您的 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
该错误表示主机 smtp.protonmail.com 不存在。
根据我在知识库中看到的内容,是的,您似乎是正确的,因为您需要设置 ProtonMail 网桥才能通过 SMTP 为您的 ProtonMail 帐户发送邮件。
ProtonMail 没有面向公众的 SMTP 服务器,您必须设置 ProtonBridge。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.