public void sendMessage ( MailItem item ) throws MessagingException { checkNotNull ( item . getReceivers ( ) ) ; checkNotNull ( item . getReceivers ( ) . get ( 0 ) ) ; checkNotNull ( item . getTitle ( ) ) ; checkNotNull ( item . getBody ( ) ) ; //If sender is empty send with the account sender. Message msg = new MimeMessage ( session ) ; if ( item . getSender ( ) == null || item . getSender ( ) . length ( ) == 0 ) { item . setSender ( configuration . getProperties ( ) . get ( configuration . getUsernameKey ( ) ) . toString ( ) ) ; } InternetAddress from = new InternetAddress ( item . getSender ( ) ) ; msg . setFrom ( from ) ; InternetAddress [ ] to = new InternetAddress [ item . getReceivers ( ) . size ( ) ] ; for ( int i = 0 ; i < item . getReceivers ( ) . size ( ) ; i ++ ) { to [ i ] = new InternetAddress ( item . getReceivers ( ) . get ( i ) ) ; } msg . setRecipients ( Message . RecipientType . TO , to ) ; msg . setSubject ( item . getTitle ( ) ) ; MimeBodyPart body = new MimeBodyPart ( ) ; body . setContent ( item . getBody ( ) , "text/html; charset=UTF-8" ) ; Multipart content = new MimeMultipart ( ) ; content . addBodyPart ( body ) ; if ( item . getAttachments ( ) != null && item . getAttachments ( ) . size ( ) > 0 ) { for ( DataSource attachment : item . getAttachments ( ) ) { BodyPart itemBodyPart = new MimeBodyPart ( ) ; itemBodyPart . setDataHandler ( new DataHandler ( attachment ) ) ; itemBodyPart . setFileName ( attachment . getName ( ) ) ; content . addBodyPart ( itemBodyPart ) ; } } msg . setContent ( content ) ; //update headers msg . saveChanges ( ) ; Transport . send ( msg ) ; // set header value for ( Map . Entry < String , String [ ] > entry : item . getHeaders ( ) . entrySet ( ) ) { String [ ] value = msg . getHeader ( entry . getKey ( ) ) ; if ( value != null ) { entry . setValue ( value ) ; } } }
Sends a mail with the given item .