Package javax.mail.internet

Examples of javax.mail.internet.MimeMessage


      failedIdentitiesBCC.addAll(result.getFailedIdentites());
      failedIdentitiesBCC.removeAll(failedIdentitiesTO);
      failedIdentitiesBCC.removeAll(failedIdentitiesCC);
      recipientsBCC.removeAll(failedIdentitiesBCC);
    }
    MimeMessage msg = createWithContext(context, recipientTO, recipientsCC, recipientsBCC, template, sender, result);
    if(msg != null && result.getReturnCode() == MailerResult.OK){
      // send mail
      MailHelper.sendMessage(msg, result);
    }
  }
View Full Code Here


    }
    if (Tracing.isDebugEnabled(MailHelper.class)) {
      // enable mail session debugging on console
      mailSession.setDebug(true);
    }
    return new MimeMessage(mailSession);
  }
View Full Code Here

      String subject, File[] attachments, MailerResult result) {
    if (Tracing.isDebugEnabled(MailHelper.class)) {
      doDebugMessage(from, recipients, recipientsCC, recipientsBCC, body, subject, attachments);
    }

    MimeMessage msg = MailHelper.createMessage();
    try {
      // TO, CC and BCC
      msg.setFrom(from);
      msg.setRecipients(RecipientType.TO, recipients);
      if (recipientsCC != null) {
        msg.setRecipients(RecipientType.CC, recipientsCC);
      }
      if (recipientsBCC != null) {
        msg.setRecipients(RecipientType.BCC, recipientsBCC);
      }
      // message data
      msg.setSubject(subject, "utf-8");

      if (attachments != null && attachments.length > 0) {
        // with attachment use multipart message
        Multipart multipart = new MimeMultipart();
        // 1) add body part
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText(body);
        multipart.addBodyPart(messageBodyPart);
        // 2) add attachments
        for (File attachmentFile : attachments) {
          // abort if attachment does not exist
          if (attachmentFile == null || !attachmentFile.exists()) {
            result.setReturnCode(MailerResult.ATTACHMENT_INVALID);
            Tracing.logError("Tried to send mail wit attachment that does not exist::"
                + (attachmentFile == null ? null : attachmentFile.getAbsolutePath()), MailHelper.class);
            return msg;
          }
          messageBodyPart = new MimeBodyPart();
          DataSource source = new FileDataSource(attachmentFile);
          messageBodyPart.setDataHandler(new DataHandler(source));
          messageBodyPart.setFileName(attachmentFile.getName());
          multipart.addBodyPart(messageBodyPart);
        }
        // Put parts in message
        msg.setContent(multipart);
      } else {
        // without attachment everything is easy, just set as text
        msg.setText(body, "utf-8");
      }
      msg.setSentDate(new Date());
      msg.saveChanges();
    } catch (MessagingException e) {
      result.setReturnCode(MailerResult.SEND_GENERAL_ERROR);
      Tracing.logWarn("Could not create MimeMessage", e, MailHelper.class);
    }
    //
View Full Code Here

   * @param result MailerResult object that stores the result code
   */
  public static void sendMessage(Address from, Address[] recipients, Address[] recipientsCC, Address[] recipientsBCC, String body,
      String subject, File[] attachments, MailerResult result) {
    //
    MimeMessage msg = createMessage(from, recipients, recipientsCC, recipientsBCC, body, subject, attachments, result);
    sendMessage(msg, result);
  }
View Full Code Here

    public void sendMail( StandardMessage sm ) {
        log.debug( "sendMail: subject: " + sm.getSubject());
        System.out.println( "sending email" );
        StandardMessageFactory smf = new StandardMessageFactoryImpl();
        MimeMessage mm  = newMessage();
        smf.toMimeMessage( sm, mm );
        sendMail( mm );
    }
View Full Code Here

    public void sendMail(String from, String fromPersonal, List<String> to, String replyTo, String subject, String text) {
        if( !started ) {
            throw new RuntimeException("This mail sender is stopped");
        }
        try {
            MimeMessage mm = new MimeMessage(getSession());
            mm.setSubject(subject);
            mm.setFrom(new InternetAddress(from, fromPersonal));
            Address[] add = new Address[1];
            add[0] = new InternetAddress(replyTo);
            mm.setReplyTo(add);
            for (String sTo : to) {
                mm.addRecipient(RecipientType.TO, new InternetAddress(sTo));
            }
            mm.setContent(text, "text/plain");
            sendMail(mm);
        } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException(ex);
        } catch (MessagingException messagingException) {
            throw new RuntimeException(messagingException);
View Full Code Here

            }
           
            @Override
            protected void invokeApplication() throws Exception
            {
                MimeMessage renderedMessage = getRenderedMailMessage("/simple.xhtml");
            
                Assert.assertTrue(MailSession.instance().getTransport() instanceof MockTransport);
               
                // Test the headers
               
                Assert.assertNotNull(renderedMessage);
                Assert.assertEquals(renderedMessage.getAllRecipients().length, 1);
                Assert.assertTrue(renderedMessage.getAllRecipients()[0] instanceof InternetAddress);
                InternetAddress to = (InternetAddress) renderedMessage.getAllRecipients()[0];
                Assert.assertEquals(to.getAddress(), "test@example.com");
                Assert.assertEquals(to.getPersonal(), "Pete Muir");
                Assert.assertEquals(renderedMessage.getFrom().length, 1);
                Assert.assertTrue(renderedMessage.getFrom()[0] instanceof InternetAddress);
                InternetAddress from = (InternetAddress) renderedMessage.getFrom()[0];
                Assert.assertEquals(from.getAddress(), "peter@example.com");
                Assert.assertEquals(from.getPersonal(), "Peter");
                Assert.assertEquals(renderedMessage.getSubject(), "Try out Seam!");
                Assert.assertNull(renderedMessage.getHeader("Precedence"));
                Assert.assertNull(renderedMessage.getHeader("X-Priority"));
                Assert.assertNull(renderedMessage.getHeader("Priority"));
                Assert.assertNull(renderedMessage.getHeader("Importance"));
                Assert.assertNull(renderedMessage.getHeader("Disposition-Notification-To"));
               
                // Check the body
               
                Assert.assertNotNull(renderedMessage.getContent());
                Assert.assertTrue(renderedMessage.getContent() instanceof MimeMultipart);
                MimeMultipart body = (MimeMultipart) renderedMessage.getContent();
                Assert.assertEquals(body.getCount(), 1);
                Assert.assertNotNull(body.getBodyPart(0));
                Assert.assertTrue(body.getBodyPart(0) instanceof MimeBodyPart);
                MimeBodyPart bodyPart = (MimeBodyPart) body.getBodyPart(0);
                Assert.assertNotNull(bodyPart.getContent());
View Full Code Here

            }
           
            @Override
            protected void invokeApplication() throws Exception
            {
                MimeMessage renderedMessage = getRenderedMailMessage("/attachment.xhtml");
               
                // Test the headers
               
                InternetAddress to = (InternetAddress) renderedMessage.getAllRecipients()[0];
                Assert.assertEquals(to.getAddress(), "gavin@king.com");
                Assert.assertEquals(to.getPersonal(), "Gavin King");
                InternetAddress from = (InternetAddress) renderedMessage.getFrom()[0];
                Assert.assertEquals(from.getAddress(), "do-not-reply@jboss.com");
                Assert.assertEquals(from.getPersonal(), "Seam");
                Assert.assertEquals(renderedMessage.getSubject(), "Try out Seam!");
                MimeMultipart body = (MimeMultipart) renderedMessage.getContent();               
   
                Assert.assertEquals(body.getCount(), 4); //3 Attachments and 1 MimeMultipart               
               
                // The root multipart/related
                Assert.assertNotNull(body.getBodyPart(0));
View Full Code Here

            }
           
            @Override
            protected void invokeApplication() throws Exception
            {  
                MimeMessage renderedMessage = getRenderedMailMessage("/html.xhtml");
            
                // Test the standard headers
               
                InternetAddress to = (InternetAddress) renderedMessage.getAllRecipients()[0];
                Assert.assertEquals(to.getAddress(), "test@example.com");
                Assert.assertEquals(to.getPersonal(), "Pete Muir");
                InternetAddress from = (InternetAddress) renderedMessage.getFrom()[0];
                Assert.assertEquals(from.getAddress(), "do-not-reply@jboss.com");
                Assert.assertEquals(from.getPersonal(), "Seam");
                Assert.assertEquals(renderedMessage.getSubject(), "Seam Mail");
               
                // Test the extra headers
               
                // Importance
                Assert.assertNotNull(renderedMessage.getHeader("X-Priority"));
                Assert.assertNotNull(renderedMessage.getHeader("Priority"));
                Assert.assertNotNull(renderedMessage.getHeader("Importance"));
                Assert.assertEquals(renderedMessage.getHeader("X-Priority").length, 1);
                Assert.assertEquals(renderedMessage.getHeader("Priority").length, 1);
                Assert.assertEquals(renderedMessage.getHeader("Importance").length, 1);
                Assert.assertEquals(renderedMessage.getHeader("X-Priority")[0], "5");
                Assert.assertEquals(renderedMessage.getHeader("Priority")[0], "Non-urgent");
                Assert.assertEquals(renderedMessage.getHeader("Importance")[0], "low");
               
                // read receipt
                Assert.assertNotNull(renderedMessage.getHeader("Disposition-Notification-To"));
                Assert.assertEquals(renderedMessage.getHeader("Disposition-Notification-To").length,  1);
                Assert.assertEquals(renderedMessage.getHeader("Disposition-Notification-To")[0], "Seam <do-not-reply@jboss.com>");
               
                // m:header
                Assert.assertNotNull(renderedMessage.getHeader("X-Sent-From"));
                Assert.assertEquals(renderedMessage.getHeader("X-Sent-From").length, 1);
                Assert.assertEquals(renderedMessage.getHeader("X-Sent-From")[0], "Seam");
               
                MimeMultipart body = (MimeMultipart) renderedMessage.getContent();
               
                // Check the alternative facet
                Assert.assertTrue(renderedMessage.getContentType().startsWith("multipart/mixed"));
                Assert.assertEquals(body.getCount(), 1);
                MimeBodyPart bodyPart = (MimeBodyPart) body.getBodyPart(0);
                Assert.assertTrue(bodyPart.getContentType().startsWith("multipart/alternative"));
                Assert.assertTrue(bodyPart.getContent() instanceof MimeMultipart);
                MimeMultipart bodyParts = (MimeMultipart) bodyPart.getContent();
View Full Code Here

            }
           
            @Override
            protected void invokeApplication() throws Exception
            {  
                MimeMessage renderedMessage = getRenderedMailMessage("/plain.xhtml");
            
                // Test the standard headers
               
                InternetAddress to = (InternetAddress) renderedMessage.getAllRecipients()[0];
                Assert.assertEquals(to.getAddress(), "test@example.com");
                Assert.assertEquals(to.getPersonal(), "Pete Muir");
                InternetAddress from = (InternetAddress) renderedMessage.getFrom()[0];
                Assert.assertEquals(from.getAddress(), "do-not-reply@jboss.com");
                Assert.assertEquals(from.getPersonal(), "Seam");
                Assert.assertEquals(renderedMessage.getReplyTo().length,  1);
                Assert.assertTrue(renderedMessage.getReplyTo()[0] instanceof InternetAddress);
                InternetAddress replyTo = (InternetAddress) renderedMessage.getReplyTo()[0];
                Assert.assertEquals(replyTo.getAddress(), "another.address@jboss.org");
                Assert.assertEquals(replyTo.getPersonal(), "JBoss");
                Assert.assertEquals(renderedMessage.getRecipients(CC).length, 1);
                Assert.assertTrue(renderedMessage.getRecipients(CC)[0] instanceof InternetAddress);
                InternetAddress cc = (InternetAddress) renderedMessage.getRecipients(CC)[0];
                Assert.assertEquals(cc.getAddress(), "test@example.com");
                Assert.assertEquals(cc.getPersonal(), "Pete Muir");
                Assert.assertEquals(renderedMessage.getRecipients(BCC).length, 1);
                Assert.assertTrue(renderedMessage.getRecipients(BCC)[0] instanceof InternetAddress);
                InternetAddress bcc = (InternetAddress) renderedMessage.getRecipients(CC)[0];
                Assert.assertEquals(bcc.getAddress(), "test@example.com");
                Assert.assertEquals(bcc.getPersonal(), "Pete Muir");
                Assert.assertEquals(renderedMessage.getHeader("Precedence")[0], "bulk");
                // Importance
                Assert.assertNotNull(renderedMessage.getHeader("X-Priority"));
                Assert.assertNotNull(renderedMessage.getHeader("Priority"));
                Assert.assertNotNull(renderedMessage.getHeader("Importance"));
                Assert.assertEquals(renderedMessage.getHeader("X-Priority").length, 1);
                Assert.assertEquals(renderedMessage.getHeader("Priority").length, 1);
                Assert.assertEquals(renderedMessage.getHeader("Importance").length, 1);
                Assert.assertEquals(renderedMessage.getHeader("X-Priority")[0], "1");
                Assert.assertEquals(renderedMessage.getHeader("Priority")[0], "Urgent");
                Assert.assertEquals(renderedMessage.getHeader("Importance")[0], "high");
                Assert.assertEquals(renderedMessage.getSubject(), "Plain text email sent by Seam");
               
                // Check the body
               
                Assert.assertNotNull(renderedMessage.getContent());
                MimeMultipart body = (MimeMultipart) renderedMessage.getContent();
                Assert.assertEquals(body.getCount(), 1);
                MimeBodyPart bodyPart = (MimeBodyPart) body.getBodyPart(0);
                Assert.assertNotNull(bodyPart.getContent());
                Assert.assertEquals(bodyPart.getDisposition(), "inline");
                Assert.assertTrue(bodyPart.isMimeType("text/plain"));
View Full Code Here

TOP

Related Classes of javax.mail.internet.MimeMessage

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.