Trying to send an email with an attachment

Posted on 2008-11-06
Last Modified: 2012-05-05
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'.

            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


            TheMailConnection = Nothing

            TheMailMessage = Nothing

            Return Nothing

        Catch ex As Exception

            Return Nothing

        End Try

Open in new window

Question by:utlonghornjulie
    LVL 3

    Accepted Solution

    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)

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

                    Catch ex As Exception
                        Debug.WriteLine("ERROR E-Mail Not Sent ... " & Now & " - " & ex.Message.ToString)
                    End Try
    LVL 3

    Expert Comment

    I had the same problem with a logging service I wrote a few months ago.  Above is the code I ended up with.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now