Examples of MimeMessage


Examples of javax.mail.internet.MimeMessage

      }else{
        throw new SendFailedException("<error>The SMTP Server has not been setup.</error>");
      }

      Session session = Session.getDefaultInstance(props, null);
      MimeMessage message = new MimeMessage(session);

      Collection bccList = mailMessageVO.getBccList();
      Collection ccList = mailMessageVO.getCcList();
      Collection toList = mailMessageVO.getToList();
      Collection attachments = mailMessageVO.getAttachedFiles();

      String subject = mailMessageVO.getSubject();
      String body = mailMessageVO.getBody();
      String fromAddress = mailMessageVO.getFromAddress();
      String replyToAddress = mailMessageVO.getReplyTo();
      String headers = mailMessageVO.getHeaders();
      String messageType = mailMessageVO.getContentType();

      //if you don't specify the from email address we will enter the administrator email address
      if(fromAddress == null){
        fromAddress =  adminEmailAddress;
      }
      message.setFrom(new InternetAddress(fromAddress));

      if (replyToAddress != null && !replyToAddress.equals("")){
        message.setReplyTo(new Address[] {new InternetAddress(replyToAddress)});
      }

      //Add raw headers to message object
      StringTokenizer tokenizer = new StringTokenizer(headers, System.getProperty("line.separator", "\n"));
      while (tokenizer.hasMoreTokens()) {
        message.addHeaderLine(tokenizer.nextToken());
      }

      //Most email clients add this line with the name of
      //their software and the version
      message.addHeader("X-Mailer", "Centraview v. " + CentraViewConfiguration.getVersion());

      message.setSubject(subject);
      BodyPart messageBodyPart = new MimeBodyPart();
      messageBodyPart.setContent(body, messageType);

      Multipart multipart = new MimeMultipart();
      multipart.addBodyPart(messageBodyPart);

      // Handle attachments
      if (attachments != null) {
        message.setContent(multipart);
        Iterator attachmentIterator = attachments.iterator();
        while (attachmentIterator.hasNext()) {
          messageBodyPart = new MimeBodyPart();
          CvFileVO thisAttachment = (CvFileVO) attachmentIterator.next();
          String path = thisAttachment.getPhysicalFolderVO().getFullPath(null, true) + thisAttachment.getName();
          DataSource source = new FileDataSource(path);
          messageBodyPart.setDataHandler(new DataHandler(source));
          messageBodyPart.setFileName(thisAttachment.getTitle());
          multipart.addBodyPart(messageBodyPart);
        }
      }

      message.setSentDate(new Date());

      String mailSendList = "";
      String mailFailedList = "";

      //End of Build The JavaMail message

      // We must have to send seperate message to individual.
      // We must have to keep track of message sent to list and failed while send message to particular individual.
      if (toList != null) {
        Iterator toIterator = toList.iterator();
        int count = 0;
        while (toIterator.hasNext()) {
          String toAddress = (String) toIterator.next();
          message.setRecipients(Message.RecipientType.TO, toAddress);
          try{
            messageSent = this.sendSimpleMessage(message, smtpServer, username, password, smtpPort, connectToPopFirst, smtpAuthenticationRequired, serverType, serverAddress);
          } catch(SendFailedException sfe) {
            // we will catch the invalid Address and by this way we will know that recipient will not receive the mail and we add individual in the failing list.
            Address[] invalidAddress = sfe.getInvalidAddresses();
View Full Code Here

Examples of javax.mail.internet.MimeMessage

        WorkItemManager manager = new DefaultWorkItemManager(null);
        handler.executeWorkItem( workItem, manager );
       
        assertEquals( 1, wiser.getMessages().size() );
       
        MimeMessage msg = (( WiserMessage  ) wiser.getMessages().get( 0 )).getMimeMessage();
        assertEquals( workItem.getParameter( "Body" ), msg.getContent() );
        assertEquals( workItem.getParameter( "Subject" ), msg.getSubject() );
        assertEquals( workItem.getParameter( "From" ), ((InternetAddress)msg.getFrom()[0]).getAddress() );
        assertEquals( workItem.getParameter( "Reply-To" ), ((InternetAddress)msg.getReplyTo()[0]).getAddress() );
        assertEquals( workItem.getParameter( "To" ), ((InternetAddress)msg.getRecipients( RecipientType.TO )[0]).getAddress() );
        assertNull( msg.getRecipients( RecipientType.CC ) );
        assertNull( msg.getRecipients( RecipientType.BCC ) );
    }
View Full Code Here

Examples of javax.mail.internet.MimeMessage

        assertTrue( list.contains("person1@domain.com"));
        assertTrue( list.contains("person2@domain.com"));
        assertTrue( list.contains("person3@domain.com"));
       
       
        MimeMessage msg = (( WiserMessage  ) wiser.getMessages().get( 0 )).getMimeMessage();
        assertEquals( workItem.getParameter( "From" ), wiser.getMessages().get( 0 ).getEnvelopeSender() );
        assertEquals( workItem.getParameter( "Body" ), msg.getContent() );
        assertEquals( workItem.getParameter( "Subject" ), msg.getSubject() );
        assertEquals( workItem.getParameter( "From" ), ((InternetAddress)msg.getFrom()[0]).getAddress() );
        assertEquals( workItem.getParameter( "Reply-To" ), ((InternetAddress)msg.getReplyTo()[0]).getAddress() );
        assertEquals( workItem.getParameter( "To" ), ((InternetAddress)msg.getRecipients( RecipientType.TO )[0]).getAddress() );
        assertEquals( workItem.getParameter( "Cc" ),((InternetAddress)msg.getRecipients( RecipientType.CC )[0]).getAddress()  );
       
        msg = (( WiserMessage  ) wiser.getMessages().get( 1 )).getMimeMessage();
        assertEquals( workItem.getParameter( "From" ), wiser.getMessages().get( 1 ).getEnvelopeSender() );
        assertEquals( workItem.getParameter( "Body" ), msg.getContent() );
        assertEquals( workItem.getParameter( "Subject" ), msg.getSubject() );
        assertEquals( workItem.getParameter( "From" ), ((InternetAddress)msg.getFrom()[0]).getAddress() );
        assertEquals( workItem.getParameter( "Reply-To" ), ((InternetAddress)msg.getReplyTo()[0]).getAddress() );
        assertEquals( workItem.getParameter( "To" ), ((InternetAddress)msg.getRecipients( RecipientType.TO )[0]).getAddress() );
        assertEquals( workItem.getParameter( "Cc" ),((InternetAddress)msg.getRecipients( RecipientType.CC )[0]).getAddress()  );
       
        msg = (( WiserMessage  ) wiser.getMessages().get( 2 )).getMimeMessage();
        assertEquals( workItem.getParameter( "From" ), wiser.getMessages().get( 2 ).getEnvelopeSender() );
        assertEquals( workItem.getParameter( "Body" ), msg.getContent() );
        assertEquals( workItem.getParameter( "Subject" ), msg.getSubject() );
        assertEquals( workItem.getParameter( "From" ), ((InternetAddress)msg.getFrom()[0]).getAddress() );
        assertEquals( workItem.getParameter( "Reply-To" ), ((InternetAddress)msg.getReplyTo()[0]).getAddress() );
        assertEquals( workItem.getParameter( "To" ), ((InternetAddress)msg.getRecipients( RecipientType.TO )[0]).getAddress() );
        assertEquals( workItem.getParameter( "Cc" ),((InternetAddress)msg.getRecipients( RecipientType.CC )[0]).getAddress()  );       
    }   
View Full Code Here

Examples of javax.mail.internet.MimeMessage

        assertTrue( list.contains("person4@domain.com"));
        assertTrue( list.contains("person5@domain.com"));
        assertTrue( list.contains("person6@domain.com"));
               
        // We know from previous test that all MimeMessages will be identical
        MimeMessage msg = (( WiserMessage  ) wiser.getMessages().get( 0 )).getMimeMessage();
        assertEquals( workItem.getParameter( "From" ), wiser.getMessages().get( 0 ).getEnvelopeSender() );
        assertEquals( workItem.getParameter( "Body" ), msg.getContent() );
        assertEquals( workItem.getParameter( "Subject" ), msg.getSubject() );
        assertEquals( workItem.getParameter( "From" ), ((InternetAddress)msg.getFrom()[0]).getAddress() );
        assertEquals( workItem.getParameter( "Reply-To" ), ((InternetAddress)msg.getReplyTo()[0]).getAddress() );
        assertEquals( workItem.getParameter( "To" ), ((InternetAddress)msg.getRecipients( RecipientType.TO )[0]).getAddress() + "; " + ((InternetAddress)msg.getRecipients( RecipientType.TO )[1]).getAddress() );
        assertEquals( workItem.getParameter( "Cc" ),((InternetAddress)msg.getRecipients( RecipientType.CC )[0]).getAddress()  + "; " ((InternetAddress)msg.getRecipients( RecipientType.CC )[1]).getAddress() );      
    }   
