Malaika Consultants LLC

How to use Google SMTP server for sending emails from ASP.NET

by Swanand Mokashi 14. January 2011 18:47

So you (or your client) moved your email to Google Apps and now you want to use Google SMTP server to send emails from ASP.NET ? It is pretty straight forward. Read on…

  • The System.Net contains the class SmtpClient which can be used to send the email from your ASP.NET page. Let us jump in the code (C#)

SmtpClient mailSender = new SmtpClient(“smtp.gmail.com”);

You can pass the smtp server location in the SmtpClient class’ constructor.

  • Next specify the port:

mailSender.Port = 587;

  • You can send the email using Google’s SMTP only by using authentication. So we will need to send the user name and password for the account from which you want to send the email (i.e your “From” email address):

System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(emailUserName, emailPassword);

where emailUserName is obviously your email address and emailPassword its password. Next use these credentials to send the email :

mailSender.Credentials = credentials ;

  • Next tell the SMTP server NOT to use the default credentials and to use SSL

mailSender.EnableSsl = true;

mailSender.UseDefaultCredentials = false;

  • Now send the email as you would normally in ASP.NET:

MailAddress toAddress = new MailAddress(“to@domain.com”);

MailAddress fromAddress = new MailAddress(“from@domain.com”);

MailMessage message= new MailMessage(fromAddress , toAddress );

message.Subject = “Email Subject”;

message.Body = “Email Message”;

mailSender .Send(message);

Of course as always recommend putting the smtp client (“smtp.gmail.com”) , port number. emailUserName and emailPassword in some kind of configuration (ASP.NET web.config file, app.config , SQL Server database etc)

This means you need to know the email user name and password of the account that the email is sent from. I have not found any other solution than physically creating this email account and using it. If anyone has found a better solution for this, please let me know

Tags: ,

General

Comments

1/15/2011 3:50:00 AM #

How to use Google SMTP server for sending emails from ASP.NET

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com | Reply

2/22/2012 3:16:40 AM #

Well written and explains a lot. Thank you.

Google Reklam United States | Reply

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



::: | © Malaika Consultants LLC | :::

Malaika Consultants LLC

Malaika Consultants LLC is a custom software development consulting firm located in Cary (Raleigh Durham Research Triangle AKA RTP area ) North Carolina. We offer our expert consulting services in the Microsoft .NET and related technologies. Our mission is to partner with you and ensure the success of your project. We have a team in the USA that will help you with your Information Technology needs and we strive to be truly "Your Information Technology Angels"

We are offering 1 hour of free consulting -- ASP.NET, SQL Server, IIS, anything. Contact us to get your free consulting

Recent Comments

Comment RSS