2

I have looked at numerous answers in stack overflow and other places and I still cannot find a good example on how to:

in C#, Successfully send email via sparkpost smtp after STARTTLS was introduced. Documentation in Sparkpost (https://www.sparkpost.com/docs/getting-started/getting-started-sparkpost/, search for starttls) refers to the following settings:

SMTP host: smtp.sparkpostmail.com Port: 587 or 2525 Encryption: STARTTLS Username: SMTP_Injection Password:

In my current applications, in web.config, I configure sparkpost smtp like so:

        <network host="smtp.sparkpostmail.com" port="587" userName="SMTP_Injection" password="..." enableSsl="true" />

This worked fine until the start of july. And then TLS 1.0 was no longer supported. So I would like to get our email working again

But how does STARTTLS come into play ?

Guttorm
  • 21
  • 2
  • "But how does STARTTLS come into play ?" - that's the SMTP command that triggers TLS negotiation. It doesn't, AFAICS, influence the version negotiated - that's up to the versions you've enabled in Windows. – Rup Aug 01 '18 at 16:03
  • Yes. I know I can see this from the documentation. But how do I solve this codewise. I cannot find a solution to this anywhwere. And this is why I posted this request. I do, obviously want to use version 1.1. or newer. – Guttorm Aug 05 '18 at 21:09
  • Ideally I would like to add the command for STARTTLS to web.config. Any clues ? Or is this solved in other ways ? Code ? – Guttorm Aug 05 '18 at 21:12
  • I think you already have? You showed us ``. [Here's the documentation for that](https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/network/network-element-network-settings): "In this mode, the SMTP session begins on an unencrypted channel, then a STARTTLS command is issued by the client to the server to switch to secure communication using SSL." – Rup Aug 05 '18 at 21:22
  • I think you need to configure the versions of TLS that .NET will negotiate. Here's some old questions about that: https://stackoverflow.com/q/45382254/243245 https://stackoverflow.com/q/28286086/243245 – Rup Aug 05 '18 at 21:22
  • Rup. I already read the url you added 8 mins ago. The clue, I think is to force the activation of TLS 1.1 or newer. And I cannot figure out how. – Guttorm Aug 05 '18 at 21:32
  • Your final reference showed the right answer at the very bottom. Put the code System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; in Application_Start of Global.asax.cs – Guttorm Aug 05 '18 at 21:37

1 Answers1

1

I added the ServicePointManager.SecurityProtocol line to my smtp C# code and it all started working again. Hope this helps.

SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.sparkpostmail.com";
smtp.Port =  587; // 2525
smtp.EnableSsl = true;
//NOTE THE LINE BELOW
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
smtp.Credentials = new NetworkCredential("SMTP_Injection", "YOURKEY");
APOUGHER
  • 49
  • 4