Я использую JavaMail API для отправки электронных писем в своем веб-приложении на Java.Мой вариант использования - отправка нескольких писем разным получателям с пользовательским контентом.Содержание включает в себя PDF-файл вложения.Я хочу сделать код следующим образом:
Map<Long, ByteArrayOutputStream> pdffiles = new HashMap<Long, ByteArrayOutputStream>();
Map<Long, String> contentMap = new HashMap<Long,String>();
start of loop
{
String userId = //uniqId;
ByteArrayOutputStream outFile= new ByteArrayOutputStream();
outFile = // statement to invoke a method to create the customer
specific pdf file
String fileName = "Invoice_<company_name>"+".pdf";
MimeBodyPart pdfBodyPart = new MimeBodyPart();
pdffiles.put(userId, outFile);
String content = //Some user specific content loaded here.
contentMap.put(userId, content);
}
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
List<MimeMessage> msgList= new ArrayList<MimeMessage>();
for(Long userid : contentMap.keySet()){
String content = contentMap.get();
String contentType ="text/html;charset=UTF-8";
MimeMessage msg = new MimeMessage(session);
ByteArrayOutputStream outFile = // get the pdf file from map using the userid as key
byte[] bytes = outFile.toByteArray();
DataSource dataSource = new ByteArrayDataSource(bytes,
"application/pdf");
pdfBodyPart.setDataHandler(new DataHandler(dataSource));
Multipart multipart = new MimeMultipart();
try {
//adding the passed multipart content to the mail that to send
as an inline attachment.
messageBodyPart.setContent(content, contentType);
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(pdfBodyPart);
msg.setContent(multipart);
// have to add this 'msg' Object in List.
InternetAddress[] addressTo = null;
try {
addressTo = InternetAddress.parse(eo.getTo());
msg.setRecipients(Message.RecipientType.TO, addressTo);
} catch (AddressException e) {
// excpetion handled here
} catch (MessagingException e) {
// excpetion handled here
}
}catch (MessagingException e) {
} catch (Exception e) {//expetion handled here
}
}
Transport transport = null;
try {
transport = session.getTransport("smtp");
} catch (NoSuchProviderException e) {
//exception handled here
}
try {
transport.connect();
for(MimeMessage msg : msgList){
transport.sendMessage(msg, msg.getAllRecipients());
}
transport.close();
}catch (Exception ex) {
//exception handled here
}
Мой вопрос: принимает ли HashMap экземпляр класса ByteArrayOutputStream в качестве значений?если да, то как получить его из Map с помощью ключа?
Принимает ли ArrayList объект MimeMessage для хранения?если так, то что, если MimeMessage имеет большие файлы в своих Bodyparts?что будет происходить при хранении больших файлов в памяти как List?