javax.mail with HTML message + attachment not sending HTML message but sending attachment
Asked Answered
A

1

6

I am using this code to send text/html content along with attachment but it's only sending attachment.

    package com.maling.sendmail;

    import java.util.Properties;
    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.activation.FileDataSource;
    import javax.mail.Message;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;

    public class sendmail {

        boolean flag = false;
        String from = "[email protected]";
        String password = "Pa**w*rd";
        String filename = "D:\\myfile.pdf";

        public boolean sendMail(String email_id_of_recipients) {
            System.out.println("into maling utility......");
            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 session = Session.getInstance(props,
                    new javax.mail.Authenticator() {
                        @Override
                        protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                            return new javax.mail.PasswordAuthentication(from, password);
                        }
                    });
            try {
                String text = "<h1>Hello My html formeted message</h1>";
                String to = email_id_of_recipients;
                MimeMessage message = new MimeMessage(session);
                message.setFrom(new InternetAddress(from));
                message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
                message.setSubject("Myresume");
                message.setText(text, "utf-8", "html");
                message.setContent(text, "text/html; charset=utf-8");
                DataSource source = new FileDataSource(filename);
                message.setDataHandler(new DataHandler(source));
                message.setFileName(filename);
                Transport.send(message);

                flag = true;
                System.out.println("end of utility");
            } catch (Exception ex) {
                System.out.println(ex);
            }

            return flag;
        }

    }

How should i make it right?

Autochthon answered 26/7, 2015 at 9:32 Comment(0)
O
7

You need a multipart message to send both a text and an attachment. This code should work:

String text = "<h1>Hello My HTML formatted message</h1>";
String to = email_id_of_recipients;
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject("My resume");
// message.setText(text, "utf-8", "html");

MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(text, "text/html; charset=utf-8");

DataSource source = new FileDataSource(filename);
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
Multipart multipart = new MimeMultipart("mixed");
multipart.addBodyPart(textPart);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);

Transport.send(message);
Olivares answered 26/7, 2015 at 10:16 Comment(2)
Sorry to say sir but, i followed your foot printed and replace my code by the code given by you and add additional packages those were required but the result is still same as it was.Autochthon
You're right - you'll need another MimeBodyPart. I updated my answer, please check again.Olivares

© 2022 - 2024 — McMap. All rights reserved.