繁体   English   中英

如何将邮件作为附件发送到java中的另一个邮件而不将其保存到磁盘

[英]How to send a mail as a attachment in another mail in java without saving it to Disk

我正在尝试使用javax api在另一封邮件中将邮件作为附件发送。 截至目前,我首先将邮件保存在磁盘上,然后使用以下代码将其附加到另一封电子邮件: -

        MimeMessage generateMailMessage = new MimeMessage(getMailSession);
        generateMailMessage.setFrom(new InternetAddress("abc@a.com"));
        String mailSubject = properties.getProperty("mail.subject");
        generateMailMessage
                .setSubject(mailSubject);
        generateMailMessage.setContent(emailBody, "text/html");
        generateMailMessage.addRecipient(Message.RecipientType.TO,
                new InternetAddress(properties.getProperty("message.recipienttype.to")));
        generateMailMessage.addRecipient(Message.RecipientType.CC,
                new InternetAddress(recipientEmail));


        File  file = new File(properties.getProperty("mail.draft.folder")+"mail.eml");
        FileOutputStream fos = new FileOutputStream(chatFile);
        generateMailMessage.writeTo(fos);

        Session getMailSession1 = Session.getDefaultInstance(mailServerProperties, null);

        MimeMessage generateMailMessage1 = new MimeMessage(getMailSession1);
        generateMailMessage1
                .setSubject("Attachment");

        generateMailMessage1.addRecipient(Message.RecipientType.TO,
                new InternetAddress("manish@xyz.com"));


        Multipart multipart = new MimeMultipart();
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setDescription("hahdsa");
        DataSource source = new FileDataSource(file);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName("mail.eml");
        multipart.addBodyPart(messageBodyPart);
        generateMailMessage1.setContent(multipart);


        transport = getMailSession1.getTransport("smtp");
        if(!transport.isConnected())
            transport.connect(properties.getProperty("mail.host"),
                Integer.parseInt((String) properties.get("mail.smtp.port")), "abc@xyz.com",
                (String) properties.get("mail.password"));



        transport.sendMessage(generateMailMessage1, generateMailMessage1.getAllRecipients());
        transport.close();

有没有办法,我可以做同样的事情,而不保存附加的电子邮件 我已经搜索过但发现要附加的文件可以存储在内存中,但无法将邮件保存在内存中。

请建议。

谢谢

您可以将附加的电子邮件写入FileOutputStream但写入ByteArrayOutputStream ,因此电子邮件将保留在RAM中。 然后,您可以将流转换为字节数组并发送它。 像这样的东西(它不是下面的纯Java代码,没有异常处理,关闭流等,它只是一个说明这个想法的伪代码):

...
ByteArrayOutputStream emailOutputStream = new ByteArrayOutputStream();
generateMailMessage.writeTo(emailOutputStream);
...
MimeBodyPart messageBodyPart = new MimeBodyPart();
...
byte[] email = emailOutputSteam.toByteArray();
messageBodyPart.setDataHandler(email);
...

唯一的问题是如何将电子邮件数据附加到邮件正文。 我不熟悉您使用的电子邮件API。 也许可以指定一个字节数组作为MimeBodyPart.setDataHandler()方法的参数,可能不是。 但是很可能MimeBodyPart.setDataHandler()方法可以接受一个蒸汽(BTW大多数Java库不仅可以从文件中读取,还可以从输入流中读取)。 在这种情况下, ByteArrayInputStream会有所帮助,如下图所示:

...
ByteArrayOutputStream emailOutputStream = new ByteArrayOutputStream();
generateMailMessage.writeTo(emailOutputStream);
...
MimeBodyPart messageBodyPart = new MimeBodyPart();
...
ByteArrayInputStream emailInputStream = ByteArrayInputStream(emailOutputSteam.toByteArray());
messageBodyPart.setDataHandler(emailInputStream);
...

UPDATE

哦,我明白了... setDataHandler()接受DataHandler DataHandler接受DataSource

但是让我们来看看DataSource的Javadoc 已经提供了两种实现: FileDataSourceURLDataSource 实现从字节数组中获取数据的新数据源并不困难。 只需要实施几种方法。 同样,下面的代码大大简化了。 但它带来了数据流是通用概念的想法。 一旦你实现了DataSource接口, DataHandler类甚至不会注意到实际上是为RAM(或数据库,或其他)获取数据:

public class ByteArrayInputStreamDataSource {

    private ByteArrayInputStream stream;

    public ByteArrayInputStreamDataSource(byte[] data) {
        this.stream = new ByteArrayInputStream(data);
    }

    public String getContentType() {
        return "Your content MIME type, perhaps, it will be text/html ...";
    }

    public InputStream getInputStream() {
        return stream;
    }       

    public String getName() {
        return "Some meaningful name";
    }

    public OutputStream getOutputStream() {
        throw new UnsupportedOperationException("Modification of the datasource is not allowed.");
    }

    public void close() {
        // This method is not required by DataSource interface. 
        // But once we deal with stream generally it will be better 
        // to put here logic that closes the stream gracefully.
        // As for ByteArrayInputStream there is no need to close it 
        // according to Javadoc:
        // https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html#close()
    }
}

所以我们的自定义数据源可以像这样使用:

...
ByteArrayOutputStream emailOutputStream = new ByteArrayOutputStream();
generateMailMessage.writeTo(emailOutputStream);
...
MimeBodyPart messageBodyPart = new MimeBodyPart();
...
DataSource byteArraySource = new ByteArrayInputStream(emailOutputStream.toByteArray());
messageBodyPart.setDataHandler(new DataHandler(byteArraySource));
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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