Easy way to send email from gmail account using ASP.NET 3.5
Posted by Tihomir Ivanov on 21 January 2009 05:54
Rating: 0.00
Sending e-mail from gmail account using asp.net (optionally with enabled SSL) is really easy:
in the web.config:
<system.net>
<mailSettings>
<smtp from="YOUR_ACCOUNT@gmail.com">
<network host="smtp.gmail.com"
port="587"
userName="YOUR_ACCOUNT@gmail.com"
password="YOUR_PASSWORD" />
</smtp>
</mailSettings>
</system.net>
* you can add system.net below the </connectionStrings> tag.
* change YOUR_ACCOUNT and YOUR_PASSWORD with suitable values.
here's a function to send emails:
...
using System.Net.Mail;
...
protected void SendMail(string aSendTo, string aSubject, string aBody, bool aIsHtml, bool aEnableSsl)
{
MailMessage mailMessage;
SmtpClient smtpClient;
try
{
mailMessage = new MailMessage();
mailMessage.To.Add(aSendTo);
mailMessage.Subject = aSubject;
mailMessage.Body = aBody;
mailMessage.IsBodyHtml = aIsHtml;
// send e-mail with the attached program
smtpClient = new SmtpClient();
smtpClient.EnableSsl = aEnableSsl;
smtpClient.Send(mailMessage);
}
catch (Exception ex)
{
// handle the exception
}
}
* if you want the SendMail to send e-mails to multiple mails, just change aSendTo to be List<string> and for each element call mailMessage.To.Add method
* if you want SSL to be enabled just call the function with param aEnableSsl = true.
* The examle should works for asp.net 2.0, too.
you can just copy-paste the function in your code and that's all :)
Comments:
No comments yet.