SMTP Email wont send with attachments in VB.Net

Hello,

I have a Window Service that creates an email with attachments at a specified time every day to a list of email recipients.    I am having problems when it comes to sending out PDF attachments to the SMTP relay.  When I send the mail object, the system takes about 60 to 70 seconds every time and acts like it sends the email, but I don't receive it.  There are four attachments that get sent with each one being anywhere from 70kb to 110kb in size.  The SMTP will send the email without any problem if I don't try and send the attachments.  This makes me believe that the attachments are the cause of the problem.  I contacted our Network guys and they haven't seen the emails in the SMTP mail queue and told me that the max size for attachments is 30MB.  

I have tried sending the information through Gmail and it works perfectly, but our business won't accept that as a permanent solution.

Below is a snippet of code that I am using to create the attachment.  Any idea on what I can try to make it work?


This is the email sending portion of code:

 Sub send_email()

        oMsg.From = New MailAddress("Do_Not_Reply@business.com", "Do Not Reply")
        oMsg.To.Add(build_lists("Email_To"))
        oMsg.CC.Add(build_lists("Email_CC"))
        oMsg.Subject = ("Business Downtime Reports")
        oMsg.IsBodyHtml = True


        Try
            'Delete out the old files from the reports folder
            delete_old_files()
            'Add the attachments to the email object
            add_attachment()
            'Setup the message body for HTML
            Dim mbody As String = "<table width=600px border=1> " & _
                             "<tr> " & _
                                 "<td><img src='cid:Business_Logo' alt='Logo' width=150 height=75/></td> " & _
                             "</tr> " & _
                             "<tr> " & _
                                 "<td> " & _
                                     "<table> " & _
                                         "<tr> " & _
                                             "<td> " & _
                                                 "<p>Hello,</p> " & _
                                                 "<p>Attached are the business quality downtime reports for the previous day. </p> " & _
                                                 "<p>You are setup as a recipient of this automated email. If you wish to no longer receive the email, please submit a ticket to the  system using the following link:</p> " & _
                                                 "<p></p> " & _
                                                 "<p>Thank You,</p> " & _
                                                 "<p>Manufacturing Execution Team</p> " & _
                                                 "<p><strong></strong></p> " & _
                                                 "<p>&nbsp;</p> " & _
                                             "</td> " & _
                                             "<td> " & _
                                                 "<img src='cid:Business_Background' alt='Background Image' width=92 height=407/></td> " & _
                                         "</tr> " & _
                                     "</table> " & _
                                 "</td> " & _
                             "</tr> " & _
                         "</table>"
            'Create the alternate view for email images to appear
            Dim alternateView As AlternateView = alternateView.CreateAlternateViewFromString(mbody, Nothing, "text/html")

            'Create the linked resource for each image
            Dim logo As New LinkedResource(System.AppDomain.CurrentDomain.BaseDirectory & "Images\Business-Logo.jpg", "image/jpeg")
            logo.ContentId = "Business_Logo"
            logo.TransferEncoding = Net.Mime.TransferEncoding.Base64
            Dim logo2 As New LinkedResource(System.AppDomain.CurrentDomain.BaseDirectory & "Images\BusinessBackground.jpg", "image/jpeg")
            logo2.ContentId = "Business_Background"
            logo2.TransferEncoding = Net.Mime.TransferEncoding.Base64


            alternateView.LinkedResources.Add(logo)
            alternateView.LinkedResources.Add(logo2)
            oMsg.AlternateViews.Add(alternateView)
            oMsg.Body = Nothing

            'Msg.Body = mbody.ToString

            'Configure the email server
            Dim client As New SmtpClient()
            client.Host = "smtprelay"
            client.Send(oMsg)

            'Add a comment to the event log
            myEventLog("Business Email was sent")
        Catch ex As Exception
            myEventLog("Error Sending the Email" & ex.ToString)
            myEventLog("Inner Exception is " & ex.InnerException.ToString)
        Finally
            'Dispose of the mail object
            oMsg.Dispose()
        End Try
    End Sub

Open in new window


