public Map getBareMessageDetails(String messageId) {
    Map<String, Object> messageDetails = new HashMap<String, Object>();
    try {
      Gmail service =
          new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
              .setApplicationName("Gmail Quickstart")
              .build();
      Message message = service.users().messages().get("me", messageId).setFormat("raw").execute();

      byte[] emailBytes = Base64.decodeBase64(message.getRaw());

      Properties props = new Properties();
      Session session = Session.getDefaultInstance(props, null);

      MimeMessage email = new MimeMessage(session, new ByteArrayInputStream(emailBytes));
      messageDetails.put("subject", email.getSubject());
      messageDetails.put("from", email.getSender() != null ? email.getSender().toString() : "None");
      messageDetails.put(
          "time", email.getSentDate() != null ? email.getSentDate().toString() : "None");
      messageDetails.put("snippet", message.getSnippet());
      messageDetails.put("threadId", message.getThreadId());
      messageDetails.put("id", message.getId());
      messageDetails.put("body", getText(email));

    } catch (MessagingException ex) {
      Logger.getLogger(GoogleAuthHelper.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
      Logger.getLogger(GoogleAuthHelper.class.getName()).log(Level.SEVERE, null, ex);
    }
    return messageDetails;
  }
 /**
  * Process specified MIME type of MimeMessage to get attachments and save them to disk.
  *
  * @param mime
  * @return
  * @throws MessagingException
  * @throws IOException
  * @throws CMSException
  * @throws OperatorCreationException
  * @throws CertificateException
  */
 protected String processMessage(MimeMessage mime)
     throws MessagingException, IOException, CMSException, OperatorCreationException,
         CertificateException {
   String attachList = null;
   if (mime.isMimeType("text/html")
       || mime.isMimeType("text/plain")
       || mime.isMimeType("multipart/mixed")) {
     // simple mail with attachment
     attachList = this.processAttachmentsOfSimpleMail(mime);
   } else if (mime.isMimeType("multipart/signed")) {
     // signed mail with attachment
     attachList = this.processAttachmentsOfSignedMail(mime);
   } else if (mime.isMimeType("application/pkcs7-mime")
       || mime.isMimeType("application/x-pkcs7-mime")) {
     String e = "It's an encrypted mail. Can not handle the Message receiving from Mail Server.";
     LOG.error(e);
     throw new MessagingException(e);
   } else {
     String e =
         "Unknown MIME Type | Message Content Type: "
             + mime.getContentType()
             + ", Message Subject: "
             + mime.getSubject()
             + ", Message Send Date: "
             + mime.getSentDate()
             + ", Message From: "
             + mime.getFrom().toString();
     LOG.error(e);
     throw new MessagingException(e);
   }
   return attachList;
 }
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    Properties prop = new Properties();
    Session session = Session.getDefaultInstance(prop, null);
    SendMail sm = new SendMail();
    String br = "\n";

    try {
      // 受信メールを取得
      MimeMessage recMsg = new MimeMessage(session, req.getInputStream());

      DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      String sentDate = df.format(recMsg.getSentDate());
      String from = recMsg.getFrom().toString();
      String subject = recMsg.getSubject();
      String content = this.getText(recMsg.getContent());

      String msgBody = "";
      msgBody += "sent at : " + sentDate + br;
      msgBody += "from : " + from + br;
      msgBody += "subject : " + subject + br;
      msgBody += "-----------content----------" + br + content;
      sm.send("*****@*****.**", "", "sutekitest転送", msgBody);
    } catch (MessagingException e) {
      sm.send("*****@*****.**", "", "sutekitestメール受信エラー", e.getMessage() + br + e.getCause());
    }
  }
 public Timestamp getSentDate() {
   MimeMessage message = getMessage();
   try {
     return UtilDateTime.toTimestamp(message.getSentDate());
   } catch (MessagingException e) {
     Debug.logError(e, module);
     return null;
   }
 }
Beispiel #5
0
 public static Map<String, Object> mcaTest(DispatchContext dctx, Map<String, ?> context) {
   MimeMessageWrapper wrapper = (MimeMessageWrapper) context.get("messageWrapper");
   MimeMessage message = wrapper.getMessage();
   try {
     if (message.getAllRecipients() != null) {
       Debug.log("To: " + UtilMisc.toListArray(message.getAllRecipients()), module);
     }
     if (message.getFrom() != null) {
       Debug.log("From: " + UtilMisc.toListArray(message.getFrom()), module);
     }
     Debug.log("Subject: " + message.getSubject(), module);
     if (message.getSentDate() != null) {
       Debug.log("Sent: " + message.getSentDate().toString(), module);
     }
     if (message.getReceivedDate() != null) {
       Debug.log("Received: " + message.getReceivedDate().toString(), module);
     }
   } catch (Exception e) {
     Debug.logError(e, module);
   }
   return ServiceUtil.returnSuccess();
 }
  /**
   * Set some email's basic information to MailMessage .
   *
   * @param mime
   * @param mailMsg
   * @throws MessagingException
   * @throws ParseException
   */
  private void setMailBasicInfoForMailMsg(MimeMessage mime, MailMessage mailMsg)
      throws MessagingException, ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
    String msgId = mime.getMessageID();
    Address[] from = mime.getFrom();
    Address[] to = mime.getRecipients(RecipientType.TO);
    Address[] cc = mime.getRecipients(RecipientType.CC);
    Address[] bcc = mime.getRecipients(RecipientType.BCC);
    String subject = mime.getSubject();
    Date sendDate = mime.getSentDate();
    String receivedUTCDate = sdf.format(this.resolveReceivedDate(mime));

    mailMsg.setMsgId(msgId);
    mailMsg.setFrom(this.convertToMailAddress(from));
    mailMsg.setTo(this.convertToMailAddress(to));
    mailMsg.setCc(this.convertToMailAddress(cc));
    mailMsg.setBcc(this.convertToMailAddress(bcc));
    mailMsg.setSubject(subject);
    if (sendDate != null) {
      mailMsg.setSendDate(sdf.format(sendDate));
    }
    mailMsg.setReceivedDate(receivedUTCDate);
  }
  public static void readTrainingMail(String fileNameWithPath) {
    InputStream is;

    try {
      is = new FileInputStream(fileNameWithPath);

      Properties props = System.getProperties();
      props.put("mail.host", "stmp.gmail.com");
      props.put("mail.transport.protocol", "stmp");
      Session mailSession = Session.getDefaultInstance(props, null);

      MimeMessage message = new MimeMessage(mailSession, is);

      if (message.getSubject() != null) {
        System.out.println("subject ---> " + message.getSubject());
      }

      if (message.getFrom() != null) {
        System.out.println(message.getFrom()[0].toString());
      }

      /**
       * The Message.RecipientType is divided into TO , the primary recipients -- CC ,carbon copy
       * recipients -- 抄送 BCC, blind carbon copy recipients-- 发送邮件副本,又不想让原始接收人知道,密抄送
       */
      if (message.getRecipients(MimeMessage.RecipientType.CC) != null) {
        System.out.println("CC : found " + message.getRecipients(Message.RecipientType.CC));
      }

      if (message.getRecipients(Message.RecipientType.BCC) != null) {
        System.out.println("BCc : Found " + message.getRecipients(Message.RecipientType.BCC));
      }

      if (message.getRecipients(Message.RecipientType.TO) != null) {
        System.out.println("To Found " + message.getRecipients(Message.RecipientType.TO));
        Address[] AddressList = message.getRecipients(Message.RecipientType.TO);

        for (Address internetAddress : AddressList) {
          InternetAddress addr = (InternetAddress) internetAddress;
          System.out.println(addr.getAddress());
        }
      }

      if (message.getSentDate() != null) {
        System.out.println("message Date : " + message.getSentDate());
      }

      System.out.println("here is the email 's content type name");
      System.out.println(message.getContentType());
      System.out.println("here is the email content ");
      System.out.println(message.getContent());

      if (message.getContentType().startsWith("multipart")) {
        Multipart multiPart = (Multipart) message.getContent();

        for (int x = 0; x < multiPart.getCount(); x++) {
          BodyPart bodyPart = multiPart.getBodyPart(x);
          String disposition = bodyPart.getDisposition();

          if (disposition != null) {
            String content = html2text((String) bodyPart.getContent()).trim();

            System.out.println("Content : " + content);
            System.out.println("disposition " + disposition);
          }

          System.out.println("is email contains disposition ? ");
          if (disposition != null && disposition.equals(BodyPart.ATTACHMENT))
            System.out.println("yes");
          else System.out.println("no");
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }