Page 1 of 2

SMTP mailer supports html, Attachments, in AND secure GMAIL

Posted: Fri Nov 07, 2008 8:33 pm
by KIKI
Version 1.1
- New configuration possibility
- Another way to send a message with importance to solve a problem
Version 1.2 2008,December,11Th
- Support inline attachments and image


Code: Select all

  ;======================================================================================== 
  ;* 
  ;* 
  ;* Use CDO Object for sending mail with Gmail or any else smtp provider 
  ;* Credit and Thanks to Jos http://www.autoitscript.com/forum/index.php?showtopic=23860 
  ; who write this script in  Autoit language and that i ported on 
  ; Purebasic with the help of SROD with his excellent library COMATE 
  ; allow sending secure email, attachments, replY to, notification disposition and more if you want...... 
   ;And much more configuration 
  ;=================================================================================== 
    
    IncludePath ".\essaicomate" 
    XIncludeFile "COMate.pbi" 
    ;=============================
    ;For the priority
    ;============================
    #cdoPriorityNonUrgent= -1  ; Nonurgent priority 
    #cdoPriorityNormal= 0        ; Normal priority 
    #cdoPriorityUrgent= 1         ;Urgent priority 
    ;===============================
    ;For the importance of the mail
    ;===============================
    #cdoLow= 0                    ;Low importance    
    #cdoNormal= 1                 ;Normal importance 
    #cdoHigh= 2                   ;High importance 
    ;====================================
    ;For the sensitivity of the mail 
    ;=================================
    #cdoSensitivityNone= 0   ; None
    #cdoPersonal = 1 ; Personal
    #cdoPrivate = 2  ; Private
    #cdoCompanyConfidential = 3 ; Company Confidential
    
    ;================================================
    ; Beginning of paramters
    ;================================================
  SmtpServer.s = "smtp.xx.com"              ; address for the smtp-server to use - REQUIRED 
  FromName.s = "Jean DURAND"                      ; name from who the email was sent 
  FromAddress.s = "jdurand@gmail.com"     ; address from where the mail should come 
  ToAddress.s = "jdupond@wanadoo.fr"   ; destination address of the email - REQUIRED 
  Subject.s = "HTML With attachments"                   ; subject from the email - can be anything you want it to be 
  Body.s = "" 
 BodyHtml.s="<Body background='cid:Fond4.gif' style='background-attachment: fixed; background-repeat: repeat'>"
     BodyHtml = BodyHtml + "Bonjour<BR> Essai<BR><BR><IMG src='cid:medecin.gif' witdh '546' height='100'>"
 bodyHtml = ReplaceString(bodyhtml,"'","$0027")           
  AttachFiles.s = ""                       ; the files you want to attach- leave you can put multi files separeted by a semicolon ";' ; blank if not needed 
  CcAddress.s = "CCadress1@test.com"       ; address for cc - leave blank if not needed 
  BccAddress.s = "BCCadress1@test.com"     ; address for bcc - leave blank if not needed 
  Username.s = "jeandurand@gmail.com"                    ; username for the account used from where the mail gets sent - REQUIRED 
  Password.s = "jean"                  ; password for the account used from where the mail gets sent - REQUIRED 
  IPPort.l = 465                            ; port used for sending the mail ex 25 for a normal provider 465 for gmail 
  ssl.l = 1                              ;0 if you don't use ssl and 1 if you use ssl ex from gmail 
  AttachFiles= "" ;listing of attach files you want to send 
  replyto.s = "toto@9toto.fr"  ;where you want To have the reply of these mail 
  dispositionnotificationTo.s = "toto@toto.fr" ;if you want to have a notification 
  ReturnreceiptTo.s="titi@gmail.com"  ; where you want to have the receipt  of these mail 
  Importance.l = #cdoHigh                   ;High importance; Send message priority: "High=2", "Normal"=1, "Low=0"
  Priorite.l =  #cdoPriorityUrgent         ;Urgent priority 
  sensitivity.l = #cdoCompanyConfidential   ; see constants 
  transfert.l = 0                       ; 0 Ne pas transférer 1 Transfert libre
  
    ;=======================================================
    ; End of paramters
    ;======================================================
    Define.COMateObject objemail,iconf,objbodypart,objbodypart1
    
    objemail = COMate_CreateObject("CDO.Message") 
    iConf = COMate_CreateObject("CDO.Configuration")
    If objemail
      objemail\SetProperty("Configuration="+ Str(iconf)+ " as COMateObject"  ) 
 objemail\SetProperty("MimeFormatted=1" ) 
      objemail\SetProperty("to='"+ ToAddress +"'") 
      objemail\SetProperty("from='"+Chr(34)+ FromName + Chr(34)+  "<" + FromAddress + ">" +"'")
      objemail\SetProperty("Subject='"+Subject+"'") 
      objemail\SetProperty(" HTMLBody="+"'"+BodyHTML+"'") 
      objemail\SetProperty("replyTo ='"+replyto+"'") 
      If CcAddress <> "" 
        objemail\SetProperty("Cc ='"+ CcAddress +"'") 
      EndIf 
      If BccAddress <> "" 
        objemail\SetProperty("Bcc ='"+ BccAddress +"'") 
      EndIf
      objemail\SetProperty("Configuration\Fields\Item ('http://schemas.microsoft.com/cdo/configuration/sendusing') = 2" )
      objemail\SetProperty("Configuration\Fields\Item ('http://schemas.microsoft.com/cdo/configuration/smtpserver') =" +"'"+ SmtpServer+"'" ) 
      If IPPort = 0
        IPPort = 25
      EndIf 
      objemail\SetProperty("Configuration\Fields\Item ('http://schemas.microsoft.com/cdo/configuration/smtpserverport') ="+ Str(IPPort))
      ;Authenticated SMTP
      If Username <> "" 
        objemail\SetProperty("Configuration\Fields\Item ('http://schemas.microsoft.com/cdo/configuration/smtpauthenticate') =1")
        objemail\SetProperty("Configuration\Fields\Item ('http://schemas.microsoft.com/cdo/configuration/sendusername') ="+ "'"+ Username+"'")
        objemail\SetProperty("Configuration\Fields\Item ('http://schemas.microsoft.com/cdo/configuration/sendpassword') =" +"'"+ Password +"'" )
      EndIf
      If ssl 
        objemail\SetProperty("Configuration\Fields\Item ('http://schemas.microsoft.com/cdo/configuration/smtpusessl') = 2" )
      EndIf
      ;Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
      objemail\SetProperty("Configuration\Fields\Item ('http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout') = 60")
      ;==================
      ; sensitivity
      ;================
      Select sensitivity
        Case  #cdoCompanyConfidential 
          ;  objemail\SetProperty("Fields\Item('urn:schemas:mailheader:Sensitivity') = "+"'"+ "Company-Confidential"+"'")
        Case #cdoPersonal
          ;objemail\SetProperty("Fields\Item('urn:schemas:mailheader:Sensitivity') = "+"'"+ "Private"+"'")
        Case  #cdoPrivate 
          ;  objemail\SetProperty("Fields\Item('urn:schemas:mailheader:Sensitivity') = "+"'"+ "Personal"+"'") 
        Case #cdoSensitivityNone
          objemail\SetProperty("Fields\Item('urn:schemas:mailheader:Sensitivity') = "+"'"+ "None"+"'") 
          Debug COMate_GetLastErrorDescription()
      EndSelect         
      
      Select transfert
        Case 0
          objemail\SetProperty("Fields\Item('urn:schemas:mailheader:X-Message-Flag') = "+"'"+"Ne pas Transférer"+"'")
        Case 1
          objemail\SetProperty("Fields\Item('urn:schemas:mailheader:X-Message-Flag') = "+"'"+"Transfert Libre"+"'")
      EndSelect     
      objemail\invoke("Configuration\Fields\Update")
  
      Select Priorite
        Case     #cdoPriorityUrgent
          objemail\SetProperty("Fields('urn:schemas:mailheader:X-MSMail-Priority') = 'High'")
          objemail\SetProperty("Fields('urn:schemas:mailheader:X-Priority') = " +Str(#cdoPriorityUrgent) ) 
          objemail\SetProperty("Fields('urn:schemas:mailheader:priority') = " +Str(#cdoPriorityUrgent) ) 
        Case     #cdoPriorityNormal
          objemail\SetProperty("Fields('urn:schemas:mailheader:X-MSMail-Priority') = 'Normal'")
          objemail\SetProperty("Fields('urn:schemas:mailheader:X-Priority') = "+Str(#cdoPriorityNormal) ) 
          objemail\SetProperty("Fields('urn:schemas:mailheader:priority') = " +Str(#cdoPriorityNormal) ) 
        Case     #cdoPriorityNonUrgent 
          objemail\SetProperty("Fields('urn:schemas:mailheader:X-MSMail-Priority') = 'Low'")
          objemail\SetProperty("Fields('urn:schemas:mailheader:X-Priority') ="+ Str(#cdoPriorityNonUrgent) ) 
          objemail\SetProperty("Fields('urn:schemas:mailheader:priority') = " +Str(#cdoPriorityNonUrgent) ) 
      EndSelect 
      
      Select Importance
        Case #cdoHigh
          objemail\SetProperty("Fields('urn:schemas:httpmail:importance') = " +Str(#cdoHigh) )
        Case #cdoNormal
          objemail\SetProperty("Fields\Item('urn:schemas:httpmail:importance') = " +Str(#cdoNormal) )
        Case #cdoLow
          objemail\SetProperty("Fields('urn:schemas:httpmail:importance') = " +Str(#cdoLow) )
      EndSelect 
      objemail\SetProperty("Fields('urn:schemas:mailheader:disposition-notification-to')=" +"'"+dispositionnotificationTo+"'" )
      objemail\SetProperty("Fields('urn:schemas:mailheader:return-receipt-to')="+"'"+ReturnreceiptTo + "'")
      ;Update settings
       objemail\invoke("Fields\Update")
;Declaring inline attachments
 objbodypart=  objemail\getObjectProperty("AddRelatedBodyPart('C:\Documents and Settings\Administrateur\Bureau\fond4.gif','fond4.gif',1)" )
    ;  Debug    COMate_GetLastErrorDescription()
      objbodypart\SetProperty("Fields('urn:schemas:mailheader:Content-ID') ="+"'<fond4.gif>'")
      objbodypart\invoke("Fields\Update")
     objbodypart1=  objemail\getObjectProperty("AddRelatedBodyPart('C:\Documents and Settings\Administrateur\Bureau\medecin.gif','medecin.gif',1)" )
      objbodypart1\SetProperty("Fields('urn:schemas:mailheader:Content-ID') ="+"'<medecin.gif>'")
      objbodypart1\invoke("Fields\Update")
      ; Sending attach files 
      If AttachFiles <> "" 
        Resultat.l = CountString(AttachFiles, ";")+1
        For x = 1 To Resultat
          Files2Attach.s=StringField(AttachFiles, x, ";")
          objemail\invoke("AddAttachment (" +"'"+Files2Attach+"'"+")"  )
        Next
      EndIf 
      ;   objEmail\Setproperty("DSNOptions = 14")
      objemail\invoke("Send")
      objemail\Release()
  iconf\Release()
      objbodypart\Release()
      ,objbodypart1\Release()
    Else 
      MessageRequester("Désolé - Erreur de création de l'objet", COMate_GetLastErrorDescription()) 
    EndIf
   

Posted: Fri Nov 07, 2008 8:37 pm
by srod
Very interesting Kiki - nice. :)

Posted: Sat Nov 08, 2008 3:49 am
by JCV
Using

Code: Select all

 tags would be much better. 
thx for the code.  8)

Posted: Sat Nov 08, 2008 8:10 am
by KIKI
JCV wrote:Using

Code: Select all

 tags would be much better. 
thx for the code.  8)[/quote]
Done today.
If you have idea about others  email header ?

Posted: Sat Nov 08, 2008 3:35 pm
by QuimV
:) Thanks for your code KIKI, but ...

I can't :( set the priority of the sent mails to High.

Anybody idea to fix it? ...

Tanks in advanced.

Posted: Sat Nov 08, 2008 10:21 pm
by KIKI
QuimV wrote::) Thanks for your code KIKI, but ...

I can't :( set the priority of the sent mails to High.

Anybody idea to fix it? ...

Tanks in advanced.
For my part i am on Windows XP SP3 French version and i have no problem to send a mail whith high importance
What is your version of windows ?

Posted: Sun Nov 09, 2008 10:28 am
by QuimV
:)
The same (XP+SP3) but Spanish version

Posted: Sun Nov 09, 2008 11:07 am
by KIKI
QuimV wrote::)
The same (XP+SP3) but Spanish version
I will try to find another way by configuration, after i will give it to you

Posted: Sun Nov 09, 2008 12:40 pm
by QuimV
:D
Thanks a lot KIKI for your help.
Best Regards

Posted: Sun Nov 09, 2008 3:40 pm
by KIKI
QuimV wrote::D
Thanks a lot KIKI for your help.
Best Regards
Quinv,
Try the new version (see first post) and if it doesn't works you have to say me what is your mailer installed on your computer.
Thanks

Posted: Sun Nov 09, 2008 7:11 pm
by QuimV
:D It runs!!! KIKI

There are one very little mistake in you code. The variable Importance is declared twice, as long and as string. I have made only 2 very small changes:

Importance.l = #cdoHigh
changed to
ImportanceN.l = #cdoHigh

and

Select Importance
changed to
Select ImportanceN

:D Congratulations and thanks a lot for your time and help.

BTW. In the mail received, I can't see the text "Write whatever you want" assigned in
BodyHTML.s = "<B>Write whatever you want</B>"
Have you any idea about this behavior?

Posted: Sun Nov 09, 2008 8:25 pm
by KIKI
QuimV wrote::D It runs!!! KIKI

There are one very little mistake in you code. The variable Importance is declared twice, as long and as string. I have made only 2 very small changes:

Importance.l = #cdoHigh
changed to
ImportanceN.l = #cdoHigh

and

Select Importance
changed to
Select ImportanceN

:D Congratulations and thanks a lot for your time and help.

BTW. In the mail received, I can't see the text "Write whatever you want" assigned in
BodyHTML.s = "<B>Write whatever you want</B>"
Have you any idea about this behavior?
replace by

Code: Select all

objemail\SetProperty(" HTMLBody="+"'"+BodyHTML+"'")
and after it's works
The example was modified with your two remarks and corrected for accepting HTML

Posted: Sun Nov 09, 2008 10:45 pm
by QuimV
:D Great!
Thanks again KIKI

Posted: Mon Nov 10, 2008 10:01 am
by Kukulkan
Hi KIKI,

This seem to be great. Thank your for the code. But I have some questions:

- What is the minimum requirement for this? Is it working without Office (Outlook) installed, too? On a plain fresh windows?
- What OS are supported? What about NT4 and W2000?
- This codes utilizes CDO.Message object. Searching google finds a lot of people having trouble with "Could not access 'CDO.Message' object" error messages. How reliable is this in a commercial program?

If this works reliable and good on all windows NT4 and up, without the need of an installed Office etc., this will be a really great solution.

Kukulkan

Posted: Mon Nov 10, 2008 1:49 pm
by gnozal
Kukulkan wrote:- What is the minimum requirement for this? Is it working without Office (Outlook) installed, too? On a plain fresh windows?
- What OS are supported? What about NT4 and W2000?
MSDN says :

Windows 2000 and Windows XP: CDOSYS is enabled by default.
Windows NT 4.0: CDO NTS must be installed from the Windows NT 4.0 SDK.

http://msdn.microsoft.com/en-us/library/aa505938.aspx
http://msdn.microsoft.com/en-us/library/aa142523.aspx