View Full Code Here

Examples of javax.mail.internet.MimeMessage

       
        assertTrue( list.contains("tony@domain.com"));
        assertTrue( list.contains("darth@domain.com"));
       
       
        MimeMessage msg = (( WiserMessage  ) getWiser().getMessages().get( 0 )).getMimeMessage();
        assertEquals( "My Body", msg.getContent() );
        assertEquals( "My Subject", msg.getSubject() );
        assertEquals( "from@domain.com", ((InternetAddress)msg.getFrom()[0]).getAddress() );
        assertEquals( "replyTo@domain.com", ((InternetAddress)msg.getReplyTo()[0]).getAddress() );
        assertEquals( "tony@domain.com", ((InternetAddress)msg.getRecipients( RecipientType.TO )[0]).getAddress() );
        assertEquals( "darth@domain.com", ((InternetAddress)msg.getRecipients( RecipientType.TO )[1]).getAddress() );       
    }
View Full Code Here

Examples of javax.mail.internet.MimeMessage

            }              
            Session session = Session.getInstance( props, null );
            session.setDebug( debug );
           
            // construct the message
            Message msg = new MimeMessage( session );
           
            msg.setFrom( new InternetAddress( from ) );
            msg.setReplyTo( new InternetAddress[] {  new InternetAddress( replyTo ) }  );
           
            for ( Recipient recipient : message.getRecipients().getRecipients() ) {
                RecipientType type = null;
                if ( "To".equals( recipient.getType() ) ) {
                    type = Message.RecipientType.TO;
                } else if ( "Cc".equals( recipient.getType() ) ) {
                    type = Message.RecipientType.CC;
                } else if ( "Bcc".equals( recipient.getType() ) ) {
                    type = Message.RecipientType.BCC;
                } else {
                    throw new RuntimeException( "Unable to determine recipient type" );
                }

                msg.addRecipients( type, InternetAddress.parse( recipient.getEmail(), false ) );
            }
            msg.setSubject( subject );
            collect( message.getBody(), msg );
            msg.setHeader( "X-Mailer", mailer );
            msg.setSentDate( new Date() );
           
            // send the thing off
          Transport t = (Transport)session.getTransport("smtp");
          try {
            t.connect(mailhost, username, password);
          t.sendMessage(msg, msg.getAllRecipients());
          } catch (Exception e) {
            throw new RuntimeException( "Connection failure", e );
           } finally {
            t.close();
          }
