0

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!!

quaabaam
  • 1,808
  • 1
  • 7
  • 15
  • Possibly a firewall issue – Osama Feb 03 '23 at 21:49
  • Have you configured SMTP in you IONOS account? [Enabling SMTP Authentication to Send Emails Through Web Applications](https://www.ionos.ca/help/hosting/enabling-smtp-authentication-to-send-emails-through-web-applications/) – quaabaam Feb 03 '23 at 23:05
  • This stackoverflow question seems to have a lot of information on this error. Perhaps something here can help! [SmtpException: Unable to read data from the transport connection: net_io_connectionclosed](https://stackoverflow.com/questions/20228644/smtpexception-unable-to-read-data-from-the-transport-connection-net-io-connect) – ClearlyClueless Feb 04 '23 at 03:27
  • most email systems OFTEN don't allow sending from any old IP address, and worse yet, in some cases, if you hosting on a different network then your own, then your email provider and account used for sending may well have to come from the same domain etc. This is done since if someone gets your email used for sending, then they can use your email to send millions of emails for free, and on behalf of you. In old days, you could simple setup email logon + password, and it would work. Now days? Not so much, and the origan IP address may well have to be added to your email system/server being used – Albert D. Kallal Feb 05 '23 at 14:25
  • Thanks all, but unfortunately: – Sébastien Grossetti Feb 06 '23 at 12:10
  • - @Osama this is not about FW (according to Ionos support team). and indeed trying a fake server name responds differently. – Sébastien Grossetti Feb 06 '23 at 12:10
  • - @quaabaam yes, this is the setup I started with, so I have a username & password that works there (at least it does when my website is hosted on my dev PC) – Sébastien Grossetti Feb 06 '23 at 12:11
  • - @ClearlyClueless I ran over this post before, and the many linked posts and articles, with no success :( – Sébastien Grossetti Feb 06 '23 at 12:11
  • - @AlbertD.Kallal I would be surprised my home dev PC's IP is allowed and their hosted server's isn't, but who knows... any clue where I can check this? – Sébastien Grossetti Feb 06 '23 at 12:11
  • The email server account and settings is where you would find such settings. The issue(s) center around what is called "relay" settings. As I pointed out, email servers don't (these days) allow any one in any location to use YOUR email server. However, in the "relay" options and settings for the email server/system, you can add + allow other IP or computers to to send emails. – Albert D. Kallal Feb 06 '23 at 16:28
  • For example, I have some code that emails from the web site, but while at home, if I don't connect up my VPN to work network, then the emails don't work. However, if your home computer is NOT connected to say work domain? Then yes, if emails send from your home computer (and we not talking about your email account setup), then yes, sending from the server should work. – Albert D. Kallal Feb 06 '23 at 16:43

0 Answers0