简体   繁体   English

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

[英]Can Map hold the java class ByteArrayoutputstream as values? Can List hold the javax.mail.MimeMessage objects?

I am using JavaMail API to send emails in my java web application. 我正在使用JavaMail API在我的java Web应用程序中发送电子邮件。 My use case is to send multiple emails to different recipients with the user-specific content. 我的用例是使用特定于用户的内容向不同的收件人发送多封电子邮件。 the content includes the pdf file attachment. 内容包括pdf文件附件。 I want to do code as follows, 我想做如下代码,


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
}

My question is, Does HashMap accept the ByteArrayOutputStream class instance as values? 我的问题是,HashMap是否接受ByteArrayOutputStream类实例作为值? if so how to get it from Map by using the key? 如果是这样如何使用密钥从Map获取它?

Does ArrayList accept to hold the MimeMessage Object? ArrayList是否接受保存MimeMessage对象? if so, What if the MimeMessage has large files in its Bodyparts? 如果是这样,如果MimeMessage在其Bodyparts中有大文件怎么办? what will happen when holding large files in memory as List? 将大文件作为List保存在内存中会发生什么?

Yes, HashMap and ArrayList can store any subtype of Object , including ByteArrayOutputStream or MimeMessage as values. 是的, HashMapArrayList可以存储Object任何子类型,包括ByteArrayOutputStreamMimeMessage作为值。

Perhaps answering part of your confusion: These data structures do not store the actual objects "inside" themselves; 也许回答你的一部分困惑:这些数据结构不会将实际对象本身存储在“内部”; rather HashMap and ArrayList store references to objects that must already exist in memory. 相反, HashMapArrayList存储必须已存在于内存中的对象的引用

So as long as you have enough memory to create the ByteArrayOutputStream , the extra memory to store a reference to it is unlikely to be a problem. 因此,只要您有足够的内存来创建 ByteArrayOutputStream ,存储对它的引用的额外内存就不太可能成为问题。

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

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