View Full Code Here

Examples of javax.mail.internet.MimeMessage

    {
        Debug.print( "MailerMDB.send(" + to + "," + subject + ",...)" );

        InitialContext     ic = new InitialContext(  );
        Session            session = ( Session ) ic.lookup( JNDINames.MAIL_SESSION );
        javax.mail.Message msg = new MimeMessage( session );

        msg.setFrom(  );
        msg.setRecipients( javax.mail.Message.RecipientType.TO, InternetAddress.parse( to, false ) );
        msg.setSubject( subject );

        msg.setDataHandler( new DataHandler( body, "text/html" ) );
        msg.setHeader( "X-Mailer", "JavaMailer" );
        msg.setSentDate( new Date(  ) );

        Transport.send( msg );
    }
View Full Code Here

Examples of javax.mail.internet.MimeMessage

                   AddressException,
                   MessagingException
    {
        InitialContext     ic = new InitialContext(  );
        Session            session = ( Session ) ic.lookup( MAIL_SESSION );
        javax.mail.Message msg = new MimeMessage( session );

        msg.setFrom(  );
        msg.setRecipients( javax.mail.Message.RecipientType.TO, InternetAddress.parse( to, false ) );
        msg.setSubject( subject );

        msg.setDataHandler( new DataHandler( body, "text/plain" ) );
        msg.setHeader( "X-Mailer", "JavaMailer" );
        msg.setSentDate( new Date(  ) );

        Transport.send( msg );
    }
View Full Code Here

