我正在尝试使用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();
有没有办法可以在不保存附加电子邮件的情况下执行相同的操作。我搜索过但发现要附加的文件可以存储在内存中,但无法将邮件保存在内存中。
请建议。
由于
答案 0 :(得分:0)
您可以将附加的电子邮件写入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);
...
<强>更新强>
哦,我明白了...... setDataHandler()
接受DataHandler
。并DataHandler
接受DataSource
!
但是让我们看一下DataSource
's Javadoc。已经提供了两种实现:FileDataSource
和URLDataSource
。实现从字节数组中获取数据的新数据源并不困难。只需要实施几种方法。同样,下面的代码大大简化了。但它带来了数据流是通用概念的想法。一旦您实现了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));
...