Sending email using smtp only works after sending via Outlook

I am trying to send an email using asp.net mvc application. The Smtp client is configured in the web.config file, for example:

<mailSettings> <smtp from=" noreply@test.sk "> <network host="mail.test.sk" defaultCredentials="false" userName=" noreply@test.sk " password="pass" port="25"/> </smtp> </mailSettings> 

WITH#:

 using (SmtpClient client = new SmtpClient()) { MailMessage message = new MailMessage(from, to, subject, body); client.Timeout = 10000; client.Send(message); return false; } 

The fact is that it does not work (I get a timeout exception) until I try to send an email using Outlook. Immediately after sending e-mail via Outlook, he successfully sends it also by my web application. Is there some special kind of authentication that outlook does that then allows me to check all emails from my IP address? What is the reason it only works after sending it via Outlook?

BTW: I am running the application on VS asp.net development server. When I deploy it to the web hosting server, it does not work (timeout). My web hosting provider told me that there is classic smtp authentication on this server (not pop3 before smtp).

Edition:

I found out when it worked, and I tried it on telnet or traced it on wirehark, the message started with:

 220 mail2.hostmaster.sk ESMTP Postfix EHLO fernet-PC 250-mail2.hostmaster.sk 250-PIPELINING 250-SIZE 104857600 250-ETRN 250-STARTTLS 250-AUTH PLAIN LOGIN 250-AUTH=PLAIN LOGIN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN ... 

Compared to when it wasnโ€™t working (just tried the day before), the whole message looked like this:

 220-mail2.hostmaster.sk ESMTP Postfix EHLO fernet-PC 250-mail2.hostmaster.sk 250-SIZE 104857600 250-ETRN 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN 

Thus, there is a difference in supported auth methods and pipeline support. I assume that System.Net.Mail.SmtpClient then does not know which authentication method it should use, and therefore it starts, and therefore I get a timeout exception.

Interestingly, in Outlook there are no problems connecting and sending emails.

I solved my problem without using their smtp server as I am running out of time. Instead, I use gmail smtp server. I created a new gmail account where I set an alias for what I need, so the email looks like it didnโ€™t appear from gmail in the first image.

+4
source share
1 answer

I fixed a similar problem on the site and ran into this Question while researching the problem.

Below is the code for Google Apps for Business;

 public class SendSMTPEmail { public static void SendText(string ToName, string ToEmail, string FromName, string FromEmail, string Subject, string Content) { MailAddress from = new MailAddress(ToEmail, ToName); MailAddress to = new MailAddress(ToEmail, ToName); var smtp = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, Credentials = new NetworkCredential(" username@googlemail.com ", "yourpassword"), Timeout = 20000 }; MailMessage message = new MailMessage() { From = from, Body = Content, Subject = Subject }; message.To.Add(to); smtp.Send(message); } } 

Does it help?

0
source

All Articles