4

I'm trying to send email using asp.net mvc application. Smtp client is configured in web.config e.g.:

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

C#:

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

The thing is, it doesn't work (I get timeout exception) until I try to send email using outlook. Right after sending email by outlook it successfully sends it also by my web application. Is there some special kind of authentication which outlook is doing which then allows all emails from my IP to be authenticated?? What is the reason it works only after sending it by outlook?

BTW: I'm running the application in VS asp.net development server. When I deploy it to webhosting server, it doesn't work (timeout). My webhosting provider told me there is a classic smtp authentication on that server (not pop3 before smtp).

EDITED:

I figured out, when it worked and I tried it by telnet or traced it by wireshark, the communication started by:

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 didn't work (just tried a day before), the whole communication looked like:

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

So there is a difference in supported auth methods and support for pipelining. I guess that System.Net.Mail.SmtpClient then doesn't know which authentication method should it use and so it stucks and therefore I'm getting the timeout exception.

That's interesting that outlook has no problems to connect and send email.

I solved my problem by not using their smtp server, as I'm running out of time. I'm using gmail's smtp server instead. I created new gmail account where I set alias to what I needed, so the email looks like it didn't come from gmail on the first look.

fernet
  • 103
  • 1
  • 7
  • 1
    Do you have anti-virus running that checks outbound connections on port 25 (Norton, McAfee, etc) – Erik Philips Jan 02 '13 at 23:03
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jan 02 '13 at 23:13
  • do you have the problem both locally and on the server, or on just one of them? – Jason Jan 02 '13 at 23:17
  • Antivirus was turned off. I have the problem locally on VS dev server, IIS 7.5, and on hosted server as well, but sometimes it works on localhost and doesn't work on hosted server at the same time and vice versa. – fernet Jan 05 '13 at 14:53

1 Answers1

0

I have fixed a similar problem on a site and came across this Question while researching the issue.

The code below works 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 this help?

Gavin
  • 1,233
  • 17
  • 30