Java Code Examples for javax.mail.internet.MimeMessage#getFrom()

The following examples show how to use javax.mail.internet.MimeMessage#getFrom() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Pop3Util.java    From anyline with Apache License 2.0 6 votes vote down vote up
/**  
   * 获得邮件发件人  
   * @param msg 邮件内容  
   * @return 姓名 <Email地址>  
   */   
  public static String getFrom(MimeMessage msg){   
      String from = "";   
      Address[] froms; 
try { 
	froms = msg.getFrom(); 
       InternetAddress address = (InternetAddress) froms[0];   
       String person = address.getPersonal();   
       if (person != null) {   
           person = MimeUtility.decodeText(person) + " ";   
       } else {   
           person = "";   
       }   
       from = person + "<" + address.getAddress() + ">";   
} catch (Exception e) { 
	e.printStackTrace(); 
}   
      return from;   
  }
 
Example 2
Source File: CommonServices.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
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.logInfo("To: " + UtilMisc.toListArray(message.getAllRecipients()), module);
        }
        if (message.getFrom() != null) {
           Debug.logInfo("From: " + UtilMisc.toListArray(message.getFrom()), module);
        }
        Debug.logInfo("Subject: " + message.getSubject(), module);
        if (message.getSentDate() != null) {
            Debug.logInfo("Sent: " + message.getSentDate().toString(), module);
        }
        if (message.getReceivedDate() != null) {
            Debug.logInfo("Received: " + message.getReceivedDate().toString(), module);
        }
    } catch (Exception e) {
        Debug.logError(e, module);
    }
    return ServiceUtil.returnSuccess();
}
 
Example 3
Source File: MailUtils.java    From scada with MIT License 6 votes vote down vote up
/**
 * ����ʼ�������
 * @param msg �ʼ�����
 * @return ���� <Email��ַ>
 */ 
public static String getFrom(MimeMessage msg) throws MessagingException, UnsupportedEncodingException { 
    String from = ""; 
    Address[] froms = msg.getFrom(); 
    if (froms.length < 1){
    	//return "ϵͳ�ָ�";
        throw new MessagingException("û�з�����!");
    }         
    InternetAddress address = (InternetAddress) froms[0]; 
    String person = address.getPersonal(); 
    if (person != null) { 
        person = MimeUtility.decodeText(person) + " "; 
    } else { 
        person = ""; 
    } 
    from = person + "<" + address.getAddress() + ">"; 
     
    return from; 
}
 
Example 4
Source File: JamesMailetContext.java    From james-project with Apache License 2.0 6 votes vote down vote up
/**
 * Place a mail on the spool for processing
 *
 * @param message the message to send
 * @throws MessagingException if an exception is caught while placing the mail on the spool
 */
@Override
public void sendMail(MimeMessage message) throws MessagingException {
    MailAddress sender = new MailAddress((InternetAddress) message.getFrom()[0]);
    Collection<MailAddress> recipients = new HashSet<>();
    Address[] addresses = message.getAllRecipients();
    if (addresses != null) {
        for (Address address : addresses) {
            // Javamail treats the "newsgroups:" header field as a
            // recipient, so we want to filter those out.
            if (address instanceof InternetAddress) {
                recipients.add(new MailAddress((InternetAddress) address));
            }
        }
    }
    sendMail(sender, recipients, message);
}
 
Example 5
Source File: JavamailService.java    From lemon with Apache License 2.0 6 votes vote down vote up
public static String getFrom(MimeMessage msg) throws MessagingException,
        UnsupportedEncodingException {
    String from = "";
    Address[] froms = msg.getFrom();

    if (froms.length < 1) {
        throw new MessagingException("没有发件人!");
    }

    InternetAddress address = (InternetAddress) froms[0];
    String person = address.getPersonal();

    if (person != null) {
        person = MimeUtility.decodeText(person) + " ";
    } else {
        person = "";
    }

    from = person + "<" + address.getAddress() + ">";

    return from;
}
 
Example 6
Source File: MimeMessageWrapper.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
public Address[] getFrom() {
    MimeMessage message = getMessage();
    try {
        return message.getFrom();
    } catch (MessagingException e) {
        Debug.logError(e, module);
        return null;
    }
}
 
