You are on page 1of 3

string to = you@gmail.

com //Replace this with the Email Address to whom you


want to send the mail
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();<o:p />
<o:p> </o:p>mail.To.Add(to);
mail.From = new MailAddress(from, "One Ghost" ,
System.Text.Encoding.UTF8);<o:p />
mail.Subject = "This is a test mail" ;<o:p />
mail.SubjectEncoding = System.Text.Encoding.UTF8;<o:p />
mail.Body = "This is Email Body Text";<o:p />
mail.BodyEncoding = System.Text.Encoding.UTF8;<o:p />
mail.IsBodyHtml = true ;<o:p />
mail.Priority = MailPriority.High;<o:p />
<o:p />
SmtpClient client = new SmtpClient();<o:p />
//Add the Creddentials- use your own email id and password
client.Credentials = new System.Net.NetworkCredential(from, "Password");
<o:p />
client.Port = 587; // Gmail works on this port<o:p />
<o:p />client.Host = "smtp.gmail.com";<o:p />
client.EnableSsl = true; //Gmail works on Server Secured Layer
try<o:p />
{<o:p />
client.Send(mail);<o:p />
}<o:p />
catch (Exception ex)<o:p />
{<o:p />
Exception ex2 = ex;<o:p />
string errorMessage = string.Empty;<o:p />
while (ex2 != null)<o:p />
{<o:p />
errorMessage += ex2.ToString();<o:p />
ex2 = ex2.InnerException;<o:p />
}<o:p />
<o:p> </o:p> HttpContext.Current.Response.Write(errorMessage );<o:p />
} // end try
<pre />

________________________________________________________________________________-

public string SendMail(string toList, string from, string ccList, string


subject, string body)
{
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);
if (ccList != null && ccList != string.Empty)
message.CC.Add(ccList);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
smtpClient.Host = "smtp.gmail.com";
// We use gmail as our smtp
client
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential("Your Gmail
User Name", "Your Gmail Password");
smtpClient.Send(message);
msg = "Successful<BR>";

}
catch (Exception ex)
{
msg = ex.Message;
}
return msg;

public static Boolean SendingMail(string From, string To, string Subject,


string Body)
{
try
{
MailMessage m = new MailMessage("Uma<test@gmail.com>", To);
m.Subject = Subject;
m.Body = Body;
m.IsBodyHtml = true;
m.From = new MailAddress(From);
m.To.Add(new MailAddress(To));
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
NetworkCredential authinfo = new
NetworkCredential("test@gmail.com","password");
smtp.UseDefaultCredentials = false;
smtp.Credentials = authinfo;

smtp.Send(m);
return true;

}
catch (Exception ex)
{
return false;
}

protected void Button1_Click(object sender, EventArgs e)


{
//string path = "D:\\Test.txt";
try
{
MailMessage mail = new MailMessage();
mail.To.Add("imran3may@gmail.com");
mail.To.Add("shi01715@yahoo.com");
mail.From = new MailAddress("shishir64092@gmail.com");
mail.Subject = "Send Email by asp.net code using google or
gmail smtp server";

my .aspx file";

string Body = "Hi, I am testing Email function in asp.net" +


" using google or gmail smtp server"+
" and next time I will send you a document from
mail.Body = Body;
Attachment at = new Attachment("D:\\Text.txt");
mail.Attachments.Add(at);
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("localhost",25);
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("shishir64092@gmail.com", "abcdef");
//Or your Smtp Email ID and Password
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
smtp.Send(mail);
Label1.Text = "Mail Send...";

}
catch (Exception ex)
{
Label1.Text = ex.Message;
}

You might also like