Tuesday, March 17, 2009

How to send email using asp.net and c#?

Code for sending email:

using System.Web.Mail;
private void Button1_Click(object sender, System.EventArgs e)

{

MailMessage mail = new MailMessage();

mail.To = txtTo.Text;

mail.From = txtFrom.Text;

mail.Subject = txtSubject.Text;

mail.Body = txtBody.Text;

SmtpMail.SmtpServer = "localhost";

SmtpMail.Send(mail);
}


You can also send email using
System.Net.Mail

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.IsBodyHtml = true;
mail.Headers.Add("Precedence", "bulk");//If u want to send mail in bulk

mail.From = new System.Net.Mail.MailAddress("EmailId", "Sender Name");
mail.ReplyTo = new System.Net.Mail.MailAddress("EmailId", "Name");
mail.Subject = " Subject";
System.Net.Mail.Attachment MailAttach = new System.Net.Mail.Attachment("path");
mail.Attachments.Add(MailAttach);
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp server name", "port no");
smtp.Credentials = new System.Net.NetworkCredential("User Id for that email", "Password");
smtp.EnableSsl = true;
mail.To.Add("Reciever Email Id ");
mail.Body = "Body Text";
smtp.Send(mail);

No comments:

Post a Comment