8

Possible Duplicate:
GMail SMTP via C# .Net errors on all ports

I am getting the following error when I try to send an email in my C# program. I am using Visual Studio 2008 on windows 7. I would paste my code first and then the error:

class email_log_files
    {

          private string login_username = "my_gmail_id";
          private string login_password = "my_gmail_password";

          public void send_email()
          {
              string src_address = "my_gmail_id@gmail.com";
              string dest_address = "my_destination_id@xyz.edu";


              try
              {
                  MailMessage email_msg = new MailMessage();

                  SmtpClient email_client = new SmtpClient();
                  email_msg.From = new MailAddress(src_address);
                  email_msg.Sender = new MailAddress(src_address);
                  email_msg.ReplyTo = new MailAddress(src_address);
                  email_msg.To.Add(dest_address);
                  email_msg.Subject = "Test";
                  email_msg.Body = "Body of the message";
                  NetworkCredential credentials = new NetworkCredential(login_username, login_password);


                  email_client.Credentials = credentials;
                  email_client.Host = "smtp.gmail.com";
                  email_client.Port = 465;
                  email_client.EnableSsl = true;


                  email_client.Send(email_msg);
                  Console.WriteLine("Message Sent Successfully!!");
                  Console.ReadLine();

            }

        }
    }

And the error message is as follows:

The operation has timed out.

Why is it always timing out? I am sure that I have the correct smtp server address and port number for gmail as I have configured my outlook with the same. Any help or ideas?

After changing the port to 587 the error is as follows. I just disabled my firewall to see if that was the problem and it was NOT. The new error (for port 587):

Thats the error for me when I change the port to 587:

Failure sending mail.

System.Net.WebException: Unable to connect to the remote server ---> System.Net.
Sockets.SocketException: No connection could be made because the target machine
actively refused it 74.125.113.109:587

   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
ss socketAddress)

   at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)

   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Sock
et 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 ow
ner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket
6, 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, GeneralAsyncD
elegate 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)
System

System.Collections.ListDictionaryInternal

Thanks, VP

Community
  • 1
  • 1
zack
  • 6,395
  • 14
  • 49
  • 61
  • 1
    There's no such thing as C#.NET. C# is the programming language, .NET is the platform. – John Saunders Apr 14 '10 at 19:46
  • 13
    Thank you. Did you get time to focus on my actual question here? – zack Apr 14 '10 at 19:52
  • I think Scott is correct, gmail seems to respond to ports 25 and 587. – Hans Olsson Apr 14 '10 at 19:58
  • Odds are a timeout in this scenario is because either the hostname or port is wrong, or you have a firewall or other security/network blocking access. – Joe Apr 14 '10 at 19:58
  • Dupe: http://stackoverflow.com/questions/1082216/gmail-smtp-via-c-net-errors-on-all-ports – bzlm Apr 14 '10 at 20:09
  • @ho: Please see the eerror Ive uploaded for port 587 Thanks, VP – zack Apr 14 '10 at 20:17
  • @VP: if you were talking about me on Apr 14, at 19:52, then 1) Use "@user" notation and the user will be informed of the comment. 2) No, I didn't have time to focus on your question. – John Saunders Apr 21 '10 at 01:17
  • System.Net.NetworkCredential temp_Credential = new System.Net.NetworkCredential(from_Mail_Address, password); client.UseDefaultCredentials = false; client.Credentials = temp_Credential; This is the sequence you have to follow. – prabhakaran Aug 10 '12 at 11:13

5 Answers5

5

Its your Port... Gmail requires port 587.

If that doesn't work, I can just give you my Email Class.

EDIT:

   private static void SendEmailMessageGmail(System.Net.Mail.MailMessage message)
    {
        message.IsBodyHtml = true;
        message.BodyEncoding = Encoding.UTF8;
        System.Net.NetworkCredential cred = new System.Net.NetworkCredential(EmailUsername, EmailPassword);
        SmtpClient smtp = new SmtpClient("smtp.gmail.com");
        smtp.UseDefaultCredentials = false;
        smtp.EnableSsl = true;
        smtp.Credentials = cred;
        smtp.Port = 587;
        smtp.Send(message);
    }

