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

Happy new year to all !

by Malaika Consultants LLC 30. December 2009 23:20

Happy new year to everybody from everybody at Malaika Consultants LLC.

 

It has been a great year for us -- working with our clients to solve their challenging and fun projects. We are hoping the trend continues in the upcoming yeat as well !

 

Enjoy responsibly!

Tags:

General

Welcome to our blog!

by Malaika Consultants LLC 6. November 2009 16:37

Welcome to the official blog of Malaika Consultants LLC

Tags:

Blog | General

::: | © 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