I created an ASP.NET website using C# that targets .NET 4.8 using Visual Studio.
The website is hosted at Ionos (aka 1&1) on a Windows Server, but I have no access to it or to the IIS console.
From my dev workstation, I can successfully send emails using MailSender.Send(....)
define in the following code:
using System;
using System.Net;
using System.Net.Mail;
public class MailSender {
public static void Send(string server, int port, string body, string subject, string to = "xxxxxxx@gmail.com", string from = "noreply@xxxxxxxx.com") {
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
SmtpClient smtp = new SmtpClient(server, port);
smtp.Credentials = new NetworkCredential(user, pw);
smtp.EnableSsl = true;
MailMessage Msg = new MailMessage();
Msg.From = new MailAddress(from);
Msg.To.Add(to);
Msg.Subject = subject;
Msg.IsBodyHtml = true;
Msg.Body = BuildBody(body);
smtp.Send(Msg);
}
}
Nevertheless, when the exact same website is put on the host server, this fails with following error:
System.Net.Mail.SmtpException: Failure sending mail. ---> **System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed**.
at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace ---
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at MailSender.Send_New(String server, Int32 port, String body, String subject, String to, String from)
Could anyone give me a hint on what I can do to fix this?
What I tried, so far:
- Port I use is now 587, but 465 or 25 give the same result.
- I tried several SecurityProtocolType values, with no difference.
- I tried many different ways of sending (System.Web.Mail, built-in WebMail class, etc...) with same result.
- I also checked that my website actually targets .NET 4.8
- Searching, I found many articles on Google and dev forums, with many tips, but nothing seems to help in my very case
- I also contacted Ionos support, who seem to say they have no problem and that this is a dev problem, so they cannot help...
Now I definitely need help, because it's now more than 2 weeks I'm trying things with no succes :(
Thanks in advance for any help!!