EDIT: Add this to your code: email_client.UseDefaultCredentials = false;

SpoiledTechie.com
  • 9,951
  • 19
  • 73
  • 98
  • lemme change it to 587 right now and check if it works.. – zack Apr 14 '10 at 19:53
  • I agree that the port needs to be updated to 587, i missed that in my initial response. I would still take everything out to the web.config file if it is truely a web application. http://mail.google.com/support/bin/answer.py?hl=en&answer=13287 – Robert Williams Apr 14 '10 at 19:58
  • having a nasty error when I change the port to 587. It says the server actively refused connection. I have sent the full error to you through your site..please check it out.. Thanks, VP – zack Apr 14 '10 at 20:05
2

Since you are referencing System.Web I assume this is a web application, although I'm not quite clear on that from your description. If this is in fact a web application, be sure that your web.config file has the following entry:

  <system.net>
    <mailSettings>
      <smtp>
        <network host="smtp.gmail.com" port="465" userName="<UID>" password="<PW>"/>
      </smtp>
    </mailSettings>
  </system.net>

This prevents you from having to specify this information within your email_log_files class

Robert Williams
  • 1,310
  • 9
  • 12
  • Please have a look at my edited post. I still have errors despite changign the port to 587. Please have a look and let me know if anything strikes you. Thanks, VP – zack Apr 14 '10 at 20:18
  • Did you try the Accepted solution that was provided in the "Dupe" stackoverflow posting provided by bzlm? http://stackoverflow.com/questions/1082216/gmail-smtp-via-c-net-errors-on-all-ports See what is returned when you run that telnet command: telnet smtp.gmail.com 587 – Robert Williams Apr 14 '10 at 21:30
  • It appears so: http://stackoverflow.com/questions/2640507/sending-email-via-gmail-in-net/2640554#2640554 (see comment) – bzlm Apr 14 '10 at 22:43
  • Well, now I'm just fishing. Any chance you have an anti virus software running on your machine that is blocking email? – Robert Williams Apr 15 '10 at 02:11
1

Not sure if this is going to be useful, but have you checked that your IP is not blocking emails from being sent from your IP? I once had a similar problem, I ended up pulling my hair out the whole weekend for nothing.

So, it's worth a check just to eliminate that. Good luck.

Helen Neely
  • 4,287
  • 7
  • 35
  • 58
0

Might be a firewall or something. I find that the best way to try it out is to just connect to the server manually.

Just open a command prompt and write:
telnet your.mail.server 25

This usually gives you a response similar to "220 your.mail.server ESMTP".

If you're on Vista o Win7 you might have to enable the built-in Telnet client first (go to "Programs and Features" and select "Turn Windows features on or off" and just select the Telnet client)

Hans Olsson
  • 51,774
  • 14
  • 88
  • 111
  • telnet smtp.gmail.com 25 does not work however telnet smtp.gmail.com 465 does work and gives me a black screen on the command prompt as if its waiting for some input..port 587 just like port 25 does not work for telnet.. Thanks, VP – zack Apr 14 '10 at 21:10
  • I get that for port 465 too, but 25 and 587 works fine for me. So I think it's a high chance that you've got a firewall of some sort or some other security program in the way that blocks port but where 465 has been opened. – Hans Olsson Apr 15 '10 at 05:19
0

Try to put full email address in NetworkCredential username

Hun1Ahpu
  • 3,169
  • 3
  • 24
  • 33
  • I tried all those combinations..doesnt work...still gives me the error – zack Apr 14 '10 at 20:09
  • 1
    This is valid if you are using GAFYD -- regular gmail may not require this, but if you have a hosted domain you need user@domain.sfx – Nate Apr 14 '10 at 20:22