email sender program : Unable to connect to the remote server
-
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 -
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 aTry 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
-
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
-
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 aThere'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 -
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