0
Sending e-mails is an essential feature needed in most of the websites, .net framework supplies a SMTP class that enables you to send simple e-mails. If you have to send an e-mail with additional features, you have to make use of the MailMessage class. With the help of this class, you can insert attachments, set priorities etc..very easily. You can also send HTML e-mail using MailMessage class. To send an e-mail using asp.net, you should have access to a server with .net Framework and SMTP enabled on it.
1. Sending a simple e-mail using SMTP
First we need to import the System.Web.Mail namespace.
using System.Web.Mail;
Synatax for sending a simple e-mail message :
SmtpMail.Send("FROM","TO","SUBJECT","MESSAGE BODY");
Example:
SmtpMail.Send("sender@testmail.com","recipient@testmail.com", "Test e-mail
2.Sending e-mail using MailMessage class
Instead of supplying all parameters in the Send() method, you can define properties and values separately by creating an instance of the MailMessage class. using MailMessage class, you can easily add attachments, set priorities, BCC, CC values and other additional features.
MailMessage objEmail = new MailMessage();
objEmail.To = "recipient@test-email.com";
objEmail.From = "sender@test-email.com";
objEmail.Cc = "recipientcc@test-email.com";
objEmail.Subject = "Test Email";
objEmail.Body = "This is a test email message";
objEmail.Priority = MailPriority.High;
try
{
SmtpMail.Send(objEmail);
Response.Write("your mail has been sent");
SmtpMail.SmtpServer = "localhost"
}
catch (Exception ex)
{
Response.Write("Mail sending failed: " + ex.ToString());
}
If you want to send mails in HTML format, include following code also
objEmail.BodyFormat = MailFormat.Html;
Adding Attachements to emails:
MailAttachment MyAttachment
objEmail.Attachments.Add()= new MailAttachment("C:\\My Folder\\MyFile.txt");
1. Sending a simple e-mail using SMTP
First we need to import the System.Web.Mail namespace.
using System.Web.Mail;
Synatax for sending a simple e-mail message :
SmtpMail.Send("FROM","TO","SUBJECT","MESSAGE BODY");
Example:
SmtpMail.Send("sender@testmail.com","recipient@testmail.com", "Test e-mail
2.Sending e-mail using MailMessage class
Instead of supplying all parameters in the Send() method, you can define properties and values separately by creating an instance of the MailMessage class. using MailMessage class, you can easily add attachments, set priorities, BCC, CC values and other additional features.
MailMessage objEmail = new MailMessage();
objEmail.To = "recipient@test-email.com";
objEmail.From = "sender@test-email.com";
objEmail.Cc = "recipientcc@test-email.com";
objEmail.Subject = "Test Email";
objEmail.Body = "This is a test email message";
objEmail.Priority = MailPriority.High;
try
{
SmtpMail.Send(objEmail);
Response.Write("your mail has been sent");
SmtpMail.SmtpServer = "localhost"
}
catch (Exception ex)
{
Response.Write("Mail sending failed: " + ex.ToString());
}
If you want to send mails in HTML format, include following code also
objEmail.BodyFormat = MailFormat.Html;
Adding Attachements to emails:
MailAttachment MyAttachment
objEmail.Attachments.Add()= new MailAttachment("C:\\My Folder\\MyFile.txt");
Post a Comment