473,385 Members | 1,487 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,385 software developers and data experts.

email attachments

I'm trying to enable attaching a file to an email that is sent by my asp.net
application. The email works fine without an attachment, but when I add a
file using the following code:

Dim newMail As New Mail.MailMessage

newMail.To = strTo

newMail.From = ConfigurationSettings.AppSettings("MailFromAddr")

newMail.Subject = strSubj

newMail.Body = strbody

Dim attachment As New MailAttachment(aAttachments(i))

newMail.Attachments.Add(aAttachments(i))

SmtpMail.SmtpServer = ConfigurationSettings.AppSettings("SMTPServer")

Try

SmtpMail.Send(newMail)

Return True

Catch ex As Exception

'copy message to string for debugging - not really used anywhere

Dim strMessage As String = ex.Message

Return False

End Try

---------------------

The mail fails to send and the exception is "Specified cast is not valid".
Since this only happens when there is an attachment present, it seems that;s
the problem. Do I need to encode the attachment first or something? I was
under the impression that it would happen automatically, but that's probably
where I'm wrong. Can anyone tell me how this should work? Thanks!

Matt
Nov 18 '05 #1
1 1435
Thanks. I realized I had been passing the string that was the path to the
file when I tried to attach instead of the object created from thatstring in
the provious line. Now it works. Yay!

Joao S Cardoso [MVP] wrote:
Try this:

Public Shared Function SendEmail(ByVal cFrom As String, ByVal cTo
As String, ByVal cCC As String, ByVal cSubject As String, ByVal cBody
As String, Optional ByVal cAttach1 As String = "", Optional ByVal
cAttach2 As String = "", Optional ByVal tMailFormat As MailFormat =
MailFormat.Text) As Boolean Dim oMailObject As New MailMessage
Dim iListAttach As Collections.IList
Dim oMailAttach1 As MailAttachment
Dim oMailAttach2 As MailAttachment

oMailObject.From = cFrom
oMailObject.To = cTo
oMailObject.Cc = cCC
oMailObject.Subject = cSubject
oMailObject.Body = cBody & vbNewLine
oMailObject.BodyFormat = tMailFormat

If cAttach1.Trim.Length > 0 Then
oMailAttach1 = New MailAttachment(cAttach1,
MailEncoding.UUEncode)
oMailObject.Attachments.Add(oMailAttach1) End If
If cAttach2.Trim.Length > 0 Then
oMailAttach2 = New MailAttachment(cAttach2,
MailEncoding.UUEncode)
oMailObject.Attachments.Add(oMailAttach2) End If

SmtpMail.SmtpServer = "127.0.0.1"
SmtpMail.Send(oMailObject)

oMailObject = Nothing

End Function

Cumprimentos / Regards

Joao Cardoso (MVP dotNET)
================================================== =====
Visite os grupos de discussão portugueses:
microsoft.public.pt.vsnet
microsoft.public.pt.dotnet
microsoft.public.pt.*

[LusoCoders]- http://groups.yahoo.com/group/lusocoders/
[PontoNetPT]- http://www.pontonetpt.com
jj***@acinet.pt.n.o-s.p-a.m - www.acinet.pt
================================================== =====


Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
3
by: LutherRevisited | last post by:
Is there a way I can put a message together without having to download any attachments there may be at the same time. I'm not having any problems dealing with attachments, but the way I'm doing...
5
by: morphex | last post by:
Hi, I have an email that's in the utf-8 encoding, and I'm getting this error message when I try to send it using smtplib: * Module smtplib, line 688, in sendmail * Module smtplib, line 485,...
5
by: paii, Ron | last post by:
How do I setup a email with attachment for preview but require the user to push the SEND button in Outlook. I have the following function but it sends the email without the sender ever seeing it. ...
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...
1
by: mike11d11 | last post by:
If someone could help me, I need to be able to send attachments from my access database that I have created. This database runs queries then generates a report off the queries from underlying...
10
by: OdAwG | last post by:
Hello All, Is it possible to send an email from Access? I found a Microsoft article on how to do this but I keep getting an error "RUNTIME ERROR 438" -- Object doesn't support this property or...
10
by: Walshi | last post by:
Hi all, I'm a relative newby to access and VBA etc. My forms and tables etc are working great and saving lots of time...However... I have two databases with the exact same table format. I want...
2
by: oyster | last post by:
I find that the existing email moudle is some hard for me to understand, especially the part of how to set the CC, BCC and attach the files. Is there any more easy one like this p-code? import...
1
by: budyerr | last post by:
All, I am trying to build a email submission form using asp.net. I currently have a web form page that will upload to my webhosting server, attach to email then delete the file after sending. ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.