Posted in WIN_7

Send Email from GMail, Yahoo! And Hotmail Using C# in ASP.Net and WCF

 

Send Email from GMail, Yahoo! And Hotmail Using C# in ASP.Net and WCF

In this article we are going to see how to send email using C#.

Most of the applications we need to send mail to the user here I am going to explain how you will send mail using your Gmail, Yahoo and Hotmail account.

For sending mail we need the server details like smtp host name and port to configure SMTP.  See the list of server details for sending mail.

Name Host name Port
Gmail smtp.gmail.com 587
Yahoo! smtp.mail.yahoo.com 587
Hotmail smtp.live.com 587

Create new asp.net or WCF application according to your requirement. Then go to your code behind page and write the following code for example using Gmail.

public void SendEmail()

{

try

{

SmtpClient clientSMTP = new SmtpClient(“smtp.gmail.com”, 587);

 

clientSMTP.EnableSsl = true;

clientSMTP.DeliveryMethod = SmtpDeliveryMethod.Network;

clientSMTP.UseDefaultCredentials = false;

clientSMTP.Credentials = new NetworkCredential(“yourgmailid@gmail.com”,”password”);

using (MailMessage message = new MailMessage())

{

message.From = new MailAddress(“yourgmailid @gmail.com”);

message.Subject = “Subject”;

message.IsBodyHtml = true;

message.To.Add(“toemailid@gmail.com”);

message.Body=”Hi ” + “Suresh” + “!\n” +

“Welcome to C# Corner!” +

“Please <a href='” + ” https://windowsapptutorials.wordpress.com/ ” + “‘>click</a> here to activate your account. \nThanks!”;

clientSMTP.Send(message);

}

}

catch(Exception ex)

{

 

}

finally

{

 

}}

You can send email with attachment also using Attachment property. If you need to CC to someone or BC to someone use the following code

message.Cc.Add(“ccmailID”);

message.Bc.Add(“bcmailID”);

 

 

 

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s