【问题标题】:Javamail : Not able to send an email with message body, text/html and attachment.Javamail:无法发送带有邮件正文、文本/html 和附件的电子邮件。
【发布时间】:2018-11-07 03:07:10
【问题描述】:

我能够获取邮件中的所有消息、html 和附件,但消息文本是作为文本文件接收的。

我希望邮件显示在邮件中。任何帮助将不胜感激。

     final String subject = "Automation Execution Report"; 

必须发送的消息文本

    StringBuilder msg = new StringBuilder();
    msg.append("Hi All"+"\n");
    msg.append("We have executed the Automation Suite in current 
    build."+"\n");
    msg.append("Please find the automation report in the attachment");

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to));
        //below code only requires if your want cc email address
        message.setSubject(subject);
        File dir = new File(System.getProperty("user.dir")+ 
        File.separator+"test_reports");
        File[] files = dir.listFiles();

        File lastModifiedFile = files[0];
        for (int i = 1; i < files.length; i++) {
           if (lastModifiedFile.lastModified() < files[i].lastModified()) {
               lastModifiedFile = files[i];
           }
        }
        String attach = lastModifiedFile.toString();

创建了多部分

        Multipart parent = new MimeMultipart("alternative");
        Multipart child=new MimeMultipart("mixed");

        message.setSubject(subject);

在邮件中打印的 html 格式的自定义报告

        MimeBodyPart htmlpart = new MimeBodyPart();
        StringWriter writer = new StringWriter();
        IOUtils.copy(new FileInputStream(new File("D:\\SVN CODE\\test- 
         output\\customized-emailable-report.html")), writer);
        htmlpart.setContent(writer.toString(), "text/html");

        child.addBodyPart(htmlpart);

必须打印在邮件上的消息文本

        MimeBodyPart txtpart=new MimeBodyPart();
        txtpart.setContent(msg.toString(), "text/plain");
        child.addBodyPart(txtpart);
        //Adding the child multipart to parent multtipart     
        MimeBodyPart sub=new MimeBodyPart();
        sub.setContent(child);
        parent.addBodyPart(sub);
        //Attachment part   
        BodyPart attachPart = new MimeBodyPart();
        DataSource source = new FileDataSource(attach);
        attachPart.setDataHandler(new DataHandler(source));
        attachPart.setFileName("Test Automation report.html");
        parent.addBodyPart(attachPart);

给消息设置内容

         message.setContent(parent);

        System.out.println("Sending");

        Transport transport = session.getTransport("smtp");
        transport.connect(host,25,from, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();

        System.out.println("Done");

      } 

  [1]: https://i.stack.imgur.com/InHyZ.png
Please advise as to how the message text can be printed on the mail

【问题讨论】:

    标签: java email jakarta-mail multipart mailer


    【解决方案1】:

    您需要使用MimeMultipart 并向其添加MimeBodyPart 元素。

    MimeMultipart multipart = new MimeMultipart();
    
    String htmlContent = "<p>Hello World!</p>"
    // add HTML content here
    final MimeBodyPart  messageBodyPart = new MimeBodyPart();
    // HTML Content
    messageBodyPart.setContent(htmlContent, "text/html;charset=UTF-8");
    
    // add it
    multipart.addBodyPart(messageBodyPart);
    
    // don't forget to add the content to your message.
    message.setContent(multipart);
    

    使用此方法,您可以将附件添加为另一个 MimeBodyPart :

         String filePath = "/tmp/dummy.txt"
         MimeBodyPart bodyPart = new MimeBodyPart();
         DataSource source = new FileDataSource(filePath);
         bodyPart.setDataHandler(new DataHandler(source));
         bodyPart.setFileName(fileName);
         multipart.addBodyPart(bodyPart);
    

    编辑 没有看到您已经在使用多部分。 您的问题仅在于您设置的内容类型。您必须使用"text/html" 而不是"text/plain"

    【讨论】:

    • 我也试过了,文本来自一个 html 文件。我需要在邮件中打印字符串。
    • 您是否尝试过设置在附件前先附加消息?
    猜你喜欢
    • 2018-07-24
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 2015-10-27
    • 1970-01-01
    • 2013-06-13
    • 2017-01-07
    相关资源
    最近更新 更多