IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Outlook Discussion :

Ajout image dans le corps du message


Sujet :

Outlook

  1. #1
    Membre régulier
    Homme Profil pro
    Développeur
    Inscrit en
    Octobre 2014
    Messages
    322
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Saône et Loire (Bourgogne)

    Informations professionnelles :
    Activité : Développeur
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2014
    Messages : 322
    Points : 124
    Points
    124
    Par défaut Ajout image dans le corps du message
    Bonjour,

    Je viens vers vous car j'ai besoin d'envoyer des mails contenant une image par programmation.

    J'avais trouvé un lien sur ce site avec un exemple en VBA mais je ne retrouve plus le lien.

    Je me rappelle seulement qu'ils parlaient de l'image en pièce-jointe et du CID.

    J'ai seulement besoin du code pour ça, et quel que soit le langage, je serait capable de l'adapter avec le langage utilisé.

  2. #2
    Expert éminent
    Avatar de Oliv-
    Homme Profil pro
    solution provider
    Inscrit en
    Mars 2006
    Messages
    4 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : solution provider

    Informations forums :
    Inscription : Mars 2006
    Messages : 4 087
    Points : 7 168
    Points
    7 168
    Billets dans le blog
    20
    Par défaut
    Bonjour,
    Il y a plusieurs méthodes en voici une:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    Sub new_mail_with_embedded()
    '---------------------------------------------------------------------------------------
    ' Procedure : new_mail_with_embedded
    ' Author    : Oliv
    ' Date      : 11/03/2019
    ' Purpose   :
    '---------------------------------------------------------------------------------------
    '
    Dim objmail As MailItem
    Dim pj As Attachment, pj2 As Attachment
        Set objmail = Application.CreateItem(olMailItem)
        Set pj = objmail.Attachments.add("C:\Users\Public\Pictures\genou.png")
        Set pj2 = objmail.Attachments.add("C:\Users\Public\Pictures\pouce.jpg")
     
        objmail.BodyFormat = olFormatHTML
        objmail.HTMLBody = "<html><p>This is a picture.</p>" & _
                "<img src='cid:" & pj.FileName & "'><p>This is one more.</p><img src='cid:" & pj2.FileName & "' >"
        objmail.Display
        'objMail.Close olSave
        Set objmail = Nothing
    End Sub

  3. #3
    Membre régulier
    Homme Profil pro
    Développeur
    Inscrit en
    Octobre 2014
    Messages
    322
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Saône et Loire (Bourgogne)

    Informations professionnelles :
    Activité : Développeur
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2014
    Messages : 322
    Points : 124
    Points
    124
    Par défaut
    Je n'ai pas de tester avant demain matin, mais j'ai une question
    Le pj.filename contient le nom du fichier avec l'extension? Ou un code specifique?

    Car j'ai deja essaye avec cid:nom.png et dans le mail recu, je n'ai pas l'image

  4. #4
    Membre régulier
    Homme Profil pro
    Développeur
    Inscrit en
    Octobre 2014
    Messages
    322
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Saône et Loire (Bourgogne)

    Informations professionnelles :
    Activité : Développeur
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2014
    Messages : 322
    Points : 124
    Points
    124
    Par défaut
    Bonjour,

    désolé pour le retard, je travaillait sur autre chose.

    Le code que tu m'as donné ne fonctionne pas.

    Quand je vais dans l'inspecteur pour voir le code généré du mail.

    J'ai une simple balise <img> vide.

    Je n'ai meme pas la src et je ne comprends pas pourquoi.

  5. #5
    Expert éminent
    Avatar de Oliv-
    Homme Profil pro
    solution provider
    Inscrit en
    Mars 2006
    Messages
    4 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : solution provider

    Informations forums :
    Inscription : Mars 2006
    Messages : 4 087
    Points : 7 168
    Points
    7 168
    Billets dans le blog
    20
    Par défaut
    Salut,
    Utilises-tu bien le code tel quel ? (avec des chemins d'images existantes bien sûr)

    Chez moi cela fonctionne très bien, quelle est ta version de OL ?

  6. #6
    Membre régulier
    Homme Profil pro
    Développeur
    Inscrit en
    Octobre 2014
    Messages
    322
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Saône et Loire (Bourgogne)

    Informations professionnelles :
    Activité : Développeur
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2014
    Messages : 322
    Points : 124
    Points
    124
    Par défaut
    J'utilise office 365, donc je suppose que j'ai office 2019.

    Voila le code que j'ai, que j'ai du un peu modifier.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     
    Sub Main()
            Dim objmail As MailItem
            Dim pj As Attachment
     
            Dim outlookApp As Application
            outlookApp = CreateObject("Outlook.Application")
     
            objmail = outlookApp.CreateItem(olMailItem)
            pj = objmail.Attachments.Add("S:\macarte.jpg")
     
            objmail.To = "************"
            objmail.CC = "********"
     
            objmail.BodyFormat = olFormatHTML
            objmail.HTMLBody = "<html><p>This is a picture.</p>" &
                        "<img src='cid:" & pj.FileName & "'/>"
            objmail.Send()
            'objMail.Close olSave
            objmail = Nothing
        End Sub

  7. #7
    Expert éminent
    Avatar de Oliv-
    Homme Profil pro
    solution provider
    Inscrit en
    Mars 2006
    Messages
    4 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : solution provider

    Informations forums :
    Inscription : Mars 2006
    Messages : 4 087
    Points : 7 168
    Points
    7 168
    Billets dans le blog
    20
    Par défaut
    Le objmail.Display est capital !

  8. #8
    Membre régulier
    Homme Profil pro
    Développeur
    Inscrit en
    Octobre 2014
    Messages
    322
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Saône et Loire (Bourgogne)

    Informations professionnelles :
    Activité : Développeur
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2014
    Messages : 322
    Points : 124
    Points
    124
    Par défaut
    En effet c'est mieux, mais par contre, l'image n'est pas trouvé.

    Nom : 2019-03-13_11h19_27.png
Affichages : 627
Taille : 1,9 Ko

    Et pourtant j'ai bien rajouter le display.

    Je remet le code au cas ou.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     
    Dim objmail As MailItem
            Dim pj As Attachment
     
            Dim outlookApp As Application
            outlookApp = CreateObject("Outlook.Application")
     
            objmail = outlookApp.CreateItem(olMailItem)
            pj = objmail.Attachments.Add("S:\macarte.jpg")
     
            objmail.To = "**************"
            objmail.CC = "**********"
            objmail.Subject = "test"
     
            objmail.BodyFormat = olFormatHTML
            objmail.HTMLBody = "<html><p>This is a picture.</p>" &
                        "<img src='cid:" & pj.FileName & "'/>"
            objmail.Display()
            objmail.Send()
            'objMail.Close olSave
            objmail = Nothing
    Images attachées Images attachées  

  9. #9
    Expert éminent
    Avatar de Oliv-
    Homme Profil pro
    solution provider
    Inscrit en
    Mars 2006
    Messages
    4 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : solution provider

    Informations forums :
    Inscription : Mars 2006
    Messages : 4 087
    Points : 7 168
    Points
    7 168
    Billets dans le blog
    20
    Par défaut
    Ton code ne peut pas marcher tel que publié !

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    Sub TEST()
     
    Dim objmail As MailItem
            Dim pj As Attachment
     
            Dim outlookApp As Application
            Set outlookApp = CreateObject("Outlook.Application")
     
            Set objmail = outlookApp.CreateItem(olMailItem)
            Set pj = objmail.Attachments.add("S:\macarte.jpg")
     
    '        objmail.To = "**************"
    '        objmail.CC = "**********"
            objmail.Subject = "test"
     
            objmail.BodyFormat = olFormatHTML
            objmail.HTMLBody = "<html><p>This is a picture.</p>" & _
                        "<img src='cid:" & pj.FileName & "'/>"
            objmail.Display
            'objmail.Send
            'objMail.Close olSave
            'objmail = Nothing
    End Sub

  10. #10
    Membre régulier
    Homme Profil pro
    Développeur
    Inscrit en
    Octobre 2014
    Messages
    322
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Saône et Loire (Bourgogne)

    Informations professionnelles :
    Activité : Développeur
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2014
    Messages : 322
    Points : 124
    Points
    124
    Par défaut
    Je comprends pas
    Pourquoi enlever le send et les destinataires?

    Cette technique fonctionne seulement si le mail est envoye par l'utilisateur?

    Car j'ai besoin de l'envoyer automatiquement.

    EDIT : Même avec ton code, et l'envoie par l'utilisateur, l'image ne s'affiche toujours pas.

    Qu'est ce que je ne comprends pas la dedans ?

  11. #11
    Expert éminent
    Avatar de Oliv-
    Homme Profil pro
    solution provider
    Inscrit en
    Mars 2006
    Messages
    4 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : solution provider

    Informations forums :
    Inscription : Mars 2006
    Messages : 4 087
    Points : 7 168
    Points
    7 168
    Billets dans le blog
    20
    Par défaut
    Le send je l'ai juste commenté pour test en fait c'est le

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
          outlookApp = CreateObject("Outlook.Application")
     
            objmail = outlookApp.CreateItem(olMailItem)
            pj = objmail.Attachments.Add("S:\macarte.jpg")
    remplacé en

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
      Set outlookApp = CreateObject("Outlook.Application")
     
            Set objmail = outlookApp.CreateItem(olMailItem)
            Set pj = objmail.Attachments.add("S:\macarte.jpg")

    Voici une autre méthode

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    Sub NewMessage_With_Embedded_Dmitry()
        Dim olApp As Outlook.Application
        If Application.Name = "Outlook" Then
            Set olApp = Application
        Else
            Set olApp = CreateObject("outlook.application")
        End If
        Set NS = olApp.Session
     
        Set objOutlookMsg = olApp.CreateItem(olMailItem)
     
        Set objOutlookRecip = objOutlookMsg.Recipients.add("test@toto.com")
        objOutlookRecip.Type = olTo
        objOutlookMsg.Subject = "test"
     
        ' add graphic as attachment to Outlook message
        Set colAttach = objOutlookMsg.Attachments
        Set l_Attach = colAttach.add("C:\Users\Public\Pictures\pouce.jpg")
        l_Attach.propertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x370E001F", "image/jpeg"
        'Change From 0x370eE001E
        l_Attach.propertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x3712001F", "myident"
        'Changed from 0x3712001E
        objOutlookMsg.propertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B", True
        'Set body format to HTML
        objOutlookMsg.BodyFormat = olFormatHTML
        objOutlookMsg.HTMLBody = "<html><p>This is a picture.</p>" & _
                "<img src='cid:myident' >"
     
        objOutlookMsg.Save
        objOutlookMsg.Display
        'objOutlookMsg.Send
    End Sub

  12. #12
    Membre régulier
    Homme Profil pro
    Développeur
    Inscrit en
    Octobre 2014
    Messages
    322
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Saône et Loire (Bourgogne)

    Informations professionnelles :
    Activité : Développeur
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2014
    Messages : 322
    Points : 124
    Points
    124
    Par défaut
    Ah okay.

    Sinon tu utilises quel langage ? du VBA ? Car VS me dit que Set/Let ne sont plus prise en charge. J'ai VS 2017.

    C'est surement la, la différence.

  13. #13
    Expert éminent
    Avatar de Oliv-
    Homme Profil pro
    solution provider
    Inscrit en
    Mars 2006
    Messages
    4 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : solution provider

    Informations forums :
    Inscription : Mars 2006
    Messages : 4 087
    Points : 7 168
    Points
    7 168
    Billets dans le blog
    20
    Par défaut
    Ah oui je code en vba

  14. #14
    Membre régulier
    Homme Profil pro
    Développeur
    Inscrit en
    Octobre 2014
    Messages
    322
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Saône et Loire (Bourgogne)

    Informations professionnelles :
    Activité : Développeur
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2014
    Messages : 322
    Points : 124
    Points
    124
    Par défaut
    Pour le coup, je ne pense que cela change grand chose pour le CID.

    Je vais continuer a chercher, sinon tu ne connais pas une autre solution pour le faire.

  15. #15
    Expert éminent
    Avatar de Oliv-
    Homme Profil pro
    solution provider
    Inscrit en
    Mars 2006
    Messages
    4 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : solution provider

    Informations forums :
    Inscription : Mars 2006
    Messages : 4 087
    Points : 7 168
    Points
    7 168
    Billets dans le blog
    20
    Par défaut
    As tu vu la dernière partie de ma réponse ?
    https://www.developpez.net/forums/d1.../#post10821604


    il y a encore une autre solution, c'est d'utiliser .WORDEDITOR et d'insérer l'image comme pour une macro WORD.

  16. #16
    Membre régulier
    Homme Profil pro
    Développeur
    Inscrit en
    Octobre 2014
    Messages
    322
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Saône et Loire (Bourgogne)

    Informations professionnelles :
    Activité : Développeur
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2014
    Messages : 322
    Points : 124
    Points
    124
    Par défaut
    Non je l'avais pas vu, mais j'ai trouvé cette solution de moi même x)

    En tout cas merci beaucoup.

    Voici le code qui fonctionne ne VB.NET avec l'objet COM.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
     
            Dim objmail As MailItem
            Dim pj As Attachment
     
            Dim outlookApp As Application
            outlookApp = CreateObject("Outlook.Application")
     
            objmail = outlookApp.CreateItem(olMailItem)
            pj = objmail.Attachments.Add("**********\cgu.png")
     
            Dim contentID As String = "cgu"
     
            pj.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", contentID)
     
            objmail.To = "****************"
            objmail.CC = "*************"
            objmail.Subject = "test"
     
            objmail.BodyFormat = olFormatHTML
            objmail.HTMLBody = "<html><p>This is a picture.</p>" &
                        "<img src='cid:" & contentID & "'/>"
            objmail.Display()
            objmail.Send()
            'objMail.Close olSave
            'objmail = Nothing

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 2
    Dernier message: 01/08/2017, 23h01
  2. [OL-2002] Email OLE: image en pièce jointe visible ET dans le corps de message HTML(cid)
    Par Rodrigue.L dans le forum VBA Outlook
    Réponses: 1
    Dernier message: 22/05/2015, 16h12
  3. Ajouter des images dans le corps d'un email
    Par developeuz93 dans le forum ASP
    Réponses: 17
    Dernier message: 23/12/2008, 17h02
  4. Réponses: 3
    Dernier message: 06/12/2008, 19h34
  5. [VBS/Outlook]Envoyer image dans le corp du message
    Par daniel_gre dans le forum VBScript
    Réponses: 5
    Dernier message: 21/05/2008, 09h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo