I have an application running on Azure App Services. So far I've been using the Gmail SMTP server to send emails, and it worked fine. The Email service is similar to the one in documentation The configurations looked like this:
"ApplicationEmail": {
"EmailAddress": "template@gmail.com",
"Password": "temppassword",
"SmtpClient": "smtp.google.com",
"Port": "587",
"SSL": "true"
}
Now I'm trying to switch to ProtonMail server, I have an account there with a custom domain and eventually, I tried to reconfigure the settings like this:
"ApplicationEmail": {
"EmailAddress": "administration@mydomain.de",
"Password": "randompassword",
"SmtpClient": "smtp.protonmail.com",
"Port": "587",
"SSL": "true"
}
Which of course doesn't work and I'm getting this error when trying to send an email
System.Net.Mail.SmtpException: Failure sending mail.
System.Net.Sockets.SocketException (11001): No such host is known.
I looked for a solution everywhere, only way I found is to install ProtonBridge that runs locally in the background and encrypts and decrypts the emails but that doesn't look like an option since I can't integrate it in the Azure App Service because that is like a PaaS not an Azure VM. If I am wrong, don't judge strictly, I am relatively new to C#, Azure :)
The way I do send email using Proton email in C#. It is not possible to use the free version. You need to upgrade at least to Plus and above.
I assume you have at least plus version, than install ProtonBridge.
Add your account using your account email and password:
When you are done, here comes the trick, click on Mailbox configuration (shown in the above image):
Your ProtonBridge will show you, your configuration with a new Password which is not the same as the one you use to your main account. This password is only valid for the client that has ProtonBridge installed.
This new username and password you can use for your local outlook client, other client OR for programming, like in C#.
Here is a working example:
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;
Your usage:
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);
And the two methods:
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"
};
}
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