RSS

Sending E-mail (gmail)

25 Nov
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;

namespace email
{
class Program
{
static void Main(string[] args)
{
try
{
//The From address (Email ID)
string str_from_address = "sender_email_address";

//The Display Name
string str_name = "Test Mail";

//The To address (Email ID)
string str_to_address = "receiver_email_address";

//Create MailMessage Object
MailMessage email_msg = new MailMessage();

//Specifying From,Sender & Reply to address
email_msg.From = new MailAddress(str_from_address, str_name);
email_msg.Sender = new MailAddress(str_from_address, str_name);
email_msg.ReplyTo = new MailAddress(str_from_address, str_name);

//The To Email id
email_msg.To.Add(str_to_address);

email_msg.Subject = "My Subject";//Subject of email

email_msg.Body = "This is the body of this message";

//Create an object for SmtpClient class
SmtpClient mail_client = new SmtpClient();

//Providing Credentials (Username & password)
NetworkCredential network_cdr = new NetworkCredential();
network_cdr.UserName = str_from_address;
network_cdr.Password = "gmail_account_password";

mail_client.Credentials = network_cdr;

//Specify the SMTP Port
mail_client.Port = 587;

//Specify the name/IP address of Host
mail_client.Host = "smtp.gmail.com";

//Uses Secure Sockets Layer(SSL) to encrypt the connection
mail_client.EnableSsl = true;

//Now Send the message
mail_client.Send(email_msg);

Console.WriteLine("Email Sent Successfully");
}
catch (Exception ex)
{
//Some error occured
Console.WriteLine(ex.Message.ToString());
}

Console.ReadKey();

}
}
}


 
Advertisements
 
Leave a comment

Posted by on November 25, 2011 in C#.Net

 

Leave a Reply

Fill in your details below or click an icon to log in:

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

 
%d bloggers like this: