Click to See Complete Forum and Search --> : Specified cast is not valid


jonah_taxt
August 30th, 2004, 07:04 PM
Hi everyone. I'm trying to send an email with my application. Here's the code:

Try
Dim mensaje As New Mail.MailMessage
Dim smtpmail As Mail.SmtpMail

If Not Session("fn") Is Nothing Then
Dim oAttach = (Server.MapPath(Request.ApplicationPath) & "\Folder\(" & Session("Cot_Num") & ")" & Session("fn"))
mensaje.Attachments.Add(oAttach)
End If
mensaje.To = ClientMail
mensaje.From = AdminMail
mensaje.Fields("http://schemas.microsoft.com/cdo/configuration/smtsperver") = MailServer
mensaje.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
mensaje.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
mensaje.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
mensaje.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = AdminMail
mensaje.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = PassAdminMail
mensaje.Subject = "Some text" mensaje.BodyFormat = Mail.MailFormat.Text
mensaje.Body = "Some text"
smtpmail.SmtpServer = MailServer
smtpmail.Send(mensaje)
mensaje = Nothing
Response.Redirect("/sent.aspx", False)
Catch ex As Exception
Response.Write("Error: " & ex.Message)
End Try

the "specified cast not valid error" happens when the debugger gets to smtpmail.Send(mensaje)

Could somebody help me?

Thanx for any replies :)

MRutledge
August 30th, 2004, 10:27 PM
Try This:


Dim myMail As New MailMessage()
myMail.From = "from@microsoft.com"
myMail.To = "to@microsoft.com"
myMail.Subject = "UtilMailMessage001"
myMail.Priority = MailPriority.Low
myMail.BodyFormat = MailFormat.Html
myMail.Body = "<html><body>UtilMailMessage001 - success</body></html>"
SmtpMail.SmtpServer = "MyMailServer"
SmtpMail.Send(myMail)


You dont actually need to create a smtpmail object you can just use Mail.SmtpMail.Send because it is a static function.

jonah_taxt
August 31st, 2004, 10:55 AM
Nope, it didn't work :(

I use the mensaje.Fields because my mail server requires authentication. I don't know what else to do. Maybe there is something wrong with the attachment or something.

Please help :cry:

Thanx for your reply Rutledge

jonah_taxt
August 31st, 2004, 12:45 PM
Well, I finally figured out what was going wrong. Somehow the attachment isn't valid. I quoted those lines and the mail was sent.

I'll try to figure that out. Anyway...

Thanx for your replies :)

cmiskow
August 31st, 2004, 04:04 PM
It looks like you're trying to attach a string to the attachments list. I think it needs a MailAttachment object as a parameter. Try this:

Dim oAttach As String = (Server.MapPath(Request.ApplicationPath) & "\Folder\(" & Session("Cot_Num") & ")" & Session("fn"))
mensaje.Attachments.Add(New Mail.MailAttachment(oAttach))

jonah_taxt
August 31st, 2004, 06:30 PM
Thanx ... thanx thanx...!!!! I have been trying to solve this problem for 3 days now. Thanx a lot cmiskow :wave:

You saved my day!!

Thanx guyz!!

AJClayton
September 30th, 2004, 10:39 AM
Just a quick addition.

I too was having this problem and had been looking in books and on-line trying to find the answer. Luckily this was more like 3 hours for me than 3 days (boy, that must have been frustrating!!)

Anyway. Easy when you know how, eh?

Thanks for this advice. I can now finish the program I was working on!

Cheers

Andy

StriderKing
August 5th, 2005, 03:31 PM
cmiskow, Compadre you save my day, I was looking for the answer (googling) for long hours. So I can say Arigato Gozaimasu cmiskow-san


PD.: I singup in this site just for answer this