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)
//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.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 = "";

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

//Now Send the message

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



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: Logo

You are commenting using your 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: