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(“[email protected]”);
MailAddress fromAddress = new MailAddress(“[email protected]”);
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