Description

Chilkat has a very robust object model for managing emails. A new email settings form now utilizes the Chilkat objects. The Email settings form is an SCX form designed to be used in zMax One Click. This article will focus on the new form and sending emails using Chilkat.

Integrate into a zMax App

To integrate into a zMax app do the following

Create Control to Launch the Config Screen

m.loCmd=m.loForm.AddButton("cmdEmailSetup","Setup Email",3)
m.loCmd.OnClick="cmdEmailSetup_OnClick"

The function would look something like this

***************************************
FUNCTION cmdEmailSetup_OnClick(loForm)
***************************************
	loApp=loForm.oApp
	zDOForm("emailsettingsChilKat.scx",loApp)
ENDFUNC	

The "Save" button saves the settings to our Zoom.config file

Send an Email

The code below is all you need to send an email, add CCs, and add attachments.

***************************************
FUNCTION cmdSendMailObj_OnClick(loForm)
***************************************

LOCAL loGlob AS "Chilkat_9_5_0.Global.1"
LOCAL loMailman, loEmail, lnSuccess, llSuccess, loApp, lcData, lcContentType

	loApp = loForm.oApp
	lcData = loForm.CompanyDir
	llSuccess = .t.
	
	* This example requires the Chilkat API to have been previously unlocked.
	* See Global Unlock Sample for sample code.
	loGlob = CreateObject("Chilkat_9_5_0.Global.1")  
    lnSuccess = loGlob.UnlockBundle("ZOOMGK.CB4012020_KhvgaNqE74ob")
         
	* The mailman object is used for sending and receiving email.
	loMailman = CreateObject('Chilkat_9_5_0.MailMan')

	*!*		this.DefaultFrom    = _SCREEN.Config.GetDBValue(lcData, "/config/email/smtp", "defaultfrom", "")
	*!*		this.SMTPServer     = _SCREEN.Config.GetDBValue(lcData, "/config/email/smtp", "smtpserver", "")
	*!*		this.SMTPServerPort = _SCREEN.Config.GetDBValue(lcData, "/config/email/smtp", "smtpserverport", "")
	*!*		this.UseSSL         = _SCREEN.Config.GetDBValue(lcData, "/config/email/smtp", "usessl", "")
	*!*		this.UseTLS         = _SCREEN.Config.GetDBValue(lcData, "/config/email/smtp", "usetls", "")
	*!*		this.Authenticate   = _SCREEN.Config.GetDBValue(lcData, "/config/email/smtp", "authentication", .F.)
	*!*		this.SMTPUserName   = _SCREEN.Config.GetDBValue(lcData, "/config/email/smtp", "username", "")
	*!*		this.SMTPPassword   = _SCREEN.Config.GetDBValue(lcData, "/config/email/smtp", "password", "")
	
	* Set the SMTP server.
	*loMailman.SmtpHost = "smtp.office365.com"
	loMailman.SmtpHost = _SCREEN.Config.GetDBValue(lcData, "/config/email/smtp", "smtpserver", "")
	
	*loMailman.SmtpUsername = "dasdocs@spectracompany.com"
	loMailman.SmtpUsername = _SCREEN.Config.GetDBValue(lcData, "/config/email/smtp", "username", "")
	
	*loMailman.SmtpPassword = "dd**2510**********"
	loMailman.SmtpPassword = _SCREEN.Config.GetDBValue(lcData, "/config/email/smtp", "password", "")	

	*loMailman.SmtpSsl = 1
	loMailman.SmtpSsl = _SCREEN.Config.GetDBValue(lcData, "/config/email/smtp", "usessl", "")
	
	*loMailman.SmtpPort = 587 
	loMailman.SmtpPort = _SCREEN.Config.GetDBValue(lcData, "/config/email/smtp", "smtpserverport", "")

	*loMailman.StartTLS = 1
	loMailman.StartTLS = _SCREEN.Config.GetDBValue(lcData, "/config/email/smtp", "usetls", "")

	* Create a new email object
	loEmail = CreateObject('Chilkat_9_5_0.Email')

	loEmail.Subject = "This is a test"
	loEmail.Body = "This is a test"
	loEmail.From = "dasdocs@spectracompany.com"
	*lnSuccess = loEmail.AddTo("Tim","tharris@spectracompany.com")
	lnSuccess = loEmail.AddTo("Paul Becker","Paul@ZoomGeeks.com")
	IF (loEmail.LastMethodSuccess <> 1) THEN
		MESSAGEBOX(loEmail.LastErrorText,64,loForm.oData.title)
		llSuccess = .f.
		RELEASE loMailman
	    RELEASE loEmail
		RELEASE loGlob 
		RETURN .f.
	ENDIF
	lnSuccess = loEmail.AddCC("Tim Harris","tharris@spectracompany.com")
	IF (lnSuccess  <> 1) THEN
		MESSAGEBOX(loEmail.LastErrorText,64,loForm.oData.title)
		llSuccess = .f.
		RELEASE loMailman
	    RELEASE loEmail
		RELEASE loGlob 
		RETURN .f.
	ENDIF
	
	* Add some attachments.
	* The AddFileAttachment method returns the value of the content-type it chose for the attachment.
	lcContentType = loEmail.AddFileAttachment("2663_20210817_Invoice_ProgressBilling.pdf") && Full path
	IF (lnSuccess  <> 1) THEN
		MESSAGEBOX(loEmail.LastErrorText,64,loForm.oData.title)
		llSuccess = .f.
		RELEASE loMailman
	    RELEASE loEmail
		RELEASE loGlob 
		RETURN .f.
	ENDIF


	lnSuccess = loMailman.SendEmail(loEmail)
	IF (lnSuccess <> 1) THEN
		MESSAGEBOX(loEmail.LastErrorText,64,loForm.oData.title)
	    RELEASE loMailman
	    RELEASE loEmail
		RELEASE loGlob 
		RETURN .f.
	ENDIF

	lnSuccess = loMailman.CloseSmtpConnection()
	IF (lnSuccess <> 1) THEN
		MESSAGEBOX("Connection to SMTP server not closed cleanly.",64,loForm.oData.title)
	ENDIF

	MESSAGEBOX("Mail Sent!",64,loForm.oData.title)

	RELEASE loMailman
	RELEASE loEmail
	RELEASE loGlob 
	
	RETURN m.llSuccess 

ENDFUNC