dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersRanjith Kumar
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » ASP.NET WebForms

Send Mail with Attachment using ASP.Net and C#.net or VB.Net


Posted Date:     Category: ASP.NET WebForms    
Author: Member Level: Gold    Points: 8


This Example is described about how to send mail with attachment using ASP.Net and C#.Net or VB.Net



 


Here I am giving you the code to send mail with attachment by using ASP.Net and C#.Net or VB.Net

Use Following code to send mail with attachment.



Follow the below code to send mail with attachment.

Using C#.Net :



public int SendMessage(string subject, string messageBody, string fromAddress, string toAddress)
{
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
message.From = new MailAddress(fromAddress.ToString());
if (toAddress.Trim().Length > 0)
{
foreach (string addr in toAddress.Split(';'))//you can use any delimiter
{
message.To.Add(new MailAddress(addr));
}
}
Attachment attachFile = new Attachment("D:\\a.xls");//you can also get the file name with using fileupload control
message.Attachments.Add(attachFile);
message.Subject = subject;
message.Body = messageBody;
client.Host = "smtp address";
client.Port = 587;//put your smtp port number
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential("mail id", "password");
client.Send(message);
return 1;
}


Using VB.Net :



Public Function SendMessage(ByVal subject As String, ByVal messageBody As String, ByVal fromAddress As String, ByVal toAddress As String) As Integer
Dim message As MailMessage = New MailMessage()
message.IsBodyHtml = True
Dim client As SmtpClient = New SmtpClient()
message.From = New MailAddress(fromAddress.ToString())
message.To.Add(New MailAddress(toAddress.ToString()))
Dim attachFile As Attachment = New Attachment("D:\\a.xls") 'specify your file location
message.Attachments.Add(attachFile)
message.Subject = subject
message.Body = messageBody
client.Host = "smtp address" ' put your smtp address
client.Port = 587 ' put your port number for smtp
client.UseDefaultCredentials = True
client.Credentials = New System.Net.NetworkCredential("smtp login mail id", "smtp login password")
client.Send(message)
Return 1
End Function

Thank You.

Reference http://msahoo.wordpress.com/2009/06/03/easy-way-to-send-mail-through-asp-net-c-net/
Related Resources:


Read related articles: Email using c#    Email ASP.Net    Sending Email    


Did you like this resource? Share it with your friends and show your love!


Responses to "Send Mail with Attachment using ASP.Net and C#.net or VB.Net"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Easy creation of Login page and Create New user (with Terms & condition) in same window
    Previous Resource: Example of CustomValidator control
    Return to Resources
    Post New Resource
    Category: ASP.NET WebForms


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    VB.Net  .  ASP.Net  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.