简体   繁体   中英

Sending multipart email in java

This is my code:

            MimeMessage emailMessage = new MimeMessage(session);

            emailMessage.setFrom(new InternetAddress(from));
            emailMessage.addRecipient(Message.RecipientType.TO,  new InternetAddress(to));      
            emailMessage.setSubject("testing html and inline images emails");

            MimeMultipart multipart = new MimeMultipart("related");

            BodyPart text = new MimeBodyPart();
            String htmlText = "<H1>Hello</H1><br/> <p align=center><img src=\"cid:senny\"> </p>";
            text.setContent(htmlText, "text/html");
            multipart.addBodyPart(text);

            BodyPart image = new MimeBodyPart();
            DataSource fds = new FileDataSource("http://mobilemarketingwatch.com/wp-content/uploads/2016/01/Is-Google-Searching-for-the-Next-Big-Thing1.jpg");         
            image.setDataHandler(new DataHandler(fds));
            image.addHeader("Content-ID","<senny>");
            multipart.addBodyPart(image);

            emailMessage.setContent(multipart, "text/html; charset=utf-8");
            emailMessage.saveChanges();            

            Transport.send(emailMessage);

I am getting in the email this message:

javax.mail.internet.MimeMultipart@598f069a

instead of the real message. Any idea ??

发送Multipart对象的方法是emailMessage.setContent(multipart) ,而不是setContent(Object o, String type)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM