Java Code Examples for javax.mail.Message#getRecipients()

The following examples show how to use javax.mail.Message#getRecipients() . 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: RecipientStringTerm.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
/**
    * Check whether the address specified in the constructor is
    * a substring of the recipient address of this Message.
    *
    * @param   msg 	The comparison is applied to this Message's recipient
    *		    	address.
    * @return          true if the match succeeds, otherwise false.
    */
   @Override
   public boolean match(Message msg) {
Address[] recipients;

try {
    recipients = msg.getRecipients(type);
} catch (Exception e) {
    return false;
}

if (recipients == null)
    return false;

for (int i=0; i < recipients.length; i++)
    if (super.match(recipients[i]))
	return true;
return false;
   }
 
Example 2
Source File: RecipientTerm.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
/**
    * The match method.
    *
    * @param msg	The address match is applied to this Message's recepient
    *			address
    * @return		true if the match succeeds, otherwise false
    */
   @Override
   public boolean match(Message msg) {
Address[] recipients;

try {
	    recipients = msg.getRecipients(type);
} catch (Exception e) {
    return false;
}

if (recipients == null)
    return false;

for (int i=0; i < recipients.length; i++)
    if (super.match(recipients[i]))
	return true;
return false;
   }
 
Example 3
Source File: MailUtils.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 过滤邮件中的 From 和 To,使邮件不允许发件人和收件人一样.
 * @param message
 *            邮件对象
 * @throws MessagingException
 *             the messaging exception
 */
public static void removeDumplicate(Message message) throws MessagingException {
	Address[] from = message.getFrom();
	Address[] to = message.getRecipients(Message.RecipientType.TO);
	Address[] cc = message.getRecipients(Message.RecipientType.CC);
	Address[] bcc = message.getRecipients(Message.RecipientType.BCC);
	Address[] tonew = removeDuplicate(from, to);
	Address[] ccnew = removeDuplicate(from, cc);
	Address[] bccnew = removeDuplicate(from, bcc);
	if (tonew != null) {
		message.setRecipients(Message.RecipientType.TO, tonew);
	}
	if (ccnew != null) {
		message.setRecipients(Message.RecipientType.CC, ccnew);
	}
	if (bccnew != null) {
		message.setRecipients(Message.RecipientType.BCC, bccnew);
	}
}
 
Example 4
Source File: MailUtils.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 过滤邮件中的 From 和 To,使邮件不允许发件人和收件人一样.
 * @param message
 *            邮件对象
 * @throws MessagingException
 *             the messaging exception
 */
public static void removeDumplicate(Message message) throws MessagingException {
	Address[] from = message.getFrom();
	Address[] to = message.getRecipients(Message.RecipientType.TO);
	Address[] cc = message.getRecipients(Message.RecipientType.CC);
	Address[] bcc = message.getRecipients(Message.RecipientType.BCC);
	Address[] tonew = removeDuplicate(from, to);
	Address[] ccnew = removeDuplicate(from, cc);
	Address[] bccnew = removeDuplicate(from, bcc);
	if (tonew != null) {
		message.setRecipients(Message.RecipientType.TO, tonew);
	}
	if (ccnew != null) {
		message.setRecipients(Message.RecipientType.CC, ccnew);
	}
	if (bccnew != null) {
		message.setRecipients(Message.RecipientType.BCC, bccnew);
	}
}
 
Example 5
Source File: AssertEmail.java    From ogham with Apache License 2.0 5 votes vote down vote up
private static void assertRecipients(List<String> expectedRecipients, Message actualEmail, RecipientType recipientType, AssertionRegistry assertions) throws MessagingException {
	Address[] actualRecipients = actualEmail == null ? null : actualEmail.getRecipients(recipientType);
	if (expectedRecipients.isEmpty()) {
		assertions.register(() -> Assert.assertTrue("should be received by no recipients (of type RecipientType." + recipientType + ")", actualRecipients == null || actualRecipients.length == 0));
	} else {
		assertions.register(() -> Assert.assertEquals("should be received by " + expectedRecipients.size() + " recipients (of type RecipientType." + recipientType + ")",
				(Integer) expectedRecipients.size(), actualRecipients == null ? null : actualRecipients.length));
		for (int i = 0; i < expectedRecipients.size(); i++) {
			final int idx = i;
			assertions.register(() -> Assert.assertEquals("recipient " + recipientType + "[" + idx + "] should be '" + expectedRecipients.get(idx) + "'", expectedRecipients.get(idx),
					actualRecipients != null && idx < actualRecipients.length ? actualRecipients[idx].toString() : null));
		}
	}
}
 
Example 6
Source File: Email.java    From BotLibre with Eclipse Public License 1.0 5 votes vote down vote up
public String getRecipient(Message message) throws MessagingException {
	Address to[] = message.getRecipients(RecipientType.TO);
	if ((to != null) && (to.length > 0) && (to[0] instanceof InternetAddress)) {
		return ((InternetAddress)to[0]).getAddress();
	}
	return null;
}
 
Example 7
Source File: EmailDataFactory.java    From bobcat with Apache License 2.0 4 votes vote down vote up
private String getAddressTo(final Message message) throws MessagingException {
  Address address = message.getRecipients(Message.RecipientType.TO)[0];
  return matchEmailAddress(address.toString());
}
 
Example 8
Source File: MailAccount.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
protected void setEnvelope(final Mail mail, final Message message) throws javax.mail.MessagingException
{
  mail.setMessage(message);
  Address[] addr;
  // ID
  mail.setMessageNumber(message.getMessageNumber());

  // FROM
  StringBuffer buf = new StringBuffer();
  addr = message.getFrom();
  if (addr != null) {
    for (int j = 0; j < addr.length; j++) {
      if (j > 0)
        buf.append(",");
      buf.append(addr[j].toString());
    }
  }
  mail.setFrom(buf.toString());

  // TO
  addr = message.getRecipients(Message.RecipientType.TO);
  buf = new StringBuffer();
  if (addr != null) {
    for (int j = 0; j < addr.length; j++) {
      if (j > 0)
        buf.append(",");
      buf.append(addr[j].toString());
    }
  }
  mail.setTo(buf.toString());

  // SUBJECT
  mail.setSubject(message.getSubject());

  // DATE
  final Date date = message.getSentDate();
  if (date != null) {
    mail.setDate(date);
  } else { // Needed for compareTo (assume 1.1.1970)
    mail.setDate(new Date(0));
  } // FLAGS
  final Flags flags = message.getFlags();
  final Flags.Flag[] systemFlags = flags.getSystemFlags(); // get the system flags

  for (int i = 0; i < systemFlags.length; i++) {
    final Flags.Flag flag = systemFlags[i];
    if (flag == Flags.Flag.ANSWERED) {
      // Ignore this flag
    } else if (flag == Flags.Flag.DELETED) {
      mail.setDeleted(true);
    } else if (flag == Flags.Flag.DRAFT) {
      // Ignore this flag
    } else if (flag == Flags.Flag.FLAGGED) {
      // Ignore this flag
    } else if (flag == Flags.Flag.RECENT) {
      mail.setRecent(true);
    } else if (flag == Flags.Flag.SEEN) {
      mail.setSeen(true);
    } else {
      // skip it
    }
  }
}