Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. email sender program : Unable to connect to the remote server

email sender program : Unable to connect to the remote server

Scheduled Pinned Locked Moved C#
sysadmincsharpcomdata-structuresdebugging
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bigz_1000
    wrote on last edited by
    #1

    Hi there, I write a program trying to send email via my gmail account, here is the code:

    MailMessage message = new MailMessage();
    MailAddress sender = new MailAddress("username@gmail.com");

            message.Body = "just for test";
            message.From = sender;
            foreach (String strEmail in emailList)
            {
                MailAddress reciever = new MailAddress(strEmail);
                message.Bcc.Add(reciever);
            }
                
            message.Subject = "test";
            message.SubjectEncoding = Encoding.UTF8;
            message.BodyEncoding = Encoding.UTF8;
    
    
            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
            smtpClient.EnableSsl = true;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = new NetworkCredential("username@gmail.com","password");
            
                //  smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                try
                {
                    smtpClient.Send(message);
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    throw new Exception(ex.Message);
                }
                finally
                {
                    message.Dispose();
                }
    

    but I have an exception in the line :"smtpClient.Send(message);" , here is it:

    System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions 74.125.43.108:587
    at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
    at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
    --- End of inner exception stack trace ---
    at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout)
    at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback)
    at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate a

    Q D 2 Replies Last reply
    0
    • B bigz_1000

      Hi there, I write a program trying to send email via my gmail account, here is the code:

      MailMessage message = new MailMessage();
      MailAddress sender = new MailAddress("username@gmail.com");

              message.Body = "just for test";
              message.From = sender;
              foreach (String strEmail in emailList)
              {
                  MailAddress reciever = new MailAddress(strEmail);
                  message.Bcc.Add(reciever);
              }
                  
              message.Subject = "test";
              message.SubjectEncoding = Encoding.UTF8;
              message.BodyEncoding = Encoding.UTF8;
      
      
              SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
              smtpClient.EnableSsl = true;
              smtpClient.UseDefaultCredentials = false;
              smtpClient.Credentials = new NetworkCredential("username@gmail.com","password");
              
                  //  smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                  try
                  {
                      smtpClient.Send(message);
      
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show(ex.ToString());
                      throw new Exception(ex.Message);
                  }
                  finally
                  {
                      message.Dispose();
                  }
      

      but I have an exception in the line :"smtpClient.Send(message);" , here is it:

      System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions 74.125.43.108:587
      at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
      at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
      --- End of inner exception stack trace ---
      at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout)
      at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback)
      at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate a

      Q Offline
      Q Offline
      Qendro
      wrote on last edited by
      #2

      Try this:

      private void SendEmail(string from, string to, string subject, string body)
      {
      SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
      client.DeliveryMethod = SmtpDeliveryMethod.Network;
      client.Credentials = new NetworkCredential("emailaddress", "password");
      client.EnableSsl = true;

           string\[\] emails = to.Split(';');
           foreach(string s in emails)
           {
               if(s != string.Empty)
                    client.Send(from, s, subject, body);
           }
      

      }

      Since i didn't watch where is your mistake, I had the method already written and just paste it here ;) Hope it will work for U too

      Qendro

      B 1 Reply Last reply
      0
      • Q Qendro

        Try this:

        private void SendEmail(string from, string to, string subject, string body)
        {
        SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.Credentials = new NetworkCredential("emailaddress", "password");
        client.EnableSsl = true;

             string\[\] emails = to.Split(';');
             foreach(string s in emails)
             {
                 if(s != string.Empty)
                      client.Send(from, s, subject, body);
             }
        

        }

        Since i didn't watch where is your mistake, I had the method already written and just paste it here ;) Hope it will work for U too

        Qendro

        B Offline
        B Offline
        bigz_1000
        wrote on last edited by
        #3

        Don't work, same exception! but thanks anyway

        1 Reply Last reply
        0
        • B bigz_1000

          Hi there, I write a program trying to send email via my gmail account, here is the code:

          MailMessage message = new MailMessage();
          MailAddress sender = new MailAddress("username@gmail.com");

                  message.Body = "just for test";
                  message.From = sender;
                  foreach (String strEmail in emailList)
                  {
                      MailAddress reciever = new MailAddress(strEmail);
                      message.Bcc.Add(reciever);
                  }
                      
                  message.Subject = "test";
                  message.SubjectEncoding = Encoding.UTF8;
                  message.BodyEncoding = Encoding.UTF8;
          
          
                  SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
                  smtpClient.EnableSsl = true;
                  smtpClient.UseDefaultCredentials = false;
                  smtpClient.Credentials = new NetworkCredential("username@gmail.com","password");
                  
                      //  smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                      try
                      {
                          smtpClient.Send(message);
          
                      }
                      catch (Exception ex)
                      {
                          MessageBox.Show(ex.ToString());
                          throw new Exception(ex.Message);
                      }
                      finally
                      {
                          message.Dispose();
                      }
          

          but I have an exception in the line :"smtpClient.Send(message);" , here is it:

          System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions 74.125.43.108:587
          at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
          at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
          --- End of inner exception stack trace ---
          at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout)
          at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback)
          at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate a

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          There's nothing wrong with your code. It would appear that either your machine has a firewall that is blocking outbound port 587 or your routers or network firewall is blocking the outbound traffic.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          B 1 Reply Last reply
          0
          • D Dave Kreskowiak

            There's nothing wrong with your code. It would appear that either your machine has a firewall that is blocking outbound port 587 or your routers or network firewall is blocking the outbound traffic.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            B Offline
            B Offline
            bigz_1000
            wrote on last edited by
            #5

            I think , that is the reason.thanks ! :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups