Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
'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'

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


What I have tried:

C#
public void EmailtoAdmin()
        {
            var message =  new  MimeMessage();
            message.From.Add(new MailboxAddress("my email"));
            message.To.Add(new MailboxAddress("email"));
            message.Subject = "";
            message.Body = new TextPart("plian")
            {
                Text = "abc"
            };
            using (var client =new SmtpClient())
            {
               // client.Timeout = 600000;
                client.Connect("smtp.gmail.com", 465, false);
                client.Authenticate("email", "pwd");
                client.Send(message);
                client.Disconnect(true);
               
            }
        }
Posted
Updated 26-Jun-19 8:25am
v4
Comments
F-ES Sitecore 26-Jun-19 7:01am    
google "c# send email gmail", this is one of the most frequently asked questions and all of the problems you'll encounter are already well documented.

However my advice is to not send through gmail at all, it's only going to stop working at some point.
Sinisa Hajnal 26-Jun-19 9:35am    
I realize this is probably just question code, but in general, you shouldn't have "magic numbers" in the code (in this case port number)

1 solution

Try this

MailMessage mail = new MailMessage();
mail.To.Add(to);
mail.From = new MailAddress(from);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com",587);
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(address, password);
smtp.Send(mail);
The port is set to 587 (instead of 465).
 
Share this answer
 
Comments
Member 14512875 26-Jun-19 7:10am    
Thanks,now it is working.
Sinisa Hajnal 26-Jun-19 9:36am    
You should accept this as the answer so that other can find it.
Maciej Los 26-Jun-19 14:26pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900