Friday April 29th

Thursday April 28th

Wednesday April 27th

Programmatically send emails using Google mail server and System.Net.M

Did you know you can use Google's gmail mail server to send and receive emails programmatically? It's true, Google folks allow you to use their mail servers to send emails programmatically from your applications.


This works with any SMTP server that permits authentication. It's certainly not limited to Google.

Protip: don't forget your try/catch block. Also, asynchronous method calls are a good idea for network calls. Don't block your UI thread with this call!

Yesthat... Agreeable, whenever going to production, a good program must catch all errors; I just wanted to make the article small and simple. I also agree 100% about the asynchronous call to avoid performance bottlenecks, but then again, we're talking GOOOGLE servers here ;-)

hi..when i tried to execute this code, it throws an exception,
can u tell me why?

public string sendMail(string to, string from, string subject, string body)
MailMessage mail = new MailMessage();
mail.From = new MailAddress(from);
mail.Subject = subject;
mail.Body = body;

SmtpClient cl = new SmtpClient("",587);
cl.EnableSsl = true;
cl.Credentials = new NetworkCredential("", pass);

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) 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 asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage message) at Service.sendMail(String to, String from, String subject, String body)


i was tring to send email using ASP with "CDO.Message" by connecting to google mail server .

With cdoConfig.Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = ""
End With

and got the following error message:

CDO.Message.1 error '80040213'
The transport failed to connect to the server.

any ideas ?


Commenting on Stories is limited for now and will open up to those recommended by the community. Learn how
Loading DotNetKicks...
brought to you by the Kicks Network