【问题标题】:javax.mail with HTML message + attachment not sending HTML message but sending attachment带有 HTML 消息 + 附件的 javax.mail 不发送 HTML 消息但发送附件
【发布时间】:2015-10-16 14:24:08
【问题描述】:

我正在使用此代码发送text/html 内容和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 = "myemailid@gmail.com";
        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;
        }

    }

我应该怎么做呢?

【问题讨论】:

    标签: java jakarta-mail mailing


    【解决方案1】:

    您需要multipart message 来发送文本和附件。这段代码应该可以工作:

    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);
    

    【讨论】:

    • 对不起,先生,但是,我按照您的脚印,用您提供的代码替换了我的代码,并添加了所需的额外包,但结果仍然是一样的。
    • 你是对的 - 你需要另一个 MimeBodyPart。我更新了我的答案,请再次检查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多