简体   繁体   中英

send email via gmail c# error #10051 network is unreachable

I have a winforms app that sends an email via gmail. It works great, but in one case, the sending fails and I get the error above. It happens when my application is running on a computer that I access to via RDP connection. I have tried so far without success:

  1. Disable windows firewall
  2. Disable Microsoft Security Essensials.
  3. Make sure no other Anti-Virus program is installed.
  4. Added the 587 port to the outbound rull.

My code as follows: (Please note that this code is working in all cases but this)

string fromAddress = txtFrom.Text.Trim();
                string toAddress = txtTo.Text.Trim();
                string fromPassword = ePassword;
                string subject = txtSubject.Text;
                string body = txtMessage.Text;

                var smtp = new SmtpClient
                {
                    Host = "smtp.gmail.com",
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    //Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
                    Credentials = new NetworkCredential(fromAddress, fromPassword)
                };
                //using (var message = new MailMessage(fromAddress, toAddress)
                using (var message = new MailMessage()
                {
                    From = new MailAddress(fromAddress, eDisplayName),
                    Subject = subject,
                    Body = body
                })
                {
                    // Check for recipiens
                    if (txtTo.Text.Trim() != "")
                    {
                        foreach (string email in txtTo.Text.Split(';'))
                        {
                            message.To.Add(email.Trim());
                        }
                    }
                    // check for copies
                    if (txtCopy.Text.Trim() != "")
                    {
                        foreach (string email in txtCopy.Text.Split(';'))
                        {
                            message.CC.Add(email.Trim());
                        }
                    }
                    // check for blind copy
                    if (txtBlindCopy.Text.Trim() != "")
                    {
                        foreach (string email in txtBlindCopy.Text.Split(';'))
                        {
                            message.Bcc.Add(email.Trim());
                        }
                    }
                    //check for attachments
                    for (int i = 0; i < clbAttachments.Items.Count; i++)
                    {
                        if (clbAttachments.GetItemChecked(i) == true)
                        {
                            Attachment attachment = new Attachment(clbAttachments.Items[i].ToString());
                            message.Attachments.Add(attachment);
                        }
                    }

                    smtp.Send(message);
                    return true;

Found an answer. I have changed the port to 25 in my code and the message was sent! I rolled back all of my changes to the OS and double checked to make sure the port changing is the only thing that needs to be done. Hope someone will find this usefull.

Edit: Port 25 is a non-secure port. In my case, I didn't care so it solved my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM