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


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Send Mail From WebMail in ASP.Net

Posted By:Sasi Prabhu       Posted Date: June 28, 2011    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

This article is used to send mail with and without attachment.
 



Mail Without Attachment :



Mail with Attachment:



Class To Send Mail without Attachment:

 
   public string sendemailwithattach(string froms,string pwds,string servers,string tos, string subject, string body)
    {
        string result = "";
        try
        {
            System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
            mail.To = tos;
            mail.From = froms;
            mail.Subject = subject;
            mail.Body = body;
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");	//basic authentication
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",froms); //set your username here
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pwds);	//set your password here
            System.Web.Mail.SmtpMail.SmtpServer = servers;
            System.Web.Mail.SmtpMail.Send(mail);
            result = "Mail Send Successfully";
        }
        catch (Exception ex) { result = ex.Message; }
        return result;
    }
Class To Send Mail with Attachment :

   public string sendemailwithattach(string froms,string pwds,string servers,string tos, string subject, string body,string location)
    {
        string result = "";
        try
        {
            System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
            mail.To = tos;
            mail.From = froms;
            mail.Subject = subject;
            mail.Body = body;
            System.Web.Mail.MailAttachment attach = new System.Web.Mail.MailAttachment(location);
            mail.Attachments.Add(attach);
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");	//basic authentication
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",froms); //set your username here
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pwds);	//set your password here
            System.Web.Mail.SmtpMail.SmtpServer = servers;
            System.Web.Mail.SmtpMail.Send(mail);
            result = "Mail Send Successfully";
        }
        catch (Exception ex) { result = ex.Message; }
        return result;
    }

To Send Mail with More Attachment :

Just Add the attachments as

MailAttachment attach1=new MailAttachment(location1);
mail.Attachments.Add(attach1);




Download Samples Here.




     

Further Readings:

Responses

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