tags:

views:

90

answers:

4

Here is my code:

try
{
    MailMessage m = new MailMessage
        ("[email protected]",
         "[email protected]",
         "Quarterly data report.",
         "Hello, world.");

    SmtpClient client = new SmtpClient("smtp.gmail.com", 465);
    client.Credentials = new NetworkCredential("[email protected]", "password");
    client.EnableSsl = true;
    client.Send(m);
    Console.WriteLine("sent");
}
catch (InvalidOperationException ey)
{
    Console.WriteLine(ey.Message);
}
catch (SmtpFailedRecipientException sm)
{
    Console.WriteLine(sm.Message);
}
catch (SmtpException ex)
{
    Console.WriteLine(ex.Message);
}

This code produce this error at SmtpException: "Failure sending mail." The full is exception is this:

ex.ToString()   "System.Net.Mail.SmtpException: Failure sending mail. 
  ---> 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.67.109:587
  at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
  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)\r\n   at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
  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 ConsoleApplication3.Program.Main(String[] args) in c:\\users\\alan\\documents\\visual studio 2010\\Projects\\ConsoleApplication3\\ConsoleApplication3\\Program.cs:line 24" string
A: 

Try to send a simple email using this snippet:

var smtpClient = new SmtpClient("smtp.gmail.com", 587)
{
    Credentials = new NetworkCredential(
        "[email protected]", 
        "yourpassword"
    ),
    EnableSsl = true
};
smtpClient.Send("[email protected]", "[email protected]", "subject", "body");

The differences from your code is the port 587 (instead of your 465) .

Alex
I tested, but it throws the same error.Thank you anyway
Alan
A: 

Try this site first or give more information if you want help:

http://www.systemnetmail.com/

osij2is
I Still can't..
Alan
Wow. Don't be too descriptive.
osij2is
A: 

It looks like your script isn't able to contact the Gmail server on the specified port. Ensure your server can reach that address/port by telnetting to it and seeing if you get a reply.

Lerxst
A: 

A very similar situation happened to me a while back. I followed templates I had found on line for using SmtpClient in System.Net. After researching I found out that my ISP had policies in place to drop mail traffic on certain ports because they wanted people to route traffic through their own mailservers (combat spam).

My situation was complicated by the fact that the port I needed to use (for my business ymail account) were blocked by my ISP. Turns out the only mailservers I could ever connect to was gmail. So you might try using different mail providers, or talking to your ISP about mail drop/block policies and how certain ports are treated.

Here's what worked for me:

private bool sendEmailAlert(string emailAddress, string subject, string message)
        {
            try
            {
                //Construct e-mail message:
                string fromAddress = "[email protected]",
                       fromName = "Your Biz";//"Don't Reply";
                MailMessage email_msg = new MailMessage();
                email_msg.From = new MailAddress(fromAddress, fromName);
                email_msg.Sender = new MailAddress(fromAddress, fromName);
                email_msg.To.Add(emailAddress);
                email_msg.Subject = subject;
                email_msg.Body = message;
                SmtpClient mail_client = new SmtpClient();
                NetworkCredential network_cdr = new NetworkCredential();
                network_cdr.UserName = "[email protected]";
                network_cdr.Password = "password";
                mail_client.Credentials = network_cdr;
                mail_client.Port = 587;
                mail_client.Host = "smtp.gmail.com";
                mail_client.EnableSsl = true;

                //Send e-mail message;
                mail_client.Send(email_msg);
                return true;
            }
            catch (Exception ex)
            {
                StreamWriter errorFile = new StreamWriter("errorLog.txt", true);
                errorFile.WriteLine(DateTime.Now.ToString() + ": sendEmailAlert exception: " + ex.ToString());
                errorFile.Close();
                return false;
            }
        }
kmarks2
dude, your code is equals my code :U
Alan
The port is different...posted without refreshing so I didn't see the guy recommend to change your port. The advice about exploring how ISPs mess with "suspicious" or "spammy" traffic still holds, though. Also you might wanna make sure you are not a blacklisted IP address. Check out spamhaus.org and see if your IP address is on a policy block list. If it is, you can remove and retest. A lot of ISPs restrict mail traffic from hosts listed on spamhaus block lists.
kmarks2
ok, thanks, but its not the case.
Alan