Вы находитесь на странице: 1из 3

Send Email using Gmail by C# - CodeProject

1 of 3

http://www.codeproject.com/Tips/1042226/Send-Email-using-Gmail-...

Articles Web Development ASP.NET Samples

Send Email using Gmail by C#


Ngo Tuong Dan, 26 Oct 2015

CPOL

2.27 (13 votes)


Send Email using Gmail by C#

Download SendMail-noexe.zip - 61.4 KB


Download SendMail.zip - 81.6 KB

Introduction
Now, gmail is a free webmail also free SMTP server for developer integrated into their app. In this post, we are using C# to send
an email from our app within two steps:
Step 1: Configure mail server via web.config
Step 2: Send mail with SmtpClient class

Background
Understand email processing
Basic about configuration file in ASP.NET

Declare smtp Server Through web.config File


<system.net>
<mailSettings>
<smtp from="ngotuongdan@gmail.com">
<network host="smtp.gmail.com"
password="your mail password" port="587" userName="your user" />
</smtp>
</mailSettings>
</system.net>
You can provide SMTP server address (DNS name or IP) with host attribute, in this tip i'm using google mail.
To authenticate with google mail, you must declare your account information as: username and password at password and
username attribute.

Get smtp Server from config File

27/10/2015 22:20

Send Email using Gmail by C# - CodeProject

2 of 3

http://www.codeproject.com/Tips/1042226/Send-Email-using-Gmail-...

var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");


string strHost = smtpSection.Network.Host;
int port = smtpSection.Network.Port;
string strUserName = smtpSection.Network.UserName;
string strFromPass = smtpSection.Network.Password;
ConfigurationManager.GetSection("system.net/mailSettings/smtp") help us move to smtp tag and cast to SmtpSection object. The
Network property of smtpSection provide information between network tag.

Provide Authenticatioin Information to Gmail Server


SmtpClient smtp = new SmtpClient(strHost, port);
NetworkCredential cert = new NetworkCredential(strUserName, strFromPass);
smtp.Credentials = cert;
smtp.EnableSsl = true;
Now, we send user/pass to gmail server through SmtpClient and NetworkCredential as above.

Create Mail Content


MailMessage msg = new MailMessage(smtpSection.From, Email);
msg.Subject = "This is the subject";
msg.IsBodyHtml = true;
msg.Body += "<h1>This is a title</h1>";
msg.Body += "<h3>This is a sub title</h3>";
msg.Body += "<p>This is the message.</p>";
EmailMessage help us create an email:
smtpSection.From get email from config file as sender email
Email is reciever email
isBodyHtml format your mail as html code, this help your mail can view as webpage in reading mode

Send Mail
smtp.Send(msg);

Conclusion
Sending email becomes more easy with C# and Gmail. Thanks Google

License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share
About the Author

27/10/2015 22:20

Send Email using Gmail by C# - CodeProject

3 of 3

http://www.codeproject.com/Tips/1042226/Send-Email-using-Gmail-...

Ngo Tuong Dan


Instructor / Trainer Can Tho University Software Center
Vietnam
Code for fun -> ^.^ <-

You may also be interested in...


Using Gmail Account to Send
Emails With Attachment

Send Emails in ASP.NET using


Gmail Credentials

Send Email from Yahoo!, GMail,


Hotmail (C#)

Embedding Analytics into a


Database Using JMSL

Using Gmail Account to Send


Emails With Attachment

Add HTML5 Document Viewer


to ASP.NET MVC 5 Project

Comments and Discussions


13 messages have been posted for this article Visit http://www.codeproject.com/Tips/1042226/Send-Email-usingGmail-by-Csharp to post and view comments on this article, or click here to get a print view with messages.
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151024.1 | Last Updated 26 Oct 2015

Selecione o idioma

Article Copyright 2015 by Ngo Tuong Dan


Everything else Copyright CodeProject, 1999-2015

27/10/2015 22:20

Вам также может понравиться