error sending attachments w/ SmtpMail.Send
-
I have two (HTML) file upload fields in my form that are initially disabled. Once a checkbox is checked for each of them, they become enabled. On form submission, one of the functions it performs is to attach the files from the boxes to an email object and send it off. Of course, it's not that easy. I'm getting the follwing error: System.InvalidCastException: Specified cast is not valid. at System.Web.Mail.CdoSysHelper.Send(MailMessage message) at System.Web.Mail.SmtpMail.Send(MailMessage message) at Marketing.EnjoyChicago.EmailAlert() in C:\Inetpub\wwwroot\Marketing\EnjoyChicago.aspx.vb:line 139 I have a feeling that I may be referencing the PostedFile property incorrectly, but I'm pretty new at this so I'm not positive. Here's the pertinent code:
'check if attachment field is active If inPhoto.Disabled = False Then 'make sure there is a file name If inPhoto.PostedFile.FileName.ToString <> "" Then objMail.Attachments.Add(inPhoto.PostedFile) End If End If If inDatabase.Disabled = False Then If inDatabase.PostedFile.FileName.ToString <> "" Then objMail.Attachments.Add(inDatabase.PostedFile) End If End If Try SmtpMail.Send(objMail) 'this is the line generating the error Response.Write(objMail.Attachments.Count.ToString()) Catch ex As Exception" Response.Write(ex) End Try
Any ideas? Thanks in advance for any help. P.S. bonus points for anyone who can link me to a complete list of MIME types or an easy way to filter out anything but .xls, .jpeg, .tiff, .pdf, or .eps files. ------------------- abort, retry, fail? -
I have two (HTML) file upload fields in my form that are initially disabled. Once a checkbox is checked for each of them, they become enabled. On form submission, one of the functions it performs is to attach the files from the boxes to an email object and send it off. Of course, it's not that easy. I'm getting the follwing error: System.InvalidCastException: Specified cast is not valid. at System.Web.Mail.CdoSysHelper.Send(MailMessage message) at System.Web.Mail.SmtpMail.Send(MailMessage message) at Marketing.EnjoyChicago.EmailAlert() in C:\Inetpub\wwwroot\Marketing\EnjoyChicago.aspx.vb:line 139 I have a feeling that I may be referencing the PostedFile property incorrectly, but I'm pretty new at this so I'm not positive. Here's the pertinent code:
'check if attachment field is active If inPhoto.Disabled = False Then 'make sure there is a file name If inPhoto.PostedFile.FileName.ToString <> "" Then objMail.Attachments.Add(inPhoto.PostedFile) End If End If If inDatabase.Disabled = False Then If inDatabase.PostedFile.FileName.ToString <> "" Then objMail.Attachments.Add(inDatabase.PostedFile) End If End If Try SmtpMail.Send(objMail) 'this is the line generating the error Response.Write(objMail.Attachments.Count.ToString()) Catch ex As Exception" Response.Write(ex) End Try
Any ideas? Thanks in advance for any help. P.S. bonus points for anyone who can link me to a complete list of MIME types or an easy way to filter out anything but .xls, .jpeg, .tiff, .pdf, or .eps files. ------------------- abort, retry, fail?InvalidCastException? I can't really see the line numbers in the code, so it is little difficult to point out exact solution. Looks like you are passing incorrect data type (Turn on Option Strict, Explcit). Also usually before sending email, we need to set the SmtpMail.MailServer property. It does not look like you did. Make sure it points to correct mail server ID. For example
SmtpMail.MailServer = "mail.companywebsite.com"
Make sure you get proper mail server address.' Call this function, this function can be further improved for performance, bla bla ' But this does our work for now. ' You have to check this on server (you can do the same thing on client but it will be broken) ' Also make sure you set the IIS Execute permissions for the folder is set to "None" Function IsValidFileType(MyPostedFile as HttpPostedFile) As Boolean Dim acceptableFileTypes as string() = New String(){ ".xls", ".pdf", ".jpeg", ".jpg" } for i=0 to acceptableFileTypes.Length - 1 If MyPostedFile.FileName.EndsWith(acceptableFileTypes(i)) Then Return True Next Return False End Function
Abi ( Abishek Bellamkonda ) My Blog: http://abibaby.blogspot.com =(:* -
I have two (HTML) file upload fields in my form that are initially disabled. Once a checkbox is checked for each of them, they become enabled. On form submission, one of the functions it performs is to attach the files from the boxes to an email object and send it off. Of course, it's not that easy. I'm getting the follwing error: System.InvalidCastException: Specified cast is not valid. at System.Web.Mail.CdoSysHelper.Send(MailMessage message) at System.Web.Mail.SmtpMail.Send(MailMessage message) at Marketing.EnjoyChicago.EmailAlert() in C:\Inetpub\wwwroot\Marketing\EnjoyChicago.aspx.vb:line 139 I have a feeling that I may be referencing the PostedFile property incorrectly, but I'm pretty new at this so I'm not positive. Here's the pertinent code:
'check if attachment field is active If inPhoto.Disabled = False Then 'make sure there is a file name If inPhoto.PostedFile.FileName.ToString <> "" Then objMail.Attachments.Add(inPhoto.PostedFile) End If End If If inDatabase.Disabled = False Then If inDatabase.PostedFile.FileName.ToString <> "" Then objMail.Attachments.Add(inDatabase.PostedFile) End If End If Try SmtpMail.Send(objMail) 'this is the line generating the error Response.Write(objMail.Attachments.Count.ToString()) Catch ex As Exception" Response.Write(ex) End Try
Any ideas? Thanks in advance for any help. P.S. bonus points for anyone who can link me to a complete list of MIME types or an easy way to filter out anything but .xls, .jpeg, .tiff, .pdf, or .eps files. ------------------- abort, retry, fail?Set HtmlInputFile.Accept property to a comma-separated list of MIME encodings available at http://www.w3schools.com/media/media_mimeref.asp Abi ( Abishek Bellamkonda ) My Blog: http://abibaby.blogspot.com =(:*
-
Set HtmlInputFile.Accept property to a comma-separated list of MIME encodings available at http://www.w3schools.com/media/media_mimeref.asp Abi ( Abishek Bellamkonda ) My Blog: http://abibaby.blogspot.com =(:*
Thanks for your input! I did have the mail server set but I didn't include that in my snippet. here's the entirety of the sub :
Sub EmailAlert() Dim strBody, Type As String strBody = "This is an automatically generated email sent to notify you that " & tbName.Text & " has submitted an order. Any databases or pictures they have uploaded will be attached to this email" Dim objMail As MailMessage = New MailMessage SmtpMail.SmtpServer = "167.10.10.15" objMail.To = "me@me.com" objMail.From = "application@app.com" objMail.Priority = MailPriority.High objMail.BodyFormat = MailFormat.Html objMail.Subject = tbName.Text & " has submitted an order." objMail.Body = strBody 'check if attachment field is active If inPhoto.Disabled = False Then 'make sure there is a file name If inPhoto.PostedFile.FileName.ToString <> "" Then objMail.Attachments.Add(inPhoto.PostedFile) End If End If If inDatabase.Disabled = False Then If inDatabase.PostedFile.FileName.ToString <> "" Then objMail.Attachments.Add(inDatabase.PostedFile) End If End If Try 'BELOW is the line that generates the error SmtpMail.Send(objMail) 'this is the line generating the error Response.Write(objMail.Attachments.Count.ToString()) Catch ex As Exception" Response.Write(ex) End Try End Sub
i also did denote the line generating the error, but it was in an in-line comment so it was easy to overlook; so here that is:SmtpMail.Send(objMail) 'this is the line generating the error
Also, I'm using VB.Net in visual studio and it's saying that accept is not a recognized attribute of an input element. I tried using it before but never actually tested it... maybe it's just one of those weird VS things that work despite the fact that VS says it won't. Any other ideas? ------------------- abort, retry, fail? -
Thanks for your input! I did have the mail server set but I didn't include that in my snippet. here's the entirety of the sub :
Sub EmailAlert() Dim strBody, Type As String strBody = "This is an automatically generated email sent to notify you that " & tbName.Text & " has submitted an order. Any databases or pictures they have uploaded will be attached to this email" Dim objMail As MailMessage = New MailMessage SmtpMail.SmtpServer = "167.10.10.15" objMail.To = "me@me.com" objMail.From = "application@app.com" objMail.Priority = MailPriority.High objMail.BodyFormat = MailFormat.Html objMail.Subject = tbName.Text & " has submitted an order." objMail.Body = strBody 'check if attachment field is active If inPhoto.Disabled = False Then 'make sure there is a file name If inPhoto.PostedFile.FileName.ToString <> "" Then objMail.Attachments.Add(inPhoto.PostedFile) End If End If If inDatabase.Disabled = False Then If inDatabase.PostedFile.FileName.ToString <> "" Then objMail.Attachments.Add(inDatabase.PostedFile) End If End If Try 'BELOW is the line that generates the error SmtpMail.Send(objMail) 'this is the line generating the error Response.Write(objMail.Attachments.Count.ToString()) Catch ex As Exception" Response.Write(ex) End Try End Sub
i also did denote the line generating the error, but it was in an in-line comment so it was easy to overlook; so here that is:SmtpMail.Send(objMail) 'this is the line generating the error
Also, I'm using VB.Net in visual studio and it's saying that accept is not a recognized attribute of an input element. I tried using it before but never actually tested it... maybe it's just one of those weird VS things that work despite the fact that VS says it won't. Any other ideas? ------------------- abort, retry, fail?Use valid email addresses. I don't think me@me.com is valid. Usually the problem is with from/to email or MailServer. Try other values it should work. Abi ( Abishek Bellamkonda ) My Blog: http://abibaby.blogspot.com =(:*
-
Use valid email addresses. I don't think me@me.com is valid. Usually the problem is with from/to email or MailServer. Try other values it should work. Abi ( Abishek Bellamkonda ) My Blog: http://abibaby.blogspot.com =(:*
-
Use valid email addresses. I don't think me@me.com is valid. Usually the problem is with from/to email or MailServer. Try other values it should work. Abi ( Abishek Bellamkonda ) My Blog: http://abibaby.blogspot.com =(:*
-
also: i know the mailserver is correct because it only fails out when i attempt to attach files. it sends perfectly fine when there are no attachments. ------------------- abort, retry, fail?
hi, Just try in this way Dim att As New MailAttachment("C:\images\123.jpg") msg.Attachments.Add(att) this works well for me. Sorry for an late reply, i dnt knw whther it help's you, but i want to suggest you. Bye Exelio