Intégration d'image dans Java mail-CID ne fonctionne pas


Je travaille sur une application pour envoyer des mails à des listes etc. Il est écrit en Java en utilisant le javax.le courrier de l'api. Le problème ici est que je ne peux pas intégrer d'images à l'aide d'un CID.

C'est la source du courrier qui est envoyé:

Delivered-To: [email protected]
Received: by 10.43.50.4 with SMTP id vc4csp85536icb;
        Tue, 17 Apr 2012 05:08:00 -0700 (PDT)
Received: by 10.204.141.25 with SMTP id k25mr4406030bku.72.1334664479298;
        Tue, 17 Apr 2012 05:07:59 -0700 (PDT)
Return-Path: <[email protected]>
Received: from mail-bk0-f43.google.com (mail-bk0-f43.google.com [209.85.214.43])
        by mx.google.com with ESMTPS id ad16si7984294bkc.150.2012.04.17.05.07.58
        (version=TLSv1/SSLv3 cipher=OTHER);
        Tue, 17 Apr 2012 05:07:59 -0700 (PDT)
Received-SPF: pass (google.com: domain of [email protected] designates 209.85.214.43 as permitted sender) client-ip=209.85.214.43;
Authentication-Results: mx.google.com; spf=pass (google.com: domain of - designates 209.85.214.43 as permitted sender) smtp.mail=-; dkim=pass [email protected]
Received: by mail-bk0-f43.google.com with SMTP id j5so5978375bkw.2
        for <[email protected]>; Tue, 17 Apr 2012 05:07:58 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=date:from:to:message-id:subject:mime-version:content-type;
        bh=Xj44O9fBIzirvcquOGxJbYLtbqgBc2Ags7prNUuAtSQ=;
        b=WWTCdrFgs3RCT/g2qXHR0fLCTc73TmNMA15sff0oIDksB6Nn3IwTYAqTVmoGiNrkW5
         08WDpTRADEKAOvjQ5FC9/uBCh1RXWTjxtawfjHc7vfUpqKbXOCj8Ab6GWXQMmX/+WB6T
         KVYLhk3/+GddIJI1XsAX9zprSYVcP6MMJ5/U+idIDlC7xQGGNuvzvpAnlnlGuWzgKc6j
         qPoFSAAaio6zICY9uSaI0deBIYTEQ2hIuBDJG8oaRvGVvpjhzJaBK+ab+rPJEboHPg8S
         3WSCG/Pp212VOw/YXOLUQV0jmMbuqAbsGdeER+Okbwe11sWi+zvPz+jhplB0NB2wq0Fn
         mWzA==
Received: by 10.204.130.13 with SMTP id q13mr4084640bks.128.1334664478358;
        Tue, 17 Apr 2012 05:07:58 -0700 (PDT)
Return-Path: <[email protected]>
Received: from Akoya ([xx])
        by mx.google.com with ESMTPS id z14sm37467763bky.15.2012.04.17.05.07.51
        (version=SSLv3 cipher=OTHER);
        Tue, 17 Apr 2012 05:07:54 -0700 (PDT)
Date: Tue, 17 Apr 2012 05:07:54 -0700 (PDT)
From: BP MM <[email protected]>
To: xx xx <[email protected]>
Message-ID: <404745073.01334664469900.JavaMail.xx@Akoya>
Subject: TestImage
MIME-Version: 1.0
Content-Type: multipart/mixed; 
    boundary="----=_Part_2_854532868.1334664469871"

------=_Part_2_854532868.1334664469871
Content-Type: multipart/mixed; 
    boundary="----=_Part_1_1457048287.1334664469871"

------=_Part_1_1457048287.1334664469871
Content-Type: image/png; name=logo.png
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=logo.png
Content-ID: img_cid

[base64 data]
------=_Part_1_1457048287.1334664469871--

------=_Part_2_854532868.1334664469871
Content-Type: multipart/alternative; 
    boundary="----=_Part_0_1033690582.1334664469858"

------=_Part_0_1033690582.1334664469858
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hello


------=_Part_0_1033690582.1334664469858
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<html>
<body>
<H1>Hello</H1>
<img src="cid:img_cid">
</body>
</html>

------=_Part_0_1033690582.1334664469858--

------=_Part_2_854532868.1334664469871--

J'ai essayé différents CID, en ajoutant au CID, en définissant la disposition sur inline, HTML validé, etc. Vous ne pouvez pas faire fonctionner cela, Thunderbird et Gmail affichent le courrier avec des images "mortes". J'ai lu que le client recherche le cid dans le courrier, donc je figuered il devrait récupérer l'image correctement. L'envoi de la pièce jointe fonctionne, mais pas l'intégration. Quelqu'un qui a une idée? Est-ce lié à la structure en plusieurs parties?