This is the section that creates the attachments.  I create the attachments from Crystal Report .rtp files and convert them into pdf's.  The files get created as designed so it appears that the problem doesn't stem from that.  I am curious if it may be the way I am adding the attachments to the the 'oMsg' mail object.

Sub add_attachment()
        'Function that will add the attachments to the email
        Dim cryRpt As New ReportDocument()
        Dim crtableLogoninfos As New TableLogOnInfos
        Dim crtableLogoninfo As New TableLogOnInfo
        Dim myConnectionInfo As New ConnectionInfo()
        Dim CrTables As Tables
        Dim CrTable As Table
        Dim files As String() = Directory.GetFiles(System.AppDomain.CurrentDomain.BaseDirectory & "CrystalSource\")
        With myConnectionInfo
            .UserID = "username"
            .ServerName = "SERVERNAME"
            .Password = "password"
        End With

        Try

            'Loop through the Reports folder
            For Each fileName In files
                'Load it to the Crystal Report Engine
                cryRpt.Load(fileName)
                CrTables = cryRpt.Database.Tables
                For Each CrTable In CrTables
                    crtableLogoninfo = CrTable.LogOnInfo
                    crtableLogoninfo.ConnectionInfo = myConnectionInfo

                    CrTable.ApplyLogOnInfo(crtableLogoninfo)
                Next
                'Strip the name off the file fro the .rpt extension
                fileName = Path.GetFileName(fileName)
                If fileName.EndsWith(".rpt") Then
                    fileName = Left$(fileName, fileName.Length - 4)
                End If
                'Setup the Crystal Reports Exporter
                Dim crExportOptions As ExportOptions
                Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
                Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()
                CrDiskFileDestinationOptions.DiskFileName = System.AppDomain.CurrentDomain.BaseDirectory & "Reports\" & fileName & "-" & System.DateTime.Now.ToString("MM-dd-yyyy") & ".pdf"
                crExportOptions = cryRpt.ExportOptions
                With crExportOptions
                    .ExportDestinationType = ExportDestinationType.DiskFile
                    .ExportFormatType = ExportFormatType.PortableDocFormat
                    .DestinationOptions = CrDiskFileDestinationOptions
                    .FormatOptions = CrFormatTypeOptions
                End With

                cryRpt.Export()

                Dim pdfFile As String = System.AppDomain.CurrentDomain.BaseDirectory & "Reports\" & fileName & "-" & System.DateTime.Now.ToString("MM-dd-yyyy") & ".pdf"


                oMsg.Attachments.Add(New Attachment(pdfFile, System.Net.Mime.MediaTypeNames.Application.Pdf))


                'oMsg.Attachments.Add(New Attachment(pdfFile))
                'Clear out the fileName and crptName variable
                fileName = ""
            Next
        Catch ex As Exception
            myEventLog("Failed to create the attachments" & ex.Message)
        End Try
    End Sub

Open in new window


Thanks in advance for your help,

SkyZipper
skyzipperAsked:
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.

Éric MoreauSenior .Net ConsultantCommented:
to create the PDF from Crystal (even if you get this part running), maybe check http://emoreau.com/Entries/Articles/2006/09/Feeding-Crystal-Reports-from-your-application.aspx and http://emoreau.com/Entries/Articles/2006/09/Feeding-Crystal-Reports-from-your-application.aspx (and specifically the section titled "Exporting a report").

To send the email, check http://emoreau.com/Entries/Articles/2007/09/Using-SystemNetMail.aspx
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
CodeCruiserCommented:
Does it work if you add an existing test file as an attachment?
0
skyzipperAuthor Commented:
I tried sending it with four attachments that were existing files and it did the same thing.
-Skyzipper
0
Éric MoreauSenior .Net ConsultantCommented:
any exceptions?
0
skyzipperAuthor Commented:
Hi Emoreau,

That is the weird part about it.  It will not return any exceptions at all.  It does run approximately 60 seconds every time and will continue on, acting like it went through.  In the past, the most it would take when I sent as Gmails SMTP would be about 10 seconds.  Do yo think timeout issue?

-Skyzipper
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
.NET Programming

From novice to tech pro — start learning today.