Send image in Email Body using Java

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 09:41:46

You need to declare your images like this :

<img src="cid:unique-name-or-id" />

Load images as MimeBodyPart and match the unique-name-or-id with the FileName of the MimeBodyPart.

Lucky

see the below code may be use full

class SimpleMail2 {
    public static void main(String[] args) throws Exception{

        System.out.println("Sending mail...");
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session mailSession = Session.getDefaultInstance(props,
        new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("sender@gmail.com","password");
            }
        });
        Message message = new MimeMessage(mailSession);
        message.setFrom(new InternetAddress("sender@gmail.com"));
        message.setSubject("HTML  mail with images");
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("receiver@gmail.com"));
        message.setText("Dear Mail Crawler," + "\n\n No spam to my email, please!");

        MimeMultipart multipart = new MimeMultipart("related");
        BodyPart messageBodyPart = new MimeBodyPart();
        String htmlText = "<H1>Raghava chary</H1>" + "<img src=\"cid:image\">";
        messageBodyPart.setContent(htmlText, "text/html");
        multipart.addBodyPart(messageBodyPart);
        try {
            messageBodyPart = new MimeBodyPart();
            InputStream imageStream = SimpleMail2.class.getClass().getResourceAsStream("/ab/log.gif");
            DataSource fds = new ByteArrayDataSource(IOUtils.toByteArray(imageStream), "image/gif");
            messageBodyPart.setDataHandler(new DataHandler(fds));
            messageBodyPart.setHeader("Content-ID","<image>");
            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);
            Transport.send(message);
            System.out.println("Done");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

and add org.apache.commons.io.jar.zip and axiom-api-1.2.6.jar and add mail.jar and activation.jar

Daniel Voina

Create a multipart body with content-disposition inline and encode in base64 your image.

Check this SO for some details (in Python) Sending Multipart html emails which contain embedded images

First, see this JavaMail FAQ entry of common mistakes.

Then, see this JavaMail FAQ entry with sample code for connecting to Gmail.

Note that there is no "mail.smtp.password" property. Since you're not supplying a password, authentication is failing.

Another common mistake (bit me today): the Content-ID header for the image must be in <angle brackets>. Failure to do so will break some mail programs (gmail, OS X 10.10) but not others (Outlook, iOS <= 8.1).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!