Programmatically send emails using Google mail server and System.Net.M(mycsharpcorner.com)

submitted by youseflayousefla(675) 5 years, 1 month ago

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.

5 comments |category: |Views: 295

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by senfosenfo(881) 5 years, 1 month ago 0

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

Reply

posted by yesthatmcgurkyesthatmcgurk(4063) 5 years, 1 month ago 0

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!

Reply

posted by youseflayousefla(675) 5 years, 1 month ago 0

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 ;-)

Reply

posted by ashinashin(0) 4 years, 10 months ago 0

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

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

SmtpClient cl = new SmtpClient("smtp.gmail.com",587);
cl.EnableSsl = true;
cl.Credentials = new NetworkCredential("ashin007@gmail.com", pass);
cl.Send(mail);
}

exception:
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)

Reply

posted by ericeric(0) 4 years, 7 months ago 0

hi,

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) = "mail.google.com"
.Update
End With

and got the following error message:

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

any ideas ?

thanks

Reply

information Login or create an account to comment on this story