Le code java pour créer le message (j'ai commenté quelques lignes en essayant de le résoudre..)

    /*
     * The method to create a MimeMessage, this message can be sent with the transport method.
     */
    private Message createMessage(final Mail mail, final Member member, final MailingList list,
            final MailAccount mailAccount, final Session session)
            throws MessagingException, UnsupportedEncodingException {
        // Get attachments
        @SuppressWarnings("unchecked")
        List<File> attachments = (List<File>) mail.getProperty(Mail.ATTACHMENTS_KEY);

        // Create content parts
        String plainContent = templateEngine.replaceTemplateTags(mail, member, list, mailAccount);
        String htmlContent = templateEngine.replaceHTMLTemplateTags(mail, member, list, mailAccount);

        Multipart multiContentPart = new MimeMultipart("alternative");
//      Multipart attachmentsBodyPart = new MimeMultipart();
        Multipart rootBodyPart = new MimeMultipart();

        // Create plain text part
        if (!plainContent.equals("")) {
            BodyPart plainMessageBodyPart = new MimeBodyPart();
            plainMessageBodyPart.setContent(plainContent, "text/plain");
            multiContentPart.addBodyPart(plainMessageBodyPart);
        }

        // Create html part
        if (!htmlContent.equals("")) {
            BodyPart htmlMessageBodyPart = new MimeBodyPart();
            htmlMessageBodyPart.setContent(htmlContent, "text/html");
            multiContentPart.addBodyPart(htmlMessageBodyPart);
        }

        // Create attachments
        if (attachments != null && attachments.size() > 0) {
            for (int i = 0; i < attachments.size(); i++) {
                BodyPart attachmentBodyPart = new MimeBodyPart();
                DataSource source = new FileDataSource(attachments.get(i));
                attachmentBodyPart.setDataHandler(new DataHandler(source));
                attachmentBodyPart.setFileName(attachments.get(i).getName());
                attachmentBodyPart.setHeader("Content-ID", "<img_cid>");
                attachmentBodyPart.setDisposition(Part.INLINE);
//              attachmentsBodyPart.addBodyPart(attachmentBodyPart);
                rootBodyPart.addBodyPart(attachmentBodyPart);
            }
            // Build attachments
//          BodyPart attachmentsWrapper = new MimeBodyPart();
//          attachmentsWrapper.setContent(attachmentsBodyPart);
//          rootBodyPart.addBodyPart(attachmentsWrapper);
        }

        // Build content
        BodyPart contentWrapper = new MimeBodyPart();
        contentWrapper.setContent(multiContentPart);
        rootBodyPart.addBodyPart(contentWrapper);

        // Create message
        Message message = new MimeMessage(session);
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(
                member.getEmail(), member.getName()));
        message.setSubject((String) mail.getProperty(Mail.SUBJECT_KEY));
        message.setContent(rootBodyPart);

        // Add headers
        message.setFrom(new InternetAddress(mailAccount.getFromAddress(),
                mailAccount.getFromName()));
        if (mailAccount.getReplyTo() != "" && mailAccount.getReplyTo() != null)
            message.setReplyTo(InternetAddress.parse(mailAccount.getReplyTo()));

        return message;
    }

Ne me dérange pas que le CID soit le même pour chaque pièce jointe, plus tard ce sera le nom du fichier - je ne teste qu'avec une seule pièce jointe pour l'instant. Espérons que cette aide..

Author: krobbens, 2012-04-17

3 answers

J'ai trouvé qu'il n'y avait rien de mal avec la source du courrier. C'est la façon dont mailclients gère les CIDs. Gmail.com nécessite que le CID soit défini entre . Thunderbird n'a pas encore montré mes images.. Je pense qu'il doit être unique d'une sorte.. Je n'ai toujours pas compris pourquoi et comment le réparer. Je l'ai trouvé grâce à Incorporant des images dans un e-mail html avec java mail

 3
Author: krobbens, 2017-05-23 11:58:35

CID fonctionne correctement , le problème est lorsque vous envoyez un courrier à mutilple email id , vous devez activer MimeMessage à chaque fois que vous n'obtiendrez pas d'erreur

MimeMessage m = new MimeMessage(mailSession);

 0
Author: rampraveen, 2013-09-17 19:21:39

Cela semble incroyable, mais toutes les balises html doivent être sur la même ligne. Entre ' ' il ne peut y avoir aucun retour chariot. L'analyseur est très miteux.

 0
Author: Luis Bartolessi, 2015-05-06 02:17:47