Map可以将java类ByteArrayoutputstream作为值吗? Can List可以保存javax.mail.MimeMessage对象吗?

问题描述 投票:0回答:1

我正在使用JavaMail API在我的java Web应用程序中发送电子邮件。我的用例是使用特定于用户的内容向不同的收件人发送多封电子邮件。内容包括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保存在内存中会发生什么?

java dictionary arraylist memory-management javamail
1个回答
0
投票

是的,HashMapArrayList可以存储Object的任何子类型,包括ByteArrayOutputStreamMimeMessage作为值。

也许回答你的一部分困惑:这些数据结构不会将实际对象本身存储在“内部”;相反,HashMapArrayList存储对必须已经存在于内存中的对象的引用。

因此,只要你有足够的内存来创建ByteArrayOutputStream,存储对它的引用的额外内存不太可能成为问题。

© www.soinside.com 2019 - 2024. All rights reserved.