C1PrintDocument to Memory Stream as MailMessage Attachment

Posted by: mikemacguyver on 31 May 2019, 12:00 pm EST

    • Post Options:
    • Link

    Posted 31 May 2019, 12:00 pm EST

    Attempted to attach a c1printdocumment to a mail message via a memory stream.

    The pdf comes across three and half times the size of the actual document if exported to a file.

    When attempting to open the document it states it’s corrupted.

    What can be done to correct this issue?

    Thanks,

    Mike

    Code:

    Public Sub EmailReg(ByVal recipient As String, ByVal sender As String, doc As C1PrintDocument)

    Try

    Using client As New SmtpClient

    Using email As New MailMessage

    email.From = New MailAddress(sender)

                    email.To.Add(New MailAddress("myemail@mybusiness.com"))
    
                    email.Subject = "Registration Code"
    
                    email.Body = "Please review the attached document For a registration code."
    
                    Using ms As New System.IO.MemoryStream
                        Dim provider = New C1.C1Preview.Export.PdfExportProvider()
    
                        ms.Position = 0
                        doc.Export(ms, provider)
    
                        email.Attachments.Add(New Attachment(ms, "Registration.PDF", "application/pdf"))
    
                        client.Host = "mail.mybusiness.com"
                        client.Port = 25
                        client.Send(email)
                    End Using
    
                End Using
            End Using
    
        Catch ex As Exception
            SendErrorEmail(ex.ToString())
        End Try
    End Sub
    
  • Posted 31 May 2019, 2:35 pm EST

    Fixed it!

    Public Sub EmailReg(ByVal recipient As String, ByVal sender As String, doc As C1PrintDocument)
        Try
            Using client As New SmtpClient
                Using email As New MailMessage
                    email.From = New MailAddress(sender)
    
                    email.To.Add(New MailAddress("email@mybusiness.com"))
    
                    email.Subject = "Registration Code"
    
                    email.Body = "Please review the attached document For a registration code."
    
                    Using ms As New System.IO.MemoryStream
                        Dim provider = New C1.C1Preview.Export.PdfExportProvider()
    
                        ms.Position = 0
                        doc.Export(ms, provider)
    
                        email.Attachments.Add(New Attachment(New MemoryStream(ms.ToArray), "Registration.PDF"))
    
                        client.Host = "mail.mybusiness.com"
                        client.Port = 25
                        client.Send(email)
                    End Using
    
                End Using
            End Using
    
        Catch ex As Exception
            SendErrorEmail(ex.ToString())
        End Try
    End Sub
    
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels