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
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