0
Thanks

A few words of thanks would be greatly appreciated.

VB6 - VBA Send emails using the CDO object





Author: lermite222
Published on: 18/04/2013

If the host PC does not have theOutlook application installed you can use the CDO object to send your mails.

The disadvantage is that with CDO must be configured with your server settings, some host like Gmail requires authentication (password) and a secure connection (SSL)

This demo implements all the necessary parameters.


Sub DEMO_EnvoiMailCDO()  
Dim mMessage As Object  
Dim mConfig As Object  
Dim mChps  
      
    Set mConfig = CreateObject("CDO.Configuration")  
      
    mConfig.Load -1  
    Set mChps = mConfig.Fields  
    With mChps  
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2  
        'Adapter suivant votre serveur de mail. (exemple pour Gmail.)=> Hormail "smtp.live.com"  
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"  
        'En principe, 25 fonctionne avec tout les serveurs.  
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25  
          
        'Vous pouvez essayer sans ces trois lignes  
        'Mais si votre serveur demande une authentification,  
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = "1"  
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "MyMail@gmail.com"  
        .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "MyCodePass"  
        'Si votre serveur demande une connexion sûre (SSL)  
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = "true"  
        .Update  
    End With  
      
    Set mMessage = CreateObject("CDO.Message")  
    With mMessage  
    Set .Configuration = mConfig  
        .To = "LeClient@hotmail.com"  
        .From = "MyMail@gmail.com"  
        .Subject = "Le sujet du mail"  
        .TextBody = "Ce mail vous est envoyer pour tester la macros de lermite"  
        'Pour ajouter une pièce jointe, un fichier, classeur, image etc.  
        '.AddAttachment 'Chemin et nom complet du fichier à joindre  
        .Send  
    End With  
    Set mMessage = Nothing  
      
    'Pour un autre message, pas besoin de tout reconfigurer, il faut toutefois recréer un nouveau  
    'message à chaque fois.  
    Set mMessage = CreateObject("CDO.Message")  
    With mMessage  
    Set .Configuration = mConfig  
        .To = "AutreClient@gmail.com"  
        .From = "MyMail@gmail.com"  
        .Subject = "C'est pour le deuxième test d'envoi mail"  
        .TextBody = "Ce mail vous est envoyer pour tester la macros de lermite" & Chr(13) _  
            & "et voir si le deuxième message est bien passer."  
        'Pour ajouter une pièce jointe, un fichier, classeur, image etc.  
        '.AddAttachment 'Chemin et nom complet du fichier à joindre  
        .Send  
    End With  
    Set mMessage = Nothing  
     
    'Libère les ressources  
    Set mConfig = Nothing  
    Set mChps = Nothing  
End Sub


You can download the demo file (Excel):
Send emails using CDO.xls
0
Thanks

A few words of thanks would be greatly appreciated.

Ask a question
Jean-François Pillou

CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jeff Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.

Learn more about the CCM team

Original article published by . Translated by deri58. Latest update on by deri58.

This document, titled "VB6 - VBA Send emails using the CDO object," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (https://ccm.net/).

0 Comments