Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. error sending attachments w/ SmtpMail.Send

error sending attachments w/ SmtpMail.Send

Scheduled Pinned Locked Moved ASP.NET
helphtmlsalesquestionlearning
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jszpila
    wrote on last edited by
    #1

    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?

    A 2 Replies Last reply
    0
    • J jszpila

      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?

      A Offline
      A Offline
      Abi Bellamkonda
      wrote on last edited by
      #2

      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 =(:*

      1 Reply Last reply
      0
      • J jszpila

        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?

        A Offline
        A Offline
        Abi Bellamkonda
        wrote on last edited by
        #3

        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 =(:*

        J 1 Reply Last reply
        0
        • A Abi Bellamkonda

          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 =(:*

          J Offline
          J Offline
          jszpila
          wrote on last edited by
          #4

          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?

          A 1 Reply Last reply
          0
          • J jszpila

            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?

            A Offline
            A Offline
            Abi Bellamkonda
            wrote on last edited by
            #5

            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 =(:*

            J 2 Replies Last reply
            0
            • A Abi Bellamkonda

              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 =(:*

              J Offline
              J Offline
              jszpila
              wrote on last edited by
              #6

              those were edited email addresses because I really didn't want to post the ones I was using here, but I guess if someone wanted to find my email address they could probably do it anyway, heh. ------------------- abort, retry, fail?

              1 Reply Last reply
              0
              • A Abi Bellamkonda

                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 =(:*

                J Offline
                J Offline
                jszpila
                wrote on last edited by
                #7

                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?

                E 1 Reply Last reply
                0
                • J jszpila

                  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?

                  E Offline
                  E Offline
                  Exelioindia
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups