Results 1 to 4 of 4

Thread: Invalid Cast Ex in SendMail

  1. #1

    Thread Starter
    Hyperactive Member Rocketdawg's Avatar
    Join Date
    Feb 2003
    Location
    Back in the doghouse
    Posts
    294

    Invalid Cast Ex in SendMail

    Hi Everyone

    I'm having a bit of trouble getting a routine to work and I can't quite figure out whats wrong. It keeps throwing an 'invalid cast exception'. Any ideas? Thanks,

    Cdog

    VB Code:
    1. Sub SendMailProcessing()
    2.         Dim myMessage As New MailMessage
    3.         Dim str As New StringBuilder
    4.         Dim StylePath As String = Session("MyStylePath") 'path to an uploaded pic
    5.         Dim PicturePath As String = Session("MyPicturePath")'path to an uploaded pic
    6.  
    7.         MyList = Session("List")
    8.  
    9.         'build the body of the email
    10.         str.Append("First Name: " & MyList("First_Name") & ControlChars.CrLf)
    11.         str.Append("Last Name: " & MyList("Last_Name") & ControlChars.CrLf)
    12.         str.Append("Address: " & MyList("Address") & ControlChars.CrLf)
    13.         str.Append("Apartment Number: " & MyList("Apt") & ControlChars.CrLf)
    14.         str.Append("City: " & MyList("City") & ControlChars.CrLf)
    15.         str.Append("State: " & MyList("State") & ControlChars.CrLf)
    16.         str.Append("Zip: " & MyList("Zip") & ControlChars.CrLf)
    17.         str.Append("Phone: " & MyList("Phone") & ControlChars.CrLf)
    18.         str.Append("Email: " & MyList("Email") & ControlChars.CrLf & ControlChars.CrLf)
    19.         str.Append("BaseColor: " & MyList("Base_Color") & ControlChars.CrLf)
    20.         str.Append("Hair Type: " & MyList("Hair_Type") & ControlChars.CrLf)
    21.         str.Append("Wave Type: " & MyList("Wave_Type") & ControlChars.CrLf)
    22.         str.Append("Front to back measurement: " & MyList("Front_to_Back_Measurement") & ControlChars.CrLf)
    23.         str.Append("Side to side measurement: " & MyList("Side_to_Side_Measurement") & ControlChars.CrLf)
    24.         str.Append("Top Length: " & MyList("Top_Length") & ControlChars.CrLf)
    25.         str.Append("Front_Length: " & MyList("Front_Length") & ControlChars.CrLf)
    26.         str.Append("Side Length: " & MyList("Side_Length") & ControlChars.CrLf)
    27.         str.Append("Back Length: " & MyList("Back_Length") & ControlChars.CrLf & ControlChars.CrLf)
    28.         str.Append("Special Instructions: " & MyList("Special_Instructions") & ControlChars.CrLf)
    29.         With myMessage
    30.             .To = "[email protected]"
    31.             .From = MyList("Email")
    32.             .Subject = "Incoming Order"
    33.             .Body = str.ToString
    34.             .BodyFormat = MailFormat.Text
    35.             If StylePath <> "" Then
    36.                 .Attachments.Add(StylePath)
    37.             End If
    38.             If PicturePath <> "" Then
    39.                 .Attachments.Add(PicturePath)
    40.             End If
    41.         End With
    42.  
    43.         SmtpMail.Send(myMessage)
    44.  
    45.     End Sub
    Last edited by Rocketdawg; Aug 11th, 2005 at 06:58 PM. Reason: addition to post

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Invalid Cast Ex in SendMail

    Which line throws the exception?

  3. #3

    Thread Starter
    Hyperactive Member Rocketdawg's Avatar
    Join Date
    Feb 2003
    Location
    Back in the doghouse
    Posts
    294

    Re: Invalid Cast Ex in SendMail

    Wild Bill

    Here's what shows up:

    VB Code:
    1. Source Error:
    2.  
    3.  
    4. Line 149:        End With
    5. Line 150:
    6. [COLOR=DarkRed]Line 151:        SmtpMail.Send(myMessage)[/COLOR]
    7. Line 152:        lblMessage.Text = "Mail has been sent"
    8. Line 153:    End Sub
    9.  
    10. Source File: ####.aspx    Line: 151
    11.  
    12. Stack Trace:
    13.  
    14.  
    15. [InvalidCastException: Specified cast is not valid.]
    16.    System.Web.Mail.CdoSysHelper.Send(MailMessage message) +769
    17.    System.Web.Mail.SmtpMail.Send(MailMessage message) +153
    Thanks!

    Cdog

  4. #4

    Thread Starter
    Hyperactive Member Rocketdawg's Avatar
    Join Date
    Feb 2003
    Location
    Back in the doghouse
    Posts
    294

    Re: Invalid Cast Ex in SendMail

    After 3 beers and 2 pots of coffee -->

    Oops....
    VB Code:
    1. strFolder = "temps/" 'my original goofy line in my upload sub
    2.   strFolder = Server.MapPath("temps/") 'changed it to this

    And added these to the SendMail:

    VB Code:
    1. Dim oAttch1 As MailAttachment = New MailAttachment(StylePath)
    2.         Dim oAttch2 As MailAttachment = New MailAttachment(PicturePath)

    And it seems to be happy goodnight

    Cdog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width