“An attempt was made to access a socket in a way forbidden by its access permissions” while using SMTP

后端 未结 7 1735
灰色年华
灰色年华 2020-11-29 07:19

I am trying to send an SMTP email when certain values in database crosses its threshold value.

I have already allowed ports 25,587 and 465 in the Windows firewall an

相关标签:
7条回答
  • 2020-11-29 07:52

    Please confirm that your firewall is allowing outbound traffic and that you are not being blocked by antivirus software.

    I received the same issue and the culprit was antivirus software.

    0 讨论(0)
  • 2020-11-29 07:58

    I got this error:

    System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions
    

    when the port was used by another program.

    0 讨论(0)
  • 2020-11-29 08:03

    Ok, so very important to realize the implications here.

    Docs say that SSL over 465 is NOT supported in SmtpClient.

    Seems like you have no choice but to use STARTTLS which may not be supported by your mail host. You may have to use a different library if your host requires use of SSL over 465.

    Quoted from http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl(v=vs.110).aspx

    The SmtpClient class only supports the SMTP Service Extension for Secure SMTP over Transport Layer Security as defined in RFC 3207. In this mode, the SMTP session begins on an unencrypted channel, then a STARTTLS command is issued by the client to the server to switch to secure communication using SSL. See RFC 3207 published by the Internet Engineering Task Force (IETF) for more information.

    An alternate connection method is where an SSL session is established up front before any protocol commands are sent. This connection method is sometimes called SMTP/SSL, SMTP over SSL, or SMTPS and by default uses port 465. This alternate connection method using SSL is not currently supported.

    0 讨论(0)
  • 2020-11-29 08:03

    If the other answers don't work you can check if something else is using the port with netstat:

    netstat -ano | findstr <your port number>

    If nothing is already using it, the port might be excluded, try this command to see if the range is blocked by something else:

    netsh interface ipv4 show excludedportrange protocol=tcp

    0 讨论(0)
  • 2020-11-29 08:03

    Do this if you are using GoDaddy, I'm using Lets Encrypt SSL if you want you can get it.

    Here is the code - The code is in asp.net core 2.0 but should work in above versions.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using MailKit.Net.Smtp;
    using MimeKit;
    
    namespace UnityAssets.Website.Services
    {
        public class EmailSender : IEmailSender
        {
            public async Task SendEmailAsync(string toEmailAddress, string subject, string htmlMessage)
            {
                var email = new MimeMessage();
                email.From.Add(new MailboxAddress("Application Name", "applicationId@gmail.com"));
                email.To.Add(new MailboxAddress(toEmailAddress, toEmailAddress));
                email.Subject = subject;
    
                var body = new BodyBuilder
                {
                    HtmlBody = htmlMessage
                };
    
                email.Body = body.ToMessageBody();
    
                using (var client = new SmtpClient())
                {
                    //provider specific settings
                    await client.ConnectAsync("smtp.gmail.com", 465, true).ConfigureAwait(false);
                    await client.AuthenticateAsync("youremailid@gmail.com", "sketchunity").ConfigureAwait(false);
    
                    await client.SendAsync(email).ConfigureAwait(false);
                    await client.DisconnectAsync(true).ConfigureAwait(false);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 08:06

    I had a same issue. It was working fine on the local machine but it had issues on the server. I have changed the SMTP setting. It works fine for me.

    If you're using GoDaddy Plesk Hosting, use the following SMTP details.

    Host = relay-hosting.secureserver.net
    Port = 25 
    
    0 讨论(0)
提交回复
热议问题