Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(DataFlex) Send HTML Email with AttachmentsDemonstrates how to send an HTML email with file attachments.
Use ChilkatAx-win32.pkg Procedure Test Handle hoMailman Variant vEmail Handle hoEmail Boolean iSuccess String sContentIdStarfish Handle hoSbHtml Integer iNumReplacements Boolean iSuccess String sContent String sTemp1 Boolean bTemp1 // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. Get Create (RefClass(cComChilkatMailMan)) To hoMailman If (Not(IsComObjectCreated(hoMailman))) Begin Send CreateComObject of hoMailman End Set ComSmtpHost Of hoMailman To "smtp.my-starttls-mail-server.com" Set ComSmtpUsername Of hoMailman To "MY-SMTP-USERNAME" Set ComSmtpPassword Of hoMailman To "MY-SMTP-PASSWORD" Set ComStartTLS Of hoMailman To True Set ComSmtpPort Of hoMailman To 587 // Create a new email object Get Create (RefClass(cComChilkatEmail)) To hoEmail If (Not(IsComObjectCreated(hoEmail))) Begin Send CreateComObject of hoEmail End Set ComSubject Of hoEmail To "Test SMTP API to Send HTML Email with Attachments" Set ComFrom Of hoEmail To "Joe Programmer <joe@my-starttls-mail-server.com>" Get ComAddTo Of hoEmail "Chilkat Support" "support@chilkatsoft.com" To iSuccess // Add a plain-text alternative body, which will likely never be seen. // (It is shown if the receiving email client is incapable of displaying HTML email.) Get ComAddPlainTextAlternativeBody Of hoEmail "This is a plain-text alternative body..." To iSuccess // Our HTML will include an image, so add the related image here. Get ComAddRelatedFile Of hoEmail "qa_data/jpg/starfish.jpg" To sContentIdStarfish Get ComLastMethodSuccess Of hoEmail To bTemp1 If (bTemp1 <> True) Begin Get ComLastErrorText Of hoEmail To sTemp1 Showln sTemp1 Procedure_Return End // The src attribute for the image tag is set to the contentIdStarfish: Get Create (RefClass(cComChilkatStringBuilder)) To hoSbHtml If (Not(IsComObjectCreated(hoSbHtml))) Begin Send CreateComObject of hoSbHtml End Get ComAppend Of hoSbHtml "<html><body><p>This is an HTML email with an embedded image.</p>" To iSuccess Get ComAppend Of hoSbHtml '<p><img src="cid:CONTENT_ID_STARFISH" /></p></body></html>' To iSuccess Get ComReplace Of hoSbHtml "CONTENT_ID_STARFISH" sContentIdStarfish To iNumReplacements Get ComGetAsString Of hoSbHtml To sTemp1 Get ComAddHtmlAlternativeBody Of hoEmail sTemp1 To iSuccess // Finally, add some attachments to the email. // Add a file attachment. Get ComAddFileAttachment2 Of hoEmail "qa_data/pdf/fishing.pdf" "application/pdf" To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoEmail To sTemp1 Showln sTemp1 Procedure_Return End // Add an attachment where the content is contained in a string. Move "This is the content of the 2nd attached file." To sContent Get ComAddStringAttachment Of hoEmail "someText.txt" sContent To iSuccess // Send the HTML email. Get pvComObject of hoEmail to vEmail Get ComSendEmail Of hoMailman vEmail To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoMailman To sTemp1 Showln sTemp1 Procedure_Return End Get ComCloseSmtpConnection Of hoMailman To iSuccess If (iSuccess <> True) Begin Showln "Connection to SMTP server not closed cleanly." End Showln "HTML email with attachments sent!" End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.