Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java邮件:附加html内容和附件_Java_Email - Fatal编程技术网

Java邮件:附加html内容和附件

Java邮件:附加html内容和附件,java,email,Java,Email,我正在尝试发送一封带有附件的邮件,同时也包含html内容。 我知道如何分别发送html内容和附件,但是否可以同时发送html和附件以及附件 以下是我尝试过的: public static void sendAttachment(final String to, final String cc, final String subject, final String text, final byte[] attachment, final String fileName) {

我正在尝试发送一封带有附件的邮件,同时也包含html内容。 我知道如何分别发送html内容和附件,但是否可以同时发送html和附件以及附件

以下是我尝试过的:

public static void sendAttachment(final String to, final String cc, final String subject, final String text,
            final byte[] attachment, final String fileName) {

    if (null == to) {
        return;
    }

    try {
        Properties props = getProperties();
        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(FROM_EMPRIS));
        message.setRecipients(RecipientType.TO, InternetAddress.parse(to));
        if(null != cc)
            message.addRecipient(RecipientType.CC, new InternetAddress(cc));
        if (null != subject)
            message.setSubject(subject);

        BodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.setText(text);

        BodyPart messageBodyPart2 = new MimeBodyPart();
        DataSource ds = new ByteArrayDataSource(attachment, "application/x-any");
        messageBodyPart2.setDataHandler(new DataHandler(ds));
        messageBodyPart2.setFileName(fileName);
        Multipart multiPart = new MimeMultipart();
        multiPart.addBodyPart(messageBodyPart1);
        multiPart.addBodyPart(messageBodyPart2);

        message.setContent(multiPart);

        Transport.send(message);

    } catch (AddressException e) {
                    e.printStackTrace();
    } catch (MessagingException e) {
                    e.printStackTrace();
    } catch (Exception e) {
                    e.printStackTrace();
    }
}
这一个以明文形式发送附件和setText(文本)。是否可以将其更改为html内容而不是纯文本?
非常感谢您的帮助。

只需创建另一个带有html内容的
mimeBodyPart
setContent(htmlContent,“text/html”)
。将其添加到纯文本主体部分之后

只需创建另一个带有html内容的
mimeBodyPart
,以及
setContent(htmlContent,“text/html”)
。将其添加到纯文本主体部分之后

使用下面的方法并在正文中设置html内容


message.setContent(正文,“text/html”)

使用下面的方法并在正文中设置html内容


message.setContent(正文,“text/html”)

为messageBodyPart1设置数据处理程序

BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText(text);
messageBodyPart1.setDataHandler(new DataHandler(textHtml, "text/html;charset=utf-8"));// this is to handle html

设置messageBodyPart1的DataHandler

BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText(text);
messageBodyPart1.setDataHandler(new DataHandler(textHtml, "text/html;charset=utf-8"));// this is to handle html

感谢您的回复,它成功了,非常感谢。:)感谢您的回复,它成功了,非常感谢。:)