• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

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

0
utlonghornjulie
Asked:
utlonghornjulie
  • 2
1 Solution
 
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
 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now