send email using gmail by c# - codeproject

Upload: gfgomes

Post on 11-Feb-2018

227 views

Category:

Documents


1 download

TRANSCRIPT

  • 7/23/2019 Send Email Using Gmail by C# - CodeProject

    1/3

    Articles Web Development ASP.NET Samples

    Ngo Tuong Dan, 26 Oct 2015 CPOL

    Send Email using Gmail by C#

    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.configStep 2: Send mail with SmtpClientclass

    Background

    Understand email processing

    Basic about configuration file in ASP.NET

    Declare smtp Server Through web.config File

    You can provide SMTP server address (DNS name or IP) with hostattribute, 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

    2.27 (13 votes)

    d Email using Gmail by C# - CodeProject http://www.codeproject.com/Tips/1042226/Send-Email-using-Gmail-...

    3 27/10/2015 22:20

  • 7/23/2019 Send Email Using Gmail by C# - CodeProject

    2/3

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

    stringstrHost = smtpSection.Network.Host;

    intport = smtpSection.Network.Port;

    stringstrUserName = smtpSection.Network.UserName;

    stringstrFromPass = 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 = newSmtpClient(strHost, port);

    NetworkCredential cert = newNetworkCredential(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 = newMailMessage(smtpSection.From, Email);

    msg.Subject = "This is the subject";

    msg.IsBodyHtml = true;

    msg.Body += "This is a title";

    msg.Body += "This is a sub title";

    msg.Body += "

    This is the message.

    ";

    EmailMessage help us create an email:

    smtpSection.Fromget email from config file as sender email

    Email is reciever emailisBodyHtml 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

    d Email using Gmail by C# - CodeProject http://www.codeproject.com/Tips/1042226/Send-Email-using-Gmail-...

    3 27/10/2015 22:20

  • 7/23/2019 Send Email Using Gmail by C# - CodeProject

    3/3

    Permalink| Advertise | Privacy| Terms of Use| Mobile

    Web01 | 2.8.151024.1 | Last Updated 26 Oct 2015Selecione o idioma

    Article Copyright 2015 by Ngo Tuong Dan

    Everything else Copyright CodeProject, 1999-2015

    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 messageshave been posted for this article Visit http://www.codeproject.com/Tips/1042226/Send-Email-using-

    Gmail-by-Csharpto post and view comments on this article, or click hereto get a print view with messages.

    Code for fun -> ^.^