Simplest Way of Sending Email


try
{
SmtpClient smtpMailObj = new SmtpClient();
//eg:localhost, 192.168.0.x, replace
//with your server name
smtpMailObj.Host = "myMailServer";
smtpMailObj.Send(txtFrom.Text, txtTo.Text,
txtSubject.Text, txtComment.Text);
Response.Write("Your Message has been
sent successfully"
);
}
catch (Exception ex)
{
Response.Write("Message Delivery Fails");
}

Sending Email with MailMessage Object


MailMessage Mail = new MailMessage();

MailAddress ma = new MailAddress(txtFrom.Text,txtName.Text);
Mail.From = ma;
Mail.To.Add(txtTo.Text);

Mail.Subject = txtSubject.Text;

Mail.Body = txtComment.Text;

try
{
SmtpClient smtpMailObj = new SmtpClient();
//eg:localhost, 192.168.0.x, replace with
//your server name
smtpMailObj.Host = "myMailServer";
smtpMailObj.Send(Mail);
Response.Write("Your Message has been sent
successfully"
);
}
catch (Exception ex)
{
Response.Write("Message Delivery Fails");
}

Sending Email with CC and BCC options


MailMessage Mail = new MailMessage();

MailAddress ma = new MailAddress(txtFrom.Text,txtName.Text);
Mail.From = ma;
Mail.To.Add(txtTo.Text);


if(txtCC.Text.Trim().Length != 0)
Mail.CC.Add(txtCC.Text);

if(txtBCC.Text.Trim().Length != 0)
Mail.Bcc.Add(txtBCC.Text);


Mail.Subject = txtSubject.Text;

Mail.Body = txtComment.Text;

try
{
SmtpClient smtpMailObj = new SmtpClient();
//eg:localhost, 192.168.0.x, replace with your server name
smtpMailObj.Host = "myMailServer";
smtpMailObj.Send(Mail);
Response.Write("Your Message has been sent successfully");
}
catch (Exception ex)
{
Response.Write("Message Delivery Fails");
}

Sending Email with Reply-To options


MailMessage Mail = new MailMessage();

MailAddress ma = new MailAddress(txtFrom.Text,txtName.Text);
Mail.From = ma;
Mail.To.Add(txtTo.Text);

if(txtCC.Text.Trim().Length != 0)
Mail.CC.Add(txtCC.Text);

if(txtBCC.Text.Trim().Length != 0)
Mail.Bcc.Add(txtBCC.Text);

Mail.Subject = txtSubject.Text;

Mail.Body = txtComment.Text;


//Setting Reply address.
if (txtReplyTo.Text.Trim().Length != 0)
{
Mail.Headers.Add("Reply-To", txtReplyTo.Text);
}
else
{
Mail.Headers.Add("Reply-To", txtFrom.Text);
}



try
{
SmtpClient smtpMailObj = new SmtpClient();
//eg:localhost, 192.168.0.x, replace with your server name
smtpMailObj.Host = "myMailServer";
smtpMailObj.Send(Mail);
Response.Write("Your Message has been sent successfully");
}
catch (Exception ex)
{
Response.Write("Message Delivery Fails");
}

Sending Email with HTML output options


MailMessage Mail = new MailMessage();

MailAddress ma = new MailAddress(txtFrom.Text,txtName.Text);
Mail.From = ma;
Mail.To.Add(txtTo.Text);

if(txtCC.Text.Trim().Length != 0)
Mail.CC.Add(txtCC.Text);

if(txtBCC.Text.Trim().Length != 0)
Mail.Bcc.Add(txtBCC.Text);

Mail.Subject = txtSubject.Text;


//Note: When you make "IsBodyHtml" to true make
//ValidateRequest="false" in page derective
//As to make HTML content passed.
Mail.IsBodyHtml = true;
Mail.Body = txtComment.Text;


//Setting Reply address.
if (txtReplyTo.Text.Trim().Length != 0)
{
Mail.Headers.Add("Reply-To", txtReplyTo.Text);
}
else
{
Mail.Headers.Add("Reply-To", txtFrom.Text);
}


try
{
SmtpClient smtpMailObj = new SmtpClient();
//eg:localhost, 192.168.0.x, replace with your server name
smtpMailObj.Host = "myMailServer";
smtpMailObj.Send(Mail);
Response.Write("Your Message has been sent successfully");
}
catch (Exception ex)
{
Response.Write("Message Delivery Fails");
}

Sending Email with Attachment facility




MailMessage Mail = new MailMessage();

MailAddress ma = new MailAddress(txtFrom.Text,txtName.Text);
Mail.From = ma;
Mail.To.Add(txtTo.Text);

if(txtCC.Text.Trim().Length != 0)
Mail.CC.Add(txtCC.Text);

if(txtBCC.Text.Trim().Length != 0)
Mail.Bcc.Add(txtBCC.Text);

Mail.Subject = txtSubject.Text;


if (txtAttachment.Text.Trim().Length != 0)
{
string sAttach = txtAttachment.Text.Replace("\\","\\\\");
Mail.Attachments.Add(new Attachment(sAttach));
}


//Note: When you make "
IsBodyHtml" to true make
//ValidateRequest="
false" in page derective
//As to make HTML content passed.
Mail.IsBodyHtml = true;
Mail.Body = txtComment.Text;


//Setting Reply address.
if (txtReplyTo.Text.Trim().Length != 0)
{
Mail.Headers.Add("
Reply-To", txtReplyTo.Text);
}
else
{
Mail.Headers.Add("
Reply-To", txtFrom.Text);
}


try
{
SmtpClient smtpMailObj = new SmtpClient();
//eg:localhost, 192.168.0.x, replace with your server name
smtpMailObj.Host = "
myMailServer";
smtpMailObj.Send(Mail);
Response.Write("
Your Message has been sent successfully");
}
catch (Exception ex)
{
Response.Write("
Message Delivery Fails");
}

Send Email from Gmail in asp.net 2.0




protected void btnSendEmail_Click(object sender, EventArgs e)
{

//Create Mail Message Object with content that you want to send with mail.
System.Net.Mail.MailMessage MyMailMessage = new
System.Net.Mail.MailMessage
("dotnetguts@gmail.com","myfriend@yahoo.com",
"This is the mail subject", "Just wanted to say Hello");


MyMailMessage.IsBodyHtml = false;

//Proper Authentication Details need to be passed when sending email from gmail
System.Net.NetworkCredential mailAuthentication = new
System.Net.NetworkCredential("dotnetguts@gmail.com", "myPassword");

//Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
//For different server like yahoo this details changes and you can
//get it from respective server.
System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com",587);

//Enable SSL
mailClient.EnableSsl = true;

mailClient.UseDefaultCredentials = false;

mailClient.Credentials = mailAuthentication;

mailClient.Send(MyMailMessage);

}

Most Recent Post

Most Recent Ado.net FAQ

Most Recent .Net Framework FAQ

Most Recent Configuration Files FAQ

Daily Quote, Motivation, Inspiration and more

Subscribe Blog via Email

Enter your email address: