email attachments

M

MattB

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
 
J

Joao S Cardoso [MVP]

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
(e-mail address removed)-s.p-a.m - www.acinet.pt
=======================================================
 
M

MattB

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top