This section describes how one can easily send an e-mail via SMTP with attached converted file.
Note
The provided code snippets have been written in JavaScript and can be found in the PDFCreator directory under “COMScripts/JS Scripts/How To”. The following code lines have been taken out ouf the EmailSmtp.js script and therefore can be found and executed there.
Get a reference to the job queue with the ActiveXObject(“ProgID”) method. Then call the Initialize() method with your COM Object(which is actually the job queue).
var PDFCreatorQueue = new ActiveXObject("PDFCreator.JobQueue");
PDFCreatorQueue.Initialize();
//Sets up the path where the converted pdf file should be saved in
var fullPath = objFSO.GetParentFolderName(WScript.ScriptFullname) + "\\TestPage.pdf";
Call WaitForJob(int timeOut) if you are waiting for one print job to get in the queue. The parameter timeOut specifies the maximum time in seconds the queue waits for the print job to arrive. This method is very important because each newly triggered print job will take some time until it gets from the printer queue to the PDFCreator job queue. If you call the NextJob property without waiting for the job it may happen that you get null although the print job was triggered some lines before.
if(!PDFCreatorQueue.WaitForJob(10))
{
WScript.Echo("The print job did not reach the queue within 10 seconds");
}
Now you are able to get the next job in the queue by using the property NextJob. With that you receive the topmost job of the queue.
var job = PDFCreatorQueue.NextJob;
Setup the profile of the job with SetProfileByGuid(guid). The guid parameter is a string type that is used to assign the appropriate conversion profile(see enumeration in Basics/SingleConversion section). Note: Alternatively, you can use SetProfileByName(“MyNameForACertainProfile”). For the standard profiles it holds that their guids are equal to their names.
job.SetProfileByGuid("DefaultGuid");
Set up the e-mail SMTP settings using the SetProfileSetting method of the job object. All variables used in the following were introduced in the beginning.
//Since we want to send the converted file, we have to add the EmailSmtp action
job.AddAction("EmailSmtp");
//Setting up the address that should be displayed as sender.
job.SetProfileSetting("EmailSmtp.Address", address);
//Setting up the recipients
job.SetProfileSetting("EmailSmtp.Recipients",recipients);
//Setting up the SMTP server port
job.SetProfileSetting("EmailSmtp.Port",port);
//User name for authentication at server
job.SetProfileSetting("EmailSmtp.UserName",username);
//Password for authentication at server
job.SetProfileSetting("EmailSmtp.Password",password);
//Setting up host name of the e-mail server
job.SetProfileSetting("EmailSmtp.Server",server);
//Setting up the subject of the mail
job.SetProfileSetting("EmailSmtp.Subject",subject);
For more information on e-mail SMTP settings see the **Reference
Manual/Settings** section.
Now the job is ready to be converted. Thereafter it will be send.
//fullPath was introduced above
job.ConvertTo(fullPath);
The property IsFinished informs about the conversion state. If the print job is done, IsFinished returns true. If you want to know whether the job was successfully done, consider the property IsSuccessful.
It returns true if the job was converted successfully otherwise false.
if(!job.IsFinished || !job.IsSuccessful)
{
WScript.Echo("Error in process");
}
After having used the COM object do not forget to release it by using ReleaseCom() otherwise no other PDFCreator instances will be able to work.
PDFCreatorQueue.ReleaseCom();