|
Send Email from your GMAIL account using ASP.Net and C#
Download Sample
You can use your gmail account to send emails from ASP.Net and C#, using SMTP. You need to set SSL credentials. This is not straight forward like a simple SMTP email. After searching for long hours I was able to find a solution. Use following class to send emails using your GMAIL account.
Please supply your gmail user name and password in following class where needed.
using System.Web.Mail;
using System;
public class MailSender
{
public static bool SendEmail(
string pGmailEmail,
string pGmailPassword,
string pTo,
string pSubject,
string pBody,
System.Web.Mail.MailFormat pFormat,
string pAttachmentPath)
{
try
{
System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/smtpserver",
"smtp.gmail.com");
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
"465");
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/sendusing",
"2");
//sendusing: cdoSendUsingPort, value 2, for sending the message using
//the network.
//smtpauthenticate: Specifies the mechanism used when authenticating
//to an SMTP
//service over the network. Possible values are:
//- cdoAnonymous, value 0. Do not authenticate.
//- cdoBasic, value 1. Use basic clear-text authentication.
//When using this option you have to provide the user name and password
//through the sendusername and sendpassword fields.
//- cdoNTLM, value 2. The current process security context is used to
// authenticate with the service.
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1");
//Use 0 for anonymous
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/sendusername",
pGmailEmail);
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/sendpassword",
pGmailPassword);
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
"true");
myMail.From = pGmailEmail;
myMail.To = pTo;
myMail.Subject = pSubject;
myMail.BodyFormat = pFormat;
myMail.Body = pBody;
if (pAttachmentPath.Trim() != "")
{
MailAttachment MyAttachment =
new MailAttachment(pAttachmentPath);
myMail.Attachments.Add(MyAttachment);
myMail.Priority = System.Web.Mail.MailPriority.High;
}
System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
System.Web.Mail.SmtpMail.Send(myMail);
return true;
}
catch (Exception ex)
{
throw;
}
}
}
Example usage of this class,
EmailLib.SendEmail.SendEmail("your_gmail_id",
"your_gmail_password",
"to_email@anydomain.com",
"This is email subject" ,
"This is email body",
Web.Mail.MailFormat.Text,
"Physical path to your Attachment")
Note: If you do not want to attach a file with email, supply blank string as "".
Comments/Questions
Add New Comment/QuestionHi Shabdar,
I am new to ASP.Net developement and I want to use my gmail account when sending password retrieval emails. I think the standard controls that visual web developer 2008 provide are really neat but I cant get the app to send the mail via gmail's SMTP.
How should i use your code in such a way that will allow me to send the mails that the controls generate via gmails smtp? => Gavin Lumsden (Sunday 20-Apr-08 08:38 AM) | | | Reply | | Hi Shabdar,
I want to send email using my gmail account from my laptop. It is connected to the internet, but there is no SMTP service running on it. My question is:
Do i need SMTP service to be running on my laptop to be able to use your code to send email (via gmail account) ? => vivek (Tuesday 06-May-08 09:05 AM) | | | Reply | No, you don't need SMTP service running on your laptop/computer/server. => Shabdar (Tuesday 06-May-08 12:18 PM) | | | Reply | |
| this was a fantastic article, it worked for me on the first try, however with VS 2008 it tells me some of the items are obsolete (works just find nonetheless) => jeff siegel (Saturday 10-May-08 12:39 AM) | | | Reply | | You'have just solved me a problem of 2 days - thank you! where did you find this solution?? => inna (Monday 19-May-08 12:33 PM) | | | Reply | Nice coding.... => sammy (Thursday 07-Aug-08 05:04 AM) | | | Reply | |
| Marvelous code
i got what i was looking for, thanks you are a gem => Gurpreet Singh (Friday 22-Aug-08 07:09 AM) | | | Reply | |
|
|