Examples of javax.mail.internet.MimeMessage

   
    Session session = Session.getInstance(props, auth);

    session.setDebug(true);

    MimeMessage msg = new MimeMessage(session);

    try
    {
      if (from != null)
      {
        msg.setFrom(getAddress(from));
      }
      else
      {
        msg.setFrom();
      }

      msg.setRecipients(Message.RecipientType.TO, parseAddress(to));

      if (subject != null)
      {
        msg.setSubject(subject);
      }

      MimeBodyPart part = new MimeBodyPart();

      StringBuffer sbuf = new StringBuffer();
     
      Formatter f = getFormatter();
     
      String head = f.getHead(this);

      if (head != null)
      {
        sbuf.append(head);
      }

      int len = cb.length();

      for (int i = 0; i < len; i++)
      {
        LogRecord record = cb.get();
        sbuf.append(getFormatter().format(record));
      }

      String tail = getFormatter().getTail(this);

      if (tail != null)
      {
        sbuf.append(tail);
      }

      part.setContent(sbuf.toString(), getEmailContentType());

      Multipart mp = new MimeMultipart();
      mp.addBodyPart(part);
      msg.setContent(mp);
     
      msg.setSentDate(new Date());
     
      Transport.send(msg);
    }
    catch (Exception ex)
    {
View Full Code Here

Examples of javax.mail.internet.MimeMessage

        assertEquals( "steve@domain.com", getWiser().getMessages().get( 1 ).getEnvelopeReceiver() );

        String subject = "Summary\n-------\n\nThis is my task subject\n\n";
        String description = "Description\n-----------\n\nThis is my task description";

        MimeMessage msg = ((WiserMessage) getWiser().getMessages().get( 0 )).getMimeMessage();
        assertEqualsIgnoreWhitespace( "multipart/alternative;boundary=\"----=_Part_",
                                      msg.getContentType(),
                                      0,
                                      47 );
        assertEquals( "tony@domain.com",
                      ((InternetAddress) msg.getFrom()[0]).getAddress() );
        assertEquals( "tony@domain.com",
                      ((InternetAddress) msg.getReplyTo()[0]).getAddress() );
        assertEquals( "steve@domain.com",
                      ((InternetAddress) msg.getRecipients( RecipientType.TO )[0]).getAddress() );
        assertEquals( "Task Assignment Start Event: This is my task name",
                      msg.getSubject() );
       
        MimeMultipart multiPart = (MimeMultipart) msg.getContent();
                       
        BodyPart messageBodyPart = multiPart.getBodyPart( 0 );
        assertEquals( "text/plain; charset=UTF8;", messageBodyPart.getDataHandler().getContentType() );
        String content = new String( getBytes( messageBodyPart.getDataHandler().getInputStream() ) );       
        assertEqualsIgnoreWhitespace( subject + description, content );
       
        messageBodyPart = multiPart.getBodyPart( 1 );
        assertEquals( "text/calendar; charset=UTF8; name=ical-Start-1.ics", messageBodyPart.getDataHandler().getContentType() );
        content = new String( getBytes( messageBodyPart.getDataHandler().getInputStream() ) );
        assertEqualsIgnoreWhitespace( "BEGIN:VCALENDARPRODID:-//iCal4j 1.0//ENCALSCALE:GREGORIANVERSION:2.0METHOD:REQUESTBEGIN:VEVENTDTSTART;TZID=UTC:", content.substring( 0, 123) );          
        assertEqualsIgnoreWhitespace( "SUMMARY:\"Task Start : This is my task subject\"DESCRIPTION:\"This is my task description\"PRIORITY:55END:VEVENTEND:VCALENDAR", content.substring( content.length()-131, content.length()) );
       
       
        msg = ((WiserMessage) getWiser().getMessages().get( 1 )).getMimeMessage();
        assertEqualsIgnoreWhitespace( "multipart/alternative;boundary=\"----=_Part_",
                                      msg.getContentType(),
                                      0,
                                      47 );
        assertEquals( "tony@domain.com",
                      ((InternetAddress) msg.getFrom()[0]).getAddress() );
        assertEquals( "tony@domain.com",
                      ((InternetAddress) msg.getReplyTo()[0]).getAddress() );
        assertEquals( "steve@domain.com",
                      ((InternetAddress) msg.getRecipients( RecipientType.TO )[0]).getAddress() );
        assertEquals( "Task Assignment End Event: This is my task name",
                      msg.getSubject() );
       
        multiPart = (MimeMultipart) msg.getContent();
                       
        messageBodyPart = multiPart.getBodyPart( 0 );
        assertEquals( "text/plain; charset=UTF8;", messageBodyPart.getDataHandler().getContentType() );
        content = new String( getBytes( messageBodyPart.getDataHandler().getInputStream() ) );       
        assertEqualsIgnoreWhitespace( subject + description, content );
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.