1

我正在实施一个邮件发件人,PDF每月一天有近 1'6000.000 封邮件(带有图片和

我需要为每个产品填写一个 Message Scratch ......以便不阅读(每封电子邮件)每个产品的其他内容。

我正在尝试实现克隆javax.mail.Messagejavax.mail.Multipart更快。

将内容添加到多部分

  public static void addContent(final Multipart multipart, String contenidoCorreo) throws Exception {
    MimeBodyPart mimeBodyPart = new PreencodedMimeBodyPart("8bit");
    mimeBodyPart.setText(contenidoCorreo, "iso-8859-1", "html");
    multipart.addBodyPart(mimeBodyPart, 0);
  }

每字节添加图像

  public static void addImageToMultipart(final Multipart multipart, byte[] contenidoImagen, String nombreImagen) throws Exception {
    MimeBodyPart imagenMimeBodyPart = new MimeBodyPart();
    try {
      ByteArrayDataSource byteArrayDataSource = new ByteArrayDataSource(contenidoImagen, "image/*");
      imagenMimeBodyPart.setDataHandler(new DataHandler(byteArrayDataSource));
      imagenMimeBodyPart.setFileName(nombreImagen);
      imagenMimeBodyPart.setContentID("<" + nombreImagen + ">");
      imagenMimeBodyPart.setDisposition(MimeBodyPart.INLINE);
      multipart.addBodyPart(imagenMimeBodyPart);
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException(e.getMessage());
    }
  }

每个文件添加PDF

  public static void addPDF(final Multipart multipart, String ruta, String nombre) throws Exception {
    Path path = Paths.get(ruta, nombre);
    if (path.toFile().exists()) {
      MimeBodyPart preencodedMimeBodyPart = new PreencodedMimeBodyPart("base64");
      preencodedMimeBodyPart.attachFile(path.toFile());
      preencodedMimeBodyPart.setFileName(nombre);
      preencodedMimeBodyPart.setHeader("Content-Type", "application/pdf");
      preencodedMimeBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
      multipart.addBodyPart(preencodedMimeBodyPart);

      MimeBodyPart pdfMimeBodyPart = new MimeBodyPart();
  }

我的克隆信息

  public static Message cloneMessage(Message source) {
    //Multiple and Separated Exceptions because maybe not all properties are defined in some time.
    Message target = new MimeMessage(source.getSession());
    try {
      if (source.getFrom() != null && source.getFrom().length > 0) {
        Address address = (source.getFrom()[0]);
        target.setFrom(new InternetAddress(((InternetAddress) address).getAddress(), ((InternetAddress) address).getPersonal()));
      }

    } catch (Exception ex) {
      //Handle Exception
    }
    try {
      target.setSentDate((Date) (source.getSentDate().clone()));
    } catch (MessagingException ex) {
      //Handle Exception
    }

    try {
      target.setRecipients(Message.RecipientType.TO, target.getRecipients(Message.RecipientType.TO).clone());
    } catch (MessagingException ex) {
      //Handle Exception
    }

    try {
      Enumeration numerationHeaders = source.getAllHeaders();
      while (numerationHeaders.hasMoreElements()) {
        Header header = (Header) numerationHeaders.nextElement();
        target.addHeader(header.getName(), header.getValue());
      }
    } catch (MessagingException ex) {
      //Handle Exception
    }

    try {
      target.setSubject(new String(source.getSubject()));
    } catch (MessagingException ex) {
      //Handle Exception
    }
    try {
      target.setContent(cloneMultipart((Multipart)(source.getContent())));
    } catch (Exception ex) {
      //Handle Exception
    }
    return target;
  }

克隆多部分

  public static Multipart cloneMultipart(Multipart source) {
    MimeMultipart target = new MimeMultipart();
    try {
      for (int i = 0; i < source.getCount(); i++) {
        MimeBodyPart mimeBodyPart = (MimeBodyPart)source.getBodyPart(i);
        mimeBodyPart //?????
      }
    } catch (MessagingException ex) {
      //Handle Exception
    }
    return target;
  }

如何克隆Multipart?

克隆消息的一些建议?

如何检测addContent添加的内容(使用 with 方法)?

4

0 回答 0