Sending Email through ASP.NET is very easy.the .NET framework comes with a namespace which is uses for handling the Email.The namespace is:
System.Net.Mail namespace
Here i am using the two classes of the above mentioned namespace.The first class is MailMessage class which is used for actual email and second is SmtpClient class which is for sending Email.
Write the following code into the page load event:
For Example:
mailMessage.Attachments.Add(new Attachment(Server.MapPath("~/image.jpg")));
In above line of code.i am attaching a image which is placed on the server.
mailMessage.To.Add("Firstperson@domain.com");
mailMessage.To.Add("Secondperson@domain.com");
Happy Programming..:)
System.Net.Mail namespace
Here i am using the two classes of the above mentioned namespace.The first class is MailMessage class which is used for actual email and second is SmtpClient class which is for sending Email.
Write the following code into the page load event:
try { MailMessage mailMessage = new MailMessage(); mailMessage.To.Add("test@domain.com"); mailMessage.From = new MailAddress("test2@domain.com"); mailMessage.Subject = "Test Email"; mailMessage.Body = "This is an ASP.NET test E-mail!"; SmtpClient smtpClient = new SmtpClient("smtp.testISP.com"); smtpClient.Send(mailMessage); Response.Write("E-mail sent!"); } catch(Exception ex) { Response.Write("Could not send the e-mail - error: " + ex.Message); }
This is a very short example.we can do many things with the above code.For Example:
- we can Attach a file or images with Email through this line of code.
mailMessage.Attachments.Add(new Attachment(Server.MapPath("~/image.jpg")));
In above line of code.i am attaching a image which is placed on the server.
- We can send Email to multiple users through this line of code.
mailMessage.To.Add("Firstperson@domain.com");
mailMessage.To.Add("Secondperson@domain.com");
- We can set Name of the sender.
- We can send HTML body Email.
- We can use the CC and BCC fields.
- We can set the priority of Email.
Happy Programming..:)
O this so cool. i try it later for sure. thank you for tutorials.
ReplyDelete