6

I'm still getting "Failure sending mail." exception. The inner exception is "Unable to connect to the remote server" and the inner exception of that is "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". I'm pretty sure the cause of this is not the firewall setting. Does anyone know what I can do about it? Thanks.

var mail = new MailMessage("username@gmail.com", "destination@gmail.cz")
                        {
                            Subject = "Testing subject",
                            Body = "Testing body"
                        };
            try
            {
                var client = new SmtpClient("smtp.google.com", 465)
                                {
                                    EnableSsl = true,
                                    Credentials = new NetworkCredential("username@gmail.com", "password")
                                };
                client.Send(mail);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
Erhard Martin
  • 63
  • 1
  • 1
  • 3

6 Answers6

7

Here is the Google settings needed:

  • Incoming Mail (POP3) Server - requires SSL: pop.gmail.com

    Use SSL: Yes

    Port: 995

    Outgoing Mail (SMTP) Server - requires TLS3 or SSL: smtp.gmail.com (use authentication)

    Use Authentication: Yes

    Port for TLS/STARTTLS: 587

    Port for SSL: 465

    Account Name: your full email address (including @gmail.com or @your_domain.com)

    Email Address: your email address (username@gmail.com or username@your_domain.com)

    Password: your Gmail password

StefanE
  • 7,578
  • 10
  • 48
  • 75
3

I don't think 465 is the right port. Have you tried 587? And the SMTP server is smtp.gmail.com.

1

I am also working with Godaddy email server and below code worked fine for me:

Namespace:

System.Net.Mail

============

string senderID = "myemailID@mydomain.com";
string senderPassword = "123456";
string body = " Test email ";

MailMessage mail = new MailMessage();
mail.To.Add(username);
//mail.CC.Add(_cc);
mail.From = new MailAddress(senderID);
mail.Priority = MailPriority.High;
mail.Subject = "Test Email";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "relay-hosting.secureserver.net"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
     (senderID, senderPassword); // ***use valid credentials***
smtp.Port = 25;
smtp.EnableSsl = false;
smtp.Send(mail);
Anjan Kant
  • 4,090
  • 41
  • 39
0

the correct port is 587 for smtpclient class

Sina
  • 51
  • 2
0

If the hostname is correct and google supports SMTP over SSL, then it is being blocked by something and I would start checking firewalls.

ChrisBint
  • 12,773
  • 6
  • 40
  • 62
0

I don't think smtp.google.com is right server for gmail. Have you tried smtp.gmail.com on port 25?

Port 465 is for SMTP via SSL, which is not supported by the .NET SmtpClient. Instead, use port 25. The SMTP Client will use the STARTTLS feature to encrypt the communication.

Henning Krause
  • 5,302
  • 3
  • 24
  • 37