Sending email from specific server gives socket forbidden error

不羁岁月 提交于 2019-12-12 04:31:47

问题


I get this error An attempt was made to access a socket in a way forbidden by its access permissions xxx.xxx.xxx.xxx:587 when sending email from a godaddy server.

I have seen many questions like this but what's unique here is that, this code works on my local computer. It also works on my other Go daddy hosting Server.

This original server has TLS 1.0, because i needed TLS 1.2, I purchased a deluxe hosting plan and moved my code to this new server, then i start getting this error. I've searched everywhere and used every combination of port 587, 465, 25 along with ssl = false or true.

Any ideas please?

        using (SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587)) 
        {

            MailMessage mail = new MailMessage();
           smtpClient.UseDefaultCredentials = false;

           smtpClient.Credentials = new NetworkCredential("********@gmail.com", "********");
            smtpClient.EnableSsl = true;
            string fromEmail = "********@gmail.com";
            mail.From = new MailAddress(fromEmail, "System");
            mail.To.Add(new MailAddress(toEmail));
            mail.Body = body.ToString();
            mail.Subject = subject;               
            smtpClient.Send(mail);
        }

回答1:


Sounds like a firewall or AV or other port blocking software preventing outbound connections to port 587. Check your server config and look in the windows event log as there might be an entry in there indicating who did the blocking.




回答2:


Try to use port 2525 for 587, 465, 25. Some cloud providers disable all outbound traffic from 587, 465, 25 ports.




回答3:


It seems like Godaddy is blocking emails from its servers when you use an outside smtp like smtp.gmail.com. At least that seems like the case with this Plesk Hosting Account. The other Economy Hosting works well with Gmail smtp. Also, the emails will only send from Godaddy server, running the code locally on Visual studio gave an error. I changed my code to this:

  using (SmtpClient smtpClient = new SmtpClient("relay-hosting.secureserver.net", 25))
            {

                MailMessage mail = new MailMessage();
                smtpClient.Credentials = new NetworkCredential("yourdomain@yourdomain.com", "****");
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                string fromEmail = "yourdomain@yourdomain.com";
                mail.From = new MailAddress(fromEmail, "Name");
                mail.To.Add(new MailAddress(toEmail));
                mail.Body = body.ToString();
                mail.Subject = subject;
               smtpClient.Send(mail);
            }


来源:https://stackoverflow.com/questions/43161317/sending-email-from-specific-server-gives-socket-forbidden-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!