Trying to send an email with an attachment

I am trying to send an email with a PDF as an attachment. When I try and run the code, I get the following error:

Unable to cast object of type 'System.String' to type 'System.Web.Mail.MailAttachment'.
Try
            Dim TheMailMessage As New MailMessage
            Dim TheMailConnection As SmtpMail
            TheMailMessage.From = sFromEmail
            TheMailMessage.To = sToEmail
 
            'Request.ServerVariables(REMOTE_ADDR) : returns the IP
 
            'System.Environment.MachineName : returns the Machine Name (when multiple servers are in load balancing)
 
            TheMailMessage.Subject = Prospect & ", " & QuoteType & ", Renewal - " & Renewal & ", Meter Fees - " & SerChg & ", Start Date - " & StartDate & ", SwitchDate - " & SwitchDate & ", Switch Type - " & SwitchType
 
            '& " - " & Request.ServerVariables("REMOTE_ADDR") & " - " & System.Environment.MachineName
            TheMailMessage.Body = "<b>Offer_BogusEsiidList</b><br>" & sMessage & "<br><b>Offer Details</b><br>" & sMessage2 & "<br><b>DealCap_EsiidList on DCID</b><br>" & sMessage3 & "<br><b>DealCap_EsiidList in ISTA not on DCID</b><br><font size=7pt>" & sMessage4 & "</font><br><b>DealCap_CustNoList</b><br>" & sMessage5 & ""
            TheMailMessage.BodyFormat = MailFormat.Html
            TheMailMessage.Attachments.Add("\\<IPAddress>\D$\GreenMountain\Pricing\SignedContracts\" & sOfferID & ".pdf")
            TheMailConnection.SmtpServer = sSmtp
            SmtpMail.Send(TheMailMessage)
            TheMailConnection = Nothing
            TheMailMessage = Nothing
            Return Nothing
        Catch ex As Exception
            Return Nothing
        End Try

Open in new window

LVL 3
utlonghornjulieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike_VCommented:
Imports System.Net
Imports System.Net.Mail

                Dim MyMessage As New MailMessage(MyFrom, MyTO, MySubject, MyMessageBody)
                MyMessage.IsBodyHtml = False
                MyAttachment = logDated
               
                Dim msgAttach As New Attachment(MyAttachment)
                MyMessage.Attachments.Add(msgAttach)

                Dim MyEmailClient As New SmtpClient(MyServer, MyServerPort)
                Dim SMTPUserInfo As New NetworkCredential(MyUN, MyPW)
                MyEmailClient.UseDefaultCredentials = False
                MyEmailClient.Credentials = SMTPUserInfo

                Try
                    MyEmailClient.Send(MyMessage)
                Catch ex As Exception
                    Debug.WriteLine("ERROR E-Mail Not Sent ... " & Now & " - " & ex.Message.ToString)
                End Try
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike_VCommented:
I had the same problem with a logging service I wrote a few months ago.  Above is the code I ended up with.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.