Sending E-mails with Attachments using ASP.NET 2.0
page 2 of 6
by Rich Crawford
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 36850/ 50

Adding attachments by filename

Attaching by filename is the simplest way to add an attachment because it requires the least amount of code.  This works best when you know the location and name of the file you wish to attach.

In this demonstration we will create a new System.Net.Mail.MailMessage to send our e-mail message.  We will be attaching an existing file by filename.  

Listing 1 - Attaching a file by filename

Sub SendMail()
  'Configure basic infomation for an e-mail message
  Dim FromName As String = "Sam Poole"
  Dim FromEmail As String = ""
  Dim Subject As String = "Sample Email with attachment"
  Dim ToName As String = "Sam Poole"
  Dim ToEmail As String = ""
  Dim FileName As String = Server.MapPath("./files/samplefilename.pdf")
  'Create the SmtpClient object  (assumes using localhost)
  Dim smtp As New System.Net.Mail.SmtpClient("")
  'Create the MailMessage object
  Dim msg As New System.Net.Mail.MailMessage()
  'Assign from address
  msg.From = New System.Net.Mail.MailAddress(FromEmail, FromName)
  'Assign to address
  msg.To.Add(New System.Net.Mail.MailAddress(ToEmail, ToName))
  'assign subject, and body
  msg.Subject = Subject
  msg.Body = "This is a test message with an attachment"
  msg.Attachments.Add(New System.Net.Mail.Attachment(Filename))
  'Send the message with SmtpClient
End Sub

View Entire Article

User Comments

Title: Good   
Name: Neetu Sachan
Date: 2010-10-01 12:49:24 PM
Plz send me language
Title: good   
Name: vimalraj
Date: 2010-09-18 12:42:24 AM
i used your sending mail coding for . it is very helpful for me. before that i searched sending mail coding for other websites. they did not give a correct format. i am extremely happy to say this well done.
Title: Re: Re: help me   
Name: Alex
Date: 2009-05-01 10:40:11 AM
Title: NVP   
Name: Nisheeth
Date: 2009-01-09 5:15:40 AM
Nice one...
Title: Re: help me   
Name: Rich
Date: 2009-01-07 7:50:32 PM

What kind of error do you get?
Title: help me   
Name: Eshwar
Date: 2009-01-07 12:47:36 AM
It is working fine in my local machine. But when i am trying to execute it from remote machine it is not working.Please help me
Title: Nice article   
Name: Firoz
Date: 2009-01-02 2:43:39 AM
This is really helpful.
Title: Thanks for posting this   
Name: KnightWing19
Date: 2008-12-03 4:21:23 PM
This was a real help thank you so much!

I was wondering if you knew what the max file size that this could send was?
Title: Very Good   
Name: Very Good
Date: 2008-09-08 1:52:14 AM
It is a very good utitlity. I need it on my project. So Thanksssssssssssss.
Title: Excellent Articles   
Name: Mohammad Javed
Date: 2008-07-08 6:22:27 AM
very good articles......
Title: Don't forget to dispose your attachment   
Name: Greg Graham
Date: 2008-05-13 6:14:54 PM
In Listing 1 above that uses a filename to initialize an Attachment, don't forget to explicitly call Dispose() on either the attachment or the message after it has been sent. The reason is that the attachment file will be silently opened in the Attachment constructor and held open even after the message is sent.

In Listing 2, an open stream is passed to the Attachment constructor. Normally, you would remember to close the stream explicitly; but the assumption here is that ASP.Net closes the stream when its done processing the request.
Title: medium trust   
Name: Lee
Date: 2008-05-07 10:50:35 PM
Can this code work on ASP.NET 2.0 Medium trust share hosting?
Title: Re Problem   
Name: Rich
Date: 2008-05-01 10:49:33 AM
If the code didn't return any errors, it probably has to do with your smtp gateway. The gateway needs to allow relaying for your server's IP address.

Try using your own email address as the from address to see if the smtp server sends you an NDR message.
Title: problem   
Name: Ravish
Date: 2008-05-01 1:46:31 AM
hello i had try this code in my application it saw its working but i am not getting mail so is there any system configuration please tell me
Thanks in advance
Title: Feed-Back   
Name: Balu
Date: 2008-04-17 1:54:29 AM
This code is helpful, but need little descrpition on the topic.
Title: Thank you   
Name: jgonzalez
Date: 2008-02-26 6:01:10 PM
It was very helpful, thanks =)
Title: Great   
Name: Kalyana Sundaram
Date: 2008-02-06 6:46:03 AM
Usefull one!!
Title: Thanks   
Name: Rob
Date: 2008-01-07 12:40:12 PM
Precise, to the point, accurate, and the code works!
Thanks very much Rich
Title: Thanks   
Name: Arindam
Date: 2007-11-29 2:05:56 PM
Its really cool...
sending attachments and all stuff really awesome..

Thanks a lot!!!
Title: Error?   
Name: Sudipta
Date: 2007-09-01 2:06:31 AM
It gives me error on smtp.Send(msg). can you help me out of this critical situation. when i am putting some value over email recipent address and sender address it gives me error on pressing send button. pls help me out..
Title: Mr.   
Name: Mekonnen
Date: 2007-07-24 5:06:17 AM
Hi Rich Crawford;

I got the article very intersting. But the problem I have is when I used the code to send mails using emails from the SQL SERVER 2000 Database, the mail is treated as Junk mail.

Can anybody help me? Thanks
Title: Spam   
Name: Hare Ram Yadav
Date: 2007-02-23 4:51:41 AM
Why mails sent by this method are declared spam by McaFee SpamKiller ? How can we protect it from being spammed ?
Title: Virus Scan?   
Name: curious
Date: 2007-02-13 1:33:01 PM
Using contentstream, do I need to worry about potential virus in theattachment? How do I do virus scan on it?
Title: Best Code   
Name: Rajiv
Date: 2006-12-27 2:10:45 AM
Thanks for the code. its the best one I am using.
Title: email   
Name: dave
Date: 2006-10-10 2:08:31 PM
email in effect! excellent article!
Title: Good one   
Name: Amrit
Date: 2006-07-25 9:34:24 AM
Really to the point.
Title: Thanks!   
Name: Jason
Date: 2006-07-17 1:55:56 PM
This was a great help! Simple and to the point.
Title: Code   
Name: Dharmesh
Date: 2006-07-14 2:35:28 PM
How to send e-mail in ASP.NET 1.0 with visual studio 2003 version.
if you have this on in this version plz send we.
i will wait for your reply.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2023  |  Page Processed at 2023-10-03 11:16:24 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search