Example 7
Source File: MailEntityProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void addEnvelopeToDocument(Part part, Map<String,Object> row)
    throws MessagingException {
  MimeMessage mail = (MimeMessage) part;
  Address[] adresses;
  if ((adresses = mail.getFrom()) != null && adresses.length > 0) row.put(
      FROM, adresses[0].toString());
  
  List<String> to = new ArrayList<>();
  if ((adresses = mail.getRecipients(Message.RecipientType.TO)) != null) addAddressToList(
      adresses, to);
  if ((adresses = mail.getRecipients(Message.RecipientType.CC)) != null) addAddressToList(
      adresses, to);
  if ((adresses = mail.getRecipients(Message.RecipientType.BCC)) != null) addAddressToList(
      adresses, to);
  if (to.size() > 0) row.put(TO_CC_BCC, to);
  
  row.put(MESSAGE_ID, mail.getMessageID());
  row.put(SUBJECT, mail.getSubject());
  
  Date d = mail.getSentDate();
  if (d != null) {
    row.put(SENT_DATE, d);
  }
  
  List<String> flags = new ArrayList<>();
  for (Flags.Flag flag : mail.getFlags().getSystemFlags()) {
    if (flag == Flags.Flag.ANSWERED) flags.add(FLAG_ANSWERED);
    else if (flag == Flags.Flag.DELETED) flags.add(FLAG_DELETED);
    else if (flag == Flags.Flag.DRAFT) flags.add(FLAG_DRAFT);
    else if (flag == Flags.Flag.FLAGGED) flags.add(FLAG_FLAGGED);
    else if (flag == Flags.Flag.RECENT) flags.add(FLAG_RECENT);
    else if (flag == Flags.Flag.SEEN) flags.add(FLAG_SEEN);
  }
  flags.addAll(Arrays.asList(mail.getFlags().getUserFlags()));
  if (flags.size() == 0) flags.add(FLAG_NONE);
  row.put(FLAGS, flags);
  
  String[] hdrs = mail.getHeader("X-Mailer");
  if (hdrs != null) row.put(XMAILER, hdrs[0]);
}
 
Example 8
Source File: ServerTime.java    From james-project with Apache License 2.0 5 votes vote down vote up
/**
 * Sends a message back to the sender indicating what time the server thinks it is.
 *
 * @param mail the mail being processed
 *
 * @throws javax.mail.MessagingException if an error is encountered while formulating the reply message
 */
@Override
public void service(Mail mail) throws javax.mail.MessagingException {
    if (!mail.hasSender()) {
        return;
    }

    MimeMessage response = (MimeMessage)mail.getMessage().reply(false);
    response.setSubject("The time is now...");
    String textBuffer = "This mail server thinks it's " + (new java.util.Date()).toString() + ".";
    response.setText(textBuffer);

    // Someone manually checking the server time by hand may send
    // an formatted message, lacking From and To headers.  If the
    // response fields are null, try setting them from the SMTP
    // MAIL FROM/RCPT TO commands used to send the inquiry.

    if (response.getFrom() == null) {
        response.setFrom(mail.getRecipients().iterator().next().toInternetAddress());
    }

    if (response.getAllRecipients() == null) {
        response.setRecipients(MimeMessage.RecipientType.TO, mail.getMaybeSender().get().toString());
    }

    response.saveChanges();
    getMailetContext().sendMail(response);
}
 
Example 9
Source File: MailImpl.java    From james-project with Apache License 2.0 4 votes vote down vote up
private static MailAddress getSender(MimeMessage mimeMessage) throws MessagingException {
    Address[] sender = mimeMessage.getFrom();
    Preconditions.checkArgument(sender.length == 1);
    return castToMailAddress(sender[0]);
}
 
Example 10
Source File: MimeProperties.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
private void initWithMimeMessage(MimeMessage mime) {
    try {
        if (mime.getSubject() != null) {
            subject = mime.getSubject();
        }

        if (mime.getFrom() != null) {
            from = mime.getFrom()[0].toString();
        }

        Address[] addresses;
        addresses = mime.getRecipients(Message.RecipientType.TO);
        if (addresses != null) {
            to = addresses[0].toString();
        }

        addresses = mime.getRecipients(Message.RecipientType.CC);
        if (addresses != null) {
            cc = addresses[0].toString();
        }

        addresses = mime.getRecipients(Message.RecipientType.BCC);
        if (addresses != null) {
            bcc = addresses[0].toString();
        }

        addresses = mime.getReplyTo();
        if (addresses != null) {
            replyTo = addresses[0].toString();
        }

        if (mime.getContent() instanceof String) {
            body = mime.getContent().toString().trim();
            log.info("ContentTypeString: " + mime.getContentType());
            log.info("ContentString: " + mime.getContent().toString().trim());
        }

        if (mime.getContent() instanceof Multipart) {
            Multipart multipart = (Multipart) mime.getContent();
            for (int i = 0; i < multipart.getCount(); i++) {
                BodyPart bodyPart = multipart.getBodyPart(i);
                String content = getContentAsString(bodyPart);
                log.info("ContentTypeMultiPart: " + bodyPart.getContentType());
                log.info("Content: " + content);
                multiPartsList.add(content);
            }
        }

    } catch (MessagingException | IOException me) {
        throw new IllegalStateException(me);
    }
}