Print Page | Close Window

Emailing previously created PDFs

Printed From: www.exp-systems.com
Category: PDF reDirect
Forum Name: Programming
Forum Discription: VBA and Batch Tools to control PDF reDirect Pro
URL: http://www.exp-systems.com/Forum_exp/forum_posts.asp?TID=597
Printed Date: 20 Apr 24 at 8:42AM


Topic: Emailing previously created PDFs
Posted By: David_Carle
Subject: Emailing previously created PDFs
Date Posted: 26 Apr 10 at 4:48PM
In your sample code for ver 2.2.8, the comments state: "If you wish to FTP or e-mail the PDF to someone, I would recommend
that you do so manually after the PDF has been created. This
ensure better control over the creation process flow. This sample code
shows an example of that"
 
However, I don't see anything in the sample code to achieve this.  I have successfully printed the reports to PDF, and would now like to email them as a separate process.
 
I've seen the Utility_FTP_Upload_File method - is there an equivalent option to send an existing file by email?
 
Or, can you give me some sample code to achieve this.
 
Thanks
 
P.S. I believe you referred to this in your post of 28 Nov 06 at 3:45AM, but I don't see a follow up.



Replies:
Posted By: Michel_K17
Date Posted: 27 Apr 10 at 1:50AM
Hi,

   Yes, you are right. I just assumed that foe e-mails, you can use any of the free (or pay) e-mail engines out there.

   Here is some code that you can use (you'll need to do some modifications). It uses the Windows CDOSYS component that should be available in 99% of Windows machines out there.

   Cheers!

Michel

----------------------

Private Function SendSmtpMailUsingCdo() As Boolean
    Dim loConfig As Object  ' New CDO.Configuration
    Dim loMsg As Object     ' New CDO.Message
'    Dim loError As Object
'    Dim lcErr As String
    Dim MyFiles() As String
   
    On Error Resume Next
   
    ' Try to create the CDO object (may not be there on newer machines)
    Err.Clear
    Set loConfig = CreateObject("CDO.Configuration")
    Set loMsg = CreateObject("CDO.Message")
   
    If Err.Number <> 0 Then
        olog.LogError True, Err.Number, Err.Description, Err.LastDllError, "SendSmtpMailUsingCdo"
        olog.WriteLog "Try alternate e-mail engine"
        Err.Clear
        Exit Function
    End If
   
    olog.WriteLog "   - Use the CDO SMTP E-Mail engine"
    With loConfig.Fields
       
        .Item(CDO_SCHEMAS & "smtpserver") = Prefs.Mail_SMTP_Server
        .Item(CDO_SCHEMAS & "smtpserverport") = Prefs.Mail_SMTP_Server_Port ' 587 or 25
       
        ' sendusing
        ' Values:
        '    cdoSendUsingPickup = 1
        '    cdoSendUsingPort = 2
        '    cdoSendUsingExchange = 3
        ' Remarks: Use the CdoSendUsing Enum to set this value. If the SMTP service is installed
        '          on the local computer, then the value defaults to cdoSendUsingPickup (1).
        '          Otherwise, if Microsoft Outlook Express is installed, the value defaults to
        '          cdoSendUsingPort (2) and the settings from the default account are used.
        .Item(CDO_SCHEMAS & "sendusing") = 2
       
        ' Authentication. Acceptable values are:
        '   cdoAnonymous = 0 - Perform no authentication.
        '   cdoBasic = 1 - Use the basic (clear text) authentication mechanism.
        '   cdoNTLM = 2 - Use the NTLM authentication mechanism (http://en.wikipedia.org/wiki/NTLM)
        ' as opposed to Prefs.Mail_Authentication: 0 = None, 1 = POP, 2 = Login, 3 = Plain
        '.Item(CDO_SCHEMAS & "smtpauthenticate") = True
        If Prefs.Mail_Authentication = 0 Then
            .Item(CDO_SCHEMAS & "smtpauthenticate") = cdoAnonymous
        Else
            .Item(CDO_SCHEMAS & "smtpauthenticate") = cdoBasic
        End If
       
        ' SSL Support (I choose not to support this for now)
        .Item(CDO_SCHEMAS & "smtpusessl") = 0 ' False
       
        ' Username and Password
        .Item(CDO_SCHEMAS & "sendusername") = Prefs.Mail_Username
        .Item(CDO_SCHEMAS & "sendpassword") = Decrypt(lblMail(6).Caption, fUnicodeUser)
       
        ' Timeout value
        .Item(CDO_SCHEMAS & "smtpconnectiontimeout") = 10 ' 10 second timeout
       
        ' Register the changes w the CDO component
        .Update
    End With
   
    With loMsg
        Set .Configuration = loConfig
       
        If fUnicodeUser Then
            .BodyPart.Charset = "unicode-1-1-utf-8"
        End If
       
        .From = Prefs.Mail_From
        .To = lblMail(0).Caption
        .CC = lblMail(1).Caption
        .BCC = lblMail(2).Caption
        .Subject = lblMail(3).Caption
        .TextBody = lblMail(4).Caption
       
        ' Attachments
        olog.WriteLog "   - Attaching Files...."
        MyFiles = Split(lblMail(5).Caption, ";")
        For i = 0 To UBound(MyFiles)
            olog.WriteLog "     >>" & GetFilePart(MyFiles(i))
            .AddAttachment MyFiles(i)
        Next
       
        Err.Clear
        .Send
       
        ' Check for Errors
        If Err.LastDllError = 0 Then
            ' Success
            olog.WriteLog SUCCESS
            TempStr = str$(True)
            SendSmtpMailUsingCdo = True
        Else
            TempStr = Trim$(str$(Err.LastDllError))
            olog.WriteLog FAILED & " CDO Mail Engine Error Code is:" & TempStr
            olog.LogError True, Err.Number, Err.Description, Err.LastDllError, "SendSmtpMailUsingCdo"
        End If
   
    End With

End Function




Posted By: David_Carle
Date Posted: 29 Apr 10 at 4:18PM
Hi Michel
 
Thanks for the suggested code.  It  does seem to be out of context and doesn't run as is (as you stated it does need modifications).
 
Rather than figuring out what changes were needed, I found a sample of code to create an Outlook email with attachment from Access at the following website:
http://www.peterssoftware.com/c_som.txt - http://www.peterssoftware.com/c_som.txt
 
(Thanks Peter!)
 
As an enhancement request, would you please consider implementing a Utility method (like the one for FTP) to send documents (eg PDFs) as email attachments?
 
Thanks, David
 


Posted By: Michel_K17
Date Posted: 02 May 10 at 10:17PM
Hi David,

   Yes, the outlook sample code is a great alternative for those people that have it (I had assumed you wanted the more generic code).

   Yes, I will consider it for the next version. Indeed, for the next version, I will be creating a common ActiveX component which will be shared between PDF reDirect and for the VBA sample code so that I do not need to port the code from one to the other.

   Cheers!

Michel


-------------
Michel Korwin-Szymanowski
EXP Systems LLC



Print Page | Close Window