473,395 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

ASP Email Attachment Problem

I am working on a simple email application and I am able to send mail successfully through but I cannot get the "fileUpload tool", to attach any files. Each time I try any different attachment code I get errors and the mail will not send. My code is below: Thank you.

Expand|Select|Wrap|Line Numbers
  1. Imports System.Web.Mail
  2. Partial Class _Default
  3.     Inherits System.Web.UI.Page
  4.  
  5.     Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
  6.         Dim emailBody As String = txtBody.Text
  7.         Dim emailMessage As New Mail.MailMessage()
  8.  
  9.         emailMessage.BodyFormat = Mail.MailFormat.Text
  10.         emailMessage.To = txtTo.Text
  11.         emailMessage.From = "mturner64@student.sosu.edu"
  12.         emailMessage.Subject = txtSubject.Text
  13.         emailMessage.Body = txtBody.Text
  14.         Label1.Text = "Your message has been sent!"
  15.  
  16.         emailMessage.Attachments.Add(New MailAttachment(AttachmentFile.PostedFile.InputStream, AttachmentFile.FileName))
  17.  
  18.         SmtpMail.SmtpServer = "webmail.student.sosu.edu"
  19.         SmtpMail.Send(emailMessage)
  20.  
  21.  
  22.     End Sub
May 8 '08 #1
13 1727
DrBunchman
979 Expert 512MB
What are the errors you are receiving?

Dr B

PS Moved to .NET Forum - the ASP Forum is for Classic ASP only.
May 8 '08 #2
What are the errors you are receiving?

Dr B

PS Moved to .NET Forum - the ASP Forum is for Classic ASP only.

Well, before I run the program it gives this error.

"Error 3 Type 'Attachment' is not defined. "

Referring to the word Attachment following the word New
May 8 '08 #3
If I add the word Mail on the front of attachment it throws errors stating the "AttachmentFile" is not declared
May 8 '08 #4
DrBunchman
979 Expert 512MB
I think it's just looking for a string containing the path and filename of your attachment.

Have you tried putting that in?

Dr B
May 8 '08 #5
I think it's just looking for a string containing the path and filename of your attachment.

Have you tried putting that in?

Dr B

Could you please share an example? I have tried about 15 different alternatives and none have seemed to work! My brain is getting a bit fuzzy, lol.
May 8 '08 #6
DrBunchman
979 Expert 512MB
Does the following work?
Expand|Select|Wrap|Line Numbers
  1. emailMessage.Attachments.Add(New System.Net.Mail.Attachment(AttachmentFile.FileName))
Dr B

Edit - actually you'll need to use the full file path of the attachment rather than the FileName property.
May 8 '08 #7
Does the following work?
Expand|Select|Wrap|Line Numbers
  1. emailMessage.Attachments.Add(New System.Net.Mail.Attachment(AttachmentFile.FileName))
Dr B

No, error says "AttachmentFile" is not declared.
May 8 '08 #8
DrBunchman
979 Expert 512MB
Where is the file stored that you wish to attach? Is it automatically selected by your app or does your user select it from a dialog box?
May 8 '08 #9
Where is the file stored that you wish to attach? Is it automatically selected by your app or does your user select it from a dialog box?

The user selects it from the fileUpload control (browse button).
May 8 '08 #10
DrBunchman
979 Expert 512MB
I could be wrong about this but I would have thought that the user must upload the file to your server before you can send it. Does your code do this?
May 8 '08 #11
I could be wrong about this but I would have thought that the user must upload the file to your server before you can send it. Does your code do this?

I do not believe so.
May 8 '08 #12
I do not believe so.

Using the following different code:

Expand|Select|Wrap|Line Numbers
  1. Dim attachFile As New MailAttachment(txtAttachmentPath.PostedFile.InputStream.ToString)
  2.         emailMessage.Attachments.Add(attachFile)
Causes no pre-build errors but when I run the application this error occurs:

Server Error in '/proj02' Application.
--------------------------------------------------------------------------------

Invalid mail attachment 'System.Web.HttpInputStream'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Invalid mail attachment 'System.Web.HttpInputStream'.

Source Error:


Line 15:
Line 16: 'emailMessage.Attachments.Add(New MailAttachment(AttachmentFile.PostedFile.InputStre am, AttachmentFile.FileName))
Line 17: Dim attachFile As New MailAttachment(txtAttachmentPath.PostedFile.InputS tream.ToString)
Line 18: emailMessage.Attachments.Add(attachFile)
Line 19:
May 8 '08 #13
DrBunchman
979 Expert 512MB
Can you try to upload the file to the server first then add the uploaded file as an attachment.

This tutorial will help you to sort out the uploading bit.

Let me know how it goes.

Dr B
May 8 '08 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Wald | last post by:
Hello group, I've got a script here that sends emails with an attachment to an email address that is retrieved from an html form. The email sending code is include below. The problem: when...
3
by: Paul Lamonby | last post by:
Hi, I am sending a file from the server as an email attachment. The file is being attached no problem and sending the email, but I get an error when I try to open it saying it is corrupt....
1
by: abracad | last post by:
I'd like to add a form that will email a file attachment to me. Can anyone recommend a decent free script? Thanks
4
by: Paul Schmidt | last post by:
Dear list: I am new to python, and I am trying to figure out the short answer on something. I want to open a POP3 mailbox, read the enclosed mail using the POP3 module, , and then process it...
2
by: New User | last post by:
Hi, I have a PDF file that I am trying to send as an attachment through a C# program. Even though the PDF file can be opened by itself, sometimes the same file cannot be opened as an...
4
by: Russell Bungay | last post by:
Hello all, I have written a short function, based on a recipe in the Python Cookbook, that sends an e-mail. The function takes arguments that define who the e-mail is to, from, the subject, the...
3
by: =?Utf-8?B?SHVnaA==?= | last post by:
Hi There, I use follow code to send email inside VB.NET 2005. It does not work well. Error message of "Failure sending email" would occue. However, email was sent out sometimes. I am confused...
1
by: krishan123456 | last post by:
i have tried to send email with attached doc file but when i receive the mail i find the attached file in encoded text instead of actuall attachment.the code that i used to send an email is given...
3
by: Steven Allport | last post by:
I am working on processing eml email message using the email module (python 2.5), on files exported from an Outlook PST file, to extract the composite parts of the email. In most instances this...
3
by: jambonjamasb | last post by:
Hi I have two tables: email_tbl Data_table Data table is is used to create a Form Data_form
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.