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

The following examples show how to use javax.mail.Message#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: FromTerm.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
/**
    * The address comparator.
    *
    * @param msg	The address comparison is applied to this Message
    * @return		true if the comparison succeeds, otherwise false
    */
   @Override
   public boolean match(Message msg) {
Address[] from;

try {
    from = msg.getFrom();
} catch (Exception e) {
    return false;
}

if (from == null)
    return false;

for (int i=0; i < from.length; i++)
    if (super.match(from[i]))
	return true;
return false;
   }
 
Example 2
Source File: FromStringTerm.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
/**
    * Check whether the address string specified in the constructor is
    * a substring of the From address of this Message.
    *
    * @param   msg 	The comparison is applied to this Message's From
    *		    	address.
    * @return          true if the match succeeds, otherwise false.
    */
   @Override
   public boolean match(Message msg) {
Address[] from;

try {
    from = msg.getFrom();
} catch (Exception e) {
    return false;
}

if (from == null)
    return false;

for (int i=0; i < from.length; i++)
    if (super.match(from[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 assertHeaders(ExpectedEmailHeader expectedEmail, Message actualEmail, AssertionRegistry assertions) throws MessagingException {
	Address[] from = actualEmail == null || actualEmail.getFrom() == null ? null : actualEmail.getFrom();
	assertions.register(() -> Assert.assertEquals("subject should be '" + expectedEmail.getSubject() + "'", expectedEmail.getSubject(), actualEmail == null ? null : actualEmail.getSubject()));
	assertions.register(() -> Assert.assertEquals("should have only one from", (Integer) 1, from == null ? null : from.length));
	assertions.register(() -> Assert.assertEquals("from should be '" + expectedEmail.getFrom() + "'", expectedEmail.getFrom(), from == null ? null : from[0].toString()));
	int recipients = expectedEmail.getTo().size() + expectedEmail.getBcc().size() + expectedEmail.getCc().size();
	assertions.register(() -> Assert.assertEquals("should be received by " + recipients + " recipients", (Integer) recipients,
			actualEmail == null || actualEmail.getAllRecipients() == null ? null : actualEmail.getAllRecipients().length));
	assertRecipients(expectedEmail.getTo(), actualEmail, RecipientType.TO, assertions);
	assertRecipients(expectedEmail.getCc(), actualEmail, RecipientType.CC, assertions);
	assertRecipients(expectedEmail.getBcc(), actualEmail, RecipientType.BCC, assertions);
}
 
Example 6
Source File: Email.java    From BotLibre with Eclipse Public License 1.0 5 votes vote down vote up
public String getFrom(Message message) throws MessagingException {
	Address from[] = message.getFrom();
	if ((from != null) && (from.length > 0) && (from[0] instanceof InternetAddress)) {
		return ((InternetAddress)from[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 getAddressFrom(final Message message) throws MessagingException {
  Address address = message.getFrom()[0];
  return matchEmailAddress(address.toString());
}
 
Example 8
Source File: TableLoaderInbox.java    From hana-native-adapters with Apache License 2.0 4 votes vote down vote up
@Override
protected void setColumnValue(int tablecolumnindex, int returncolumnindex, AdapterRow row, Object o) throws AdapterException {
	Message msg = (Message) o;
	try {
    	switch (tablecolumnindex) {
    	case 0:
    		Address from;
    		if (msg.getFrom().length > 0) {
    			from = msg.getFrom()[0];
	    		row.setColumnValue(returncolumnindex, checkLength(from.toString(), 127));
    		} else {
	    		row.setColumnNull(returncolumnindex);
    		}
    		break;
    	case 1:
    		row.setColumnValue(returncolumnindex, checkLength(msg.getSubject(), 512));
    		break;
    	case 2:
    		row.setColumnValue(returncolumnindex, new Timestamp(msg.getReceivedDate()));
    		break;
    	case 3:
    		row.setColumnValue(returncolumnindex, new Timestamp(msg.getSentDate()));
    		break;
    	case 4:
    		row.setColumnValue(returncolumnindex, checkLength(msg.getContentType(), 127));
    		break;
    	case 5:
    		row.setColumnValue(returncolumnindex, msg.getSize());
    		break;
    	case 6:
    		Address reply;
    		if (msg.getReplyTo() != null && msg.getReplyTo().length > 0) {
    			reply = msg.getReplyTo()[0];
    			row.setColumnValue(returncolumnindex, checkLength(reply.toString(), 1024));
    		} else {
    			row.setColumnNull(returncolumnindex);
    		}
    		break;
    	case 7:
    		Object contentObj = msg.getContent();
    		String resultString = null;
    		if (contentObj instanceof Multipart) {
    			BodyPart clearTextPart = null;
    			BodyPart htmlTextPart = null;
    			Multipart content = (Multipart)contentObj;
    			int count = content.getCount();
    			for(int i=0; i<count; i++)
    			{
    				BodyPart part =  content.getBodyPart(i);
    				if (part.isMimeType("text/plain")) {
    					clearTextPart = part;
    					break;
    				}
    				else if(part.isMimeType("text/html")) {
    					htmlTextPart = part;
    				}
    			}

    			if (clearTextPart != null) {
    				resultString = (String) clearTextPart.getContent();
    			} else if (htmlTextPart != null) {
    				String html = (String) htmlTextPart.getContent();
    				resultString = Jsoup.parse(html).text();
    			}

    		} else if (contentObj instanceof String) {
    			if (msg.getContentType().startsWith("text/html")) {
    				resultString = Jsoup.parse((String) contentObj).text();
    			} else {
    				resultString = (String) contentObj;
    			}
    		} else {
    			resultString = contentObj.toString();
    		}
    		row.setColumnValue(returncolumnindex, checkLength(resultString, 5000));
    		break;
    	}		
	} catch (MessagingException | IOException e) {
		throw new AdapterException(e);
	}
}
 
Example 9
Source File: cfMailMessageData.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
private boolean extractMessage( Message Mess, long messageID, String attachURI, String attachDIR ){
cfArrayData ADD	= cfArrayData.createArray(1);

try{

	setData( "subject", 	new cfStringData( Mess.getSubject() ) );
	setData( "id",				new cfNumberData( messageID ) );
	
	//--- Pull out all the headers
	cfStructData	headers	= new cfStructData();
	Enumeration<Header> eH	= Mess.getAllHeaders();
	String headerKey;
	while (eH.hasMoreElements()){
		Header hdr	= eH.nextElement();
		
		headerKey = hdr.getName().replace('-','_').toLowerCase();
		if ( headers.containsKey(headerKey) ){
			headers.setData( headerKey, new cfStringData( headers.getData(headerKey).toString() + ";" + hdr.getValue() ) );
		}else
			headers.setData( headerKey, new cfStringData( hdr.getValue() ) );
	}
	
	setData( "headers",  headers );

	// Get the Date
    Date DD = Mess.getReceivedDate();
    if ( DD == null )
			setData( "rxddate", 	new cfDateData( System.currentTimeMillis() ) );
	  else
			setData( "rxddate", 	new cfDateData( DD.getTime() ) );


    DD = Mess.getSentDate();
    if ( DD == null )
			setData( "sentdate", 	new cfDateData( System.currentTimeMillis() ) );
	  else
			setData( "sentdate", 	new cfDateData( DD.getTime() ) );
	
	// Get the FROM field
	Address[] from	= Mess.getFrom();
	if ( from != null && from.length > 0 ){
		cfStructData sdFrom	= new cfStructData();
		String name =  ((InternetAddress)from[0]).getPersonal();
		if ( name != null )
				sdFrom.setData( "name", new cfStringData(name) );
				
		sdFrom.setData( "email", new cfStringData( ((InternetAddress)from[0]).getAddress() ) );
		setData( "from", sdFrom );
	}
	
	//--[ Get the TO/CC/BCC field
	cfArrayData	AD	= extractAddresses( Mess.getRecipients(Message.RecipientType.TO) );
	if ( AD != null )
		setData( "to", AD );

	AD	= extractAddresses( Mess.getRecipients(Message.RecipientType.CC) );
	if ( AD != null )
		setData( "cc", AD );		

	AD	= extractAddresses( Mess.getRecipients(Message.RecipientType.BCC) );
	if ( AD != null )
		setData( "bcc", AD );
		
	AD	= extractAddresses( Mess.getReplyTo() );
	if ( AD != null )
		setData( "replyto", AD );
		
	//--[ Set the flags
	setData( "answered",	cfBooleanData.getcfBooleanData( Mess.isSet( Flags.Flag.ANSWERED ) ) );
	setData( "deleted",		cfBooleanData.getcfBooleanData( Mess.isSet( Flags.Flag.DELETED ) ) );
	setData( "draft",			cfBooleanData.getcfBooleanData( Mess.isSet( Flags.Flag.DRAFT ) ) );
	setData( "flagged",		cfBooleanData.getcfBooleanData( Mess.isSet( Flags.Flag.FLAGGED ) ) );
	setData( "recent",		cfBooleanData.getcfBooleanData( Mess.isSet( Flags.Flag.RECENT ) ) );
	setData( "seen",			cfBooleanData.getcfBooleanData( Mess.isSet( Flags.Flag.SEEN ) ) );

	setData( "size",			new cfNumberData( Mess.getSize() ) );
		setData( "lines",			new cfNumberData( Mess.getLineCount() ) );
	
	String tmp	= Mess.getContentType();
	if ( tmp.indexOf(";") != -1 )
		tmp	= tmp.substring( 0, tmp.indexOf(";") );

	setData( "mimetype", new cfStringData( tmp ) );
	
	// Get the body of the email
	extractBody( Mess, ADD, attachURI, attachDIR );

}catch(Exception E){
	return false;
}
	
setData( "body",	ADD );
return true;
}
 
Example 10
Source File: cfMailFolderMessagesData.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
private Map<String, cfData> extractMessage( Message Mess ){
 	Map<String, cfData> HT	= new FastMap<String, cfData>();
	
try{
	HT.put( "subject", 	new cfStringData( Mess.getSubject() ) );
	
	Folder parentFolder	= Mess.getFolder();
	if ( parentFolder instanceof UIDFolder )
		HT.put( "id",	new cfNumberData( ((UIDFolder)parentFolder).getUID( Mess) ) );
	else
		HT.put( "id",	new cfNumberData( Mess.getMessageNumber() ) );
	
	//--[ Get the FROM field
	Address[] from	= Mess.getFrom();
	if ( from != null && from.length > 0 ){
		cfStructData sdFrom	= new cfStructData();
		
		String name = ((InternetAddress)from[0]).getPersonal();
		if ( name != null )
				sdFrom.setData( "name", new cfStringData( name ) );
		
		sdFrom.setData( "email", new cfStringData( ((InternetAddress)from[0]).getAddress() ) );
		HT.put( "from", sdFrom );
	}
	
	//--[ Get the TO/CC/BCC field
	cfArrayData	AD	= extractAddresses( Mess.getRecipients(Message.RecipientType.TO) );
	if ( AD != null )
		HT.put( "to", AD );
    else
      HT.put( "to", cfNullData.NULL );
      

	AD	= extractAddresses( Mess.getRecipients(Message.RecipientType.CC) );
	if ( AD != null )
		HT.put( "cc", AD );		
    else
      HT.put( "cc", cfNullData.NULL );

	AD	= extractAddresses( Mess.getRecipients(Message.RecipientType.BCC) );
	if ( AD != null )
		HT.put( "bcc", AD );
    else
      HT.put( "bcc", cfNullData.NULL );

	//--[ Set the flags
	HT.put( "answered",	cfBooleanData.getcfBooleanData( Mess.isSet( Flags.Flag.ANSWERED ) ) );
	HT.put( "deleted",	cfBooleanData.getcfBooleanData( Mess.isSet( Flags.Flag.DELETED ) ) );
	HT.put( "draft",		cfBooleanData.getcfBooleanData( Mess.isSet( Flags.Flag.DRAFT ) ) );
	HT.put( "flagged",	cfBooleanData.getcfBooleanData( Mess.isSet( Flags.Flag.FLAGGED ) ) );
	HT.put( "recent",		cfBooleanData.getcfBooleanData( Mess.isSet( Flags.Flag.RECENT ) ) );
	HT.put( "seen",			cfBooleanData.getcfBooleanData( Mess.isSet( Flags.Flag.SEEN ) ) );

	HT.put( "size",			new cfNumberData( Mess.getSize() ) );
	HT.put( "lines",		new cfNumberData( Mess.getLineCount() ) );

    Date DD = Mess.getReceivedDate();
    if ( DD == null )
			HT.put( "rxddate", 	new cfDateData( System.currentTimeMillis() ) );
	  else
			HT.put( "rxddate", 	new cfDateData( DD.getTime() ) );


    DD = Mess.getSentDate();
    if ( DD == null )
			HT.put( "sentdate", 	new cfDateData( System.currentTimeMillis() ) );
	  else
			HT.put( "sentdate", 	new cfDateData( DD.getTime() ) );

}catch(Exception E){
  com.nary.Debug.printStackTrace( E );
}
	
return HT;  	  
}
 
Example 11
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
    }
  }
}