1
votes
protected void SendEmail(object sender, EventArgs e)
    {
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();
        try
        {
            MailAddress fromAddress = new MailAddress("fromemail", "From Me");
            MailAddress toAddress = new MailAddress("toemail", "To You");
            message.From = fromAddress;
            message.To.Add(toAddress);
            message.Subject = "Testing!";
            message.Body = "This is the body of a sample message";
            smtpClient.Host = "smtp.host.com";
            smtpClient.Credentials = new System.Net.NetworkCredential("username", "password");
            smtpClient.EnableSsl = true;
            smtpClient.Port = 587;
            smtpClient.Send(message);
            statusLabel.Text = "Email sent.";
        }
        catch (Exception ex)
        {
            statusLabel.Text = "Coudn't send the message!\n"+ex.Message;
        }
    }

The error is

"Failure sending mail."

I can not send any. What is the problem?

innerexception is

System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.39.109:587 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) --- End of inner exception stack trace --- at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message)

7
So what exception do you get (if any)?Hank
Please include the output of the entire ex.ToString(). The actual problem is probably described in an inner exception and the entire exception output is needed to diagnose.Erv Walter

7 Answers

2
votes

From the inner exception: 74.125.39.109:587 cannot be reached.

Make sure:

  1. 74.125.39.109:587 actually has a SMTP server running
  2. From the server running the code, make sure you can connect to that IP:Port (there isn't a firewall or virus scanner blocking the outgoing smtp connection attempt).

One way to test, is from that same server, open a telnet connection to 74.125.39.109:587.

3
votes

For an SmtpException, you always have to examine the inner exception through the Exception.InnerException property. Always.

3
votes

Remove the try/catch and see what happens :-)

You can test locally first:

<mailSettings>
    <smtp deliveryMethod='SpecifiedPickupDirectory'>
        <specifiedPickupDirectory pickupDirectoryLocation="c:\maildrop" />
    </smtp>
</mailSettings>

More details:

http://weblogs.asp.net/gunnarpeipman/archive/2010/05/27/asp-net-using-pickup-directory-for-outgoing-e-mails.aspx

1
votes

Try taking out all the smtp server settings and place the mailSettings in your app/web.config

Sometimes mail servers will not allow mail relaying. In this case, you will need to set up a virtual smtp server.

Relaying in IIS 6

If you are using windows server 2008, you will need to install the IIS 6 management console to configure relaying.

1
votes

Default SMPT port is 25. You are using 587, could this be the problem. Or is there a reason for using port?

1
votes

u have use 587 as the port....this is similar to gmail's smtp service port number....if ur using gmail smtp service...try use this...

host: smtp.gmail.com port: 587

in network ceredentials give ur valid gmail username and its password...

also check ur internet connectivity

0
votes

I remember i face similar problem the solution i find is:

before:

smtpClient.Send(message);

Put this line:

ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

BTW:

the port should be: smtp.Port = 25;