Code:
'Simple CDO Example for sending HTML-based email. 'CDO.INC is not included, just prepare your own from 'the PB COM Browser, I used: 'Microsoft CDO for NTS 1.2 Library on Windows 2000 Pro 'Feel free to use this code in any derivative you would like. 'Author: Ken Myers 'Date: 10/06/2003 'PBCC 3.0 Version. Feel free to change STDOUT below to MSGBOX for PBWIN 7.0. 'FYI, I use it via JNI and the CoInitialize is very handy to know for other 'JNI bridge makers. [img]http://www.powerbasic.com/support/forums/smile.gif[/img] #COMPILE EXE #INCLUDE "win32api.inc" #INCLUDE "cdo.inc" FUNCTION PBMAIN() AS LONG ON ERROR GOTO errtrap: CALL CoInitialize(BYVAL 0) DIM myMail AS CDONTSNewMail DIM A AS ASCIIZ * 1024 SET myMail = NEW CDONTSNewMail IN $PROGID_CDONTSNewMail1 IF ISFALSE ISOBJECT(myMail)THEN GOTO errtrap: END IF DIM HTML AS STRING HTML = "<!DOCTYPE HTML PUBLIC ""-//IETF//DTD HTML//EN"">" & $CRLF HTML = HTML & "<html>" HTML = HTML & "<head>" HTML = HTML & "<title>CDO Magic</title>" HTML = HTML & "</head>" HTML = HTML & "<body>" HTML = HTML & "<b>This</b> is a test email in HTML format. <BR></body>" HTML = HTML & "</html>" DIM vVnt AS VARIANT DIM vBody AS VARIANT DIM vSubject AS VARIANT DIM vTo AS VARIANT DIM vFrom AS VARIANT DIM vBodyFormat AS VARIANT DIM vMailFormat AS VARIANT DIM vAttach AS VARIANT 'Change the from/to email addresses! vFrom = "[email protected]" vTo = "[email protected]" vSubject = "Sample Message" vBodyFormat = 0 vMailFormat = 0 vBody = HTML 'Make sure you use the absolute path to the file if you want to attach one! 'vAttach = "c:\test.txt" 'FYI, Really should test all string values before executing the LET... If trim$(VARIANT$(vFrom))<>"" Then OBJECT LET myMail.From = vFrom End If OBJECT LET myMail.To = vTo OBJECT LET myMail.Subject = vSubject OBJECT LET myMail.BodyFormat = vBodyFormat OBJECT LET myMail.MailFormat = vMailFormat OBJECT LET myMail.Body = vBody 'OBJECT CALL myMail.AttachFile(vAttach) TO vVnt OBJECT CALL myMail.Send TO vVnt SET myMail = NOTHING CALL CoUninitialize() STDOUT "Success!" WAITKEY$ EXIT FUNCTION errtrap: 'I like showing the more descriptive error during 'testing. CALL FormatMessage(%FORMAT_MESSAGE_FROM_SYSTEM, _ BYVAL 0&, OBJRESULT, BYVAL MAKELANGID( _ %LANG_NEUTRAL, %SUBLANG_DEFAULT), _ A, SIZEOF(A), BYVAL 0&) STDOUT A STDOUT "Failure " & ERROR$ WAITKEY$ EXIT FUNCTION END FUNCTION
[This message has been edited by Ken Myers (edited October 06, 2003).]
Comment