.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Imran Ghani
Sohaib Khalid

Home >> Code Snippets >> Email >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Function To Send Email

Posted By:Lakhan Pal Garg       Posted Date: September 12, 2009    Points: 10    Category: Email    URL: http://www.dotnetspark.com  

User needs to pass few parameter to this method to send the mail.
body - Content of email that you want to send.
toadd - Address of the user to whom you want to send the Email.
ccAdd - Address of the user to whom you want to send Email in CC.
bccadd -Address of the user to whom you want to send Email in BCC.
fromaddm - Address of the user that want to send the Email.
subject - Subject of the Email.
attachment - Attachment any if you want to send to user otherwise set the value as blank if you don't want to send any email.

Address of SMTP Server - Replace this text with the address of your SMTP Server.
Code To Send Email:
public static bool SendMail(string body, string toadd, string fromadd, string subject, string attachment, string ccAdd, string bccadd)
string mailServerName = "Address of SMTP Server ";
//MailMessage represents the e-mail being sent
using (MailMessage message = new MailMessage(fromadd, toadd, subject, body))
if (attachment != "")
message.Attachments.Add(new Attachment(attachment));
if (bccadd != "")
message.IsBodyHtml = true;
message.Priority = MailPriority.Normal;
SmtpClient mailClient = new SmtpClient();
mailClient.Host = mailServerName;
mailClient.UseDefaultCredentials = true;

return true;
catch (SmtpException ex)

return false;
catch (Exception ex)

return false;


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend