Advertisement
NunoFurtado

java send email

Feb 6th, 2014
1,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. public boolean sendEMail( String sender,EmailArtifact artifact)  throws InvalidEmailAddressException, SendEmailException
  2.     {
  3.        
  4.         try {
  5.        
  6.             javax.mail.Session session =null;
  7.  
  8.             if(StringUtils.isBlank(username)){
  9.                 session  = Session.getInstance(props, null);     
  10.             }else{
  11.                 /**
  12.                  * Using Default session generates an error : access to Default Session denied~
  13.                  * It seems the appserver already created a session using Session.getDefaultSession
  14.                  * which is denying us access to it
  15.                  */
  16.                 session= javax.mail.Session.getInstance( props,
  17.                         new Authenticator (  )   {  
  18.                                 public PasswordAuthentication getPasswordAuthentication (  )   {  
  19.                                     return new PasswordAuthentication ( username, password ) ;
  20.                                 }  
  21.                         }  );              
  22.             }
  23.        
  24.            
  25.             MimeMessage message = new MimeMessage( session );
  26.             message.setFrom(new InternetAddress( sender ));
  27.             message.setHeader("Content-Type","text/plain; charset=\"UTF-8\"");
  28.             message.setHeader("Content-Transfer-Encoding", "quoted-printable");
  29.             if (artifact.getTo() != null) {
  30.                 for ( Iterator<String> it = artifact.getTo().iterator() ; it.hasNext() ; )
  31.                     message.addRecipient(
  32.                             javax.mail.Message.RecipientType.TO,
  33.                             new InternetAddress( (String) it.next() )
  34.                     );
  35.             }
  36.            
  37.             if (artifact.getCc() != null) {
  38.                 for ( Iterator<String> it = artifact.getCc().iterator() ; it.hasNext() ; )
  39.                     message.addRecipient(
  40.                             javax.mail.Message.RecipientType.CC,
  41.                             new InternetAddress( (String) it.next() )
  42.                     );
  43.             }
  44.            
  45.             if (artifact.getBcc() != null) {
  46.                 for ( Iterator<String> it = artifact.getBcc().iterator() ; it.hasNext() ; )
  47.                     message.addRecipient(
  48.                             javax.mail.Message.RecipientType.BCC,
  49.                             new InternetAddress( (String) it.next() )
  50.                     );
  51.             }
  52.            
  53.             message.setSubject( artifact.getSubject() ,"UTF-8");
  54.             message.setSentDate ( new Date()) ;
  55.            
  56.             // create and fill the first message part
  57.             MimeBodyPart mbp1 = new MimeBodyPart (  ) ;
  58.  
  59.             mbp1.setContent(artifact.getBody() +"\n\n"+artifact.getSignature(), "text/plain; charset=UTF-8");
  60.            
  61.             mbp1.setHeader("Content-Type","text/plain; charset=UTF-8");
  62.             mbp1.setHeader("Content-Transfer-Encoding", "quoted-printable");
  63.             Multipart mp = new MimeMultipart (  ) ;
  64.             mp.addBodyPart ( mbp1 ) ;
  65.             for (Entry<String, byte[]> att : artifact.getAttachments().entrySet()) {
  66.                 MimeBodyPart attachment = new MimeBodyPart (  ) ;
  67.                 attachment.setDataHandler ( new DataHandler ( new ByteArrayDataSource(att.getValue(),"application/octet-stream") ) );
  68.                 attachment.setFileName(att.getKey());
  69.                 mp.addBodyPart ( attachment ) ;
  70.             }
  71.  
  72.  
  73.             // add the Multipart to the message
  74.             message.setContent ( mp) ;
  75.  
  76.             Transport.send( message );
  77.  
  78.             return true;
  79.         }
  80.         catch ( AddressException e )
  81.         {
  82.             throw new InvalidEmailAddressException(e.getMessage());
  83.         }
  84.         catch ( MessagingException e )
  85.         {
  86.             throw new SendEmailException(e);
  87.         }
  88.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement