Package javax.mail.internet

Examples of javax.mail.internet.MimeMessage


        assertEquals( "steve@domain.com", getWiser().getMessages().get( 0 ).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 End 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 );
View Full Code Here


   */
  private int applyRule(ArrayList rv, Message message, int accountID, int userID) throws Exception
  {
    CVDal cvdl = new CVDal(dataSource);

    MimeMessage mimeMessage = (MimeMessage)message;

    int messageID = 0;

    try
    {
      Address from[] = message.getFrom();

      // get the message's date and time
      long messageTime = (message.getSentDate()).getTime();
      java.sql.Timestamp messageDate = (java.sql.Timestamp)new java.sql.Timestamp(messageTime);

      cvdl.setSqlQuery("INSERT INTO emailmessage(MessageDate, MailFrom, Subject, Body, accountID , Priority, Headers, owner,Size) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");

      String mesgFrom = from[0].toString();
      mesgFrom = mesgFrom.replaceAll("\""," ");
      mesgFrom = mesgFrom.replaceAll("'"," ");
      mesgFrom = mesgFrom.trim();

      String[] DateHeader = message.getHeader("Date");
      Date receiveDate = new Date(DateHeader[0]);
      SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
      String receivedDate = formatter.format(receiveDate);

      if (receivedDate != null && !(receivedDate.equals("")))
      {
        cvdl.setString(1, receivedDate);
      }else{
        cvdl.setString(1, "CONCAT(CURRENT_DATE, CURRENT_TIME)");
      }

      cvdl.setString(2, mesgFrom);
      cvdl.setString(3, message.getSubject());

      String MsgBody = null;
      StringBuffer msgBody = new StringBuffer();


      Object o = message.getContent();
      if (o instanceof String)
      {
        msgBody.append((String) o);
      }else if(o instanceof Multipart){
        Multipart mp = (Multipart)o;
        int countMultipart = mp.getCount();

        for (int j = 0; j < countMultipart; j++)
        {
          // Part are numbered starting at 0
          BodyPart b = mp.getBodyPart(j);
          String mimeType2 = b.getContentType();
          String disposition2 = b.getDisposition();

          Object o2 = b.getContent();

          if (o2 instanceof String)
          {
            if (mimeType2 != null &&
                (mimeType2.toLowerCase().indexOf("text/plain") != -1 || mimeType2.toLowerCase().indexOf("text/html") != -1) &&
                (disposition2 == null ||(disposition2!=null && !disposition2.equals(Part.ATTACHMENT))))
            {
              if (mimeType2.toLowerCase().indexOf("text/html")>=0)
              {
                // HTML message body
                msgBody.append(b.getContent().toString());
              }else{
                // plain text part
                msgBody.append(b.getContent().toString());
              }
              if (! msgBody.toString().equals(""))
              {
                break;
              }
            }
          }else if(o2 instanceof Multipart){
            //"**This BodyPart is a nested Multipart.  ");
            Multipart mp2 = (Multipart)o2;
            int countMultipart1 = mp2.getCount();

            for (int k = 0; k < countMultipart1; k++)
            {
              // Part are numbered starting at 0
              BodyPart b2 = mp2.getBodyPart(k);
              String mimeType3 = b2.getContentType();
              String disposition3 = b2.getDisposition();

              Object o3 = b2.getContent();

              if (o3 instanceof String)
              {
                if (mimeType3 != null &&
                    (mimeType3.toLowerCase().indexOf("text/plain") != -1 || mimeType3.toLowerCase().indexOf("text/html") != -1) &&
                    (disposition3==null ||( disposition3!=null && !disposition3.equals(Part.ATTACHMENT))))
                {
                  if (mimeType3.toLowerCase().indexOf("text/html") >= 0)
                  {
                    // HTML message body
                    msgBody.append(b2.getContent().toString());
                  }else{
                    // plain text part
                    msgBody.append(b2.getContent().toString());
                  }
                }

                if (! msgBody.toString().equals(""))
                {
                  break;
                }
              }   // end if (o3 instanceof String)
            }   // end for (int k = 0; k < countMultipart1; k++)
          }   // end if(o2 instanceof Multipart)
        }   // end for (int j = 0; j < countMultipart; j++)
      }   // end if (o instanceof String)

      MsgBody = msgBody.toString();

      if (MsgBody != null)
      {
        cvdl.setString(4,MsgBody);
      }else{
        cvdl.setString(4,"");
      }

      cvdl.setInt(5, accountID);

      // set priority field based on message X-Priority header.
      // if the X-Priority header is not set, or is unknown, then
      // set our database priority field to medium.
      if (mimeMessage.getHeader("X-Priority", "") != null)
      {
        if ((mimeMessage.getHeader("X-Priority", "")).equals("1"))
        {
          cvdl.setString(6, "HIGH");
        }else if ((mimeMessage.getHeader("X-Priority", "")).equals("5")){
          cvdl.setString(6, "LOW");
        }else{
          cvdl.setString(6, "MEDIUM");
        }
      }else{
        cvdl.setString(6, "MEDIUM");
      }

      // added for taking the header also
      Enumeration em = mimeMessage.getAllHeaderLines();

      StringBuffer ah = new StringBuffer();

      // loop through all the headers
      while (em.hasMoreElements())
      {
        String ch = (String)em.nextElement();
        ah.append(ch);
      }   // end while (em.hasMoreElements())

      if (ah.length() > 0)
      {
        cvdl.setString(7, ah.toString());
      }else{
        cvdl.setString(7, null);
      }

      cvdl.setInt(8, userID);

      long messageSize = mimeMessage.getSize();
      if (messageSize > 1024)
      {
        messageSize = messageSize / 1024;
      }else{
        messageSize = 1;
View Full Code Here

      Session session = Session.getDefaultInstance(props, null);
     
      byte b[] = (byte[])messageMap.get("inputstream");
      InputStream fis = new ByteArrayInputStream( b );
     
      MimeMessage messagefile = new MimeMessage( session , fis );
      Message mm [] = new Message[1];
      mm[0] = messagefile;
      messageMap.put( "message" , mm );
     
      InitialContext ic      = CVUtility.getInitialContext();
View Full Code Here

        return(false);
      }

      Session session = Session.getDefaultInstance(props, null);

      MimeMessage message = new MimeMessage(session);

      ArrayList to = mailmessage.getTo();
      String arrayto[] = new String[to.size()];
      for (int i=0; i<arrayto.length; i++)
      {
        MailAddress ma = new MailAddress((String)to.get(i));
        arrayto[i] = ma.getAddress();
      }

      long date = (new java.util.Date()).getTime();
      java.sql.Timestamp messagedate = new java.sql.Timestamp(date);
      String subject = mailmessage.getSubject();
      String body = mailmessage.getBody();
      String from = mailmessage.getMailFrom();
      String replyTo = mailmessage.getReplyTo();

      if (replyTo != null && replyTo.length() >0)
      {
        InternetAddress[] ia = new InternetAddress[1];
        ia[0] = new InternetAddress(replyTo);
        message.setReplyTo(ia);
      }

      if (arrayto.length != 0)
      {
        for (int i = 0; i < arrayto.length; i++)
        {
          message.addRecipient(Message.RecipientType.TO, new InternetAddress(arrayto[i]));
        }
      }

      message.setFrom(new InternetAddress(from));
      message.setSubject(subject);
      BodyPart messageBodyPart        = new MimeBodyPart();
      String messageContext = "text/plain";
      messageBodyPart.setContent(body, messageContext);
      Multipart multipart             = new MimeMultipart();
      multipart.addBodyPart(messageBodyPart);
      messageBodyPart = new MimeBodyPart();
      message.setContent(multipart);
      Transport.send(message);
    }catch(Exception e){
      System.out.println("[Exception]: SendMailEJB.sendMailMessage:");
      e.printStackTrace();
    } //end of catch block (Exception)
View Full Code Here

        props.put("mail.smtp.host", mailmessage.getSmtpserver());
      }else{
        return(false);
      }
      Session session = Session.getDefaultInstance(props, null);
      MimeMessage message = new MimeMessage(session);

      int accountid = mailmessage.getAccountID();

      ArrayList bcc = mailmessage.getBcc();
      //System.out.println("bcc"+bcc);
      //System.out.println("bcc.size()"+bcc.size());
      String arraybcc[] = new String[bcc.size()];

      for (int i = 0; i < arraybcc.length; i++)
      {
        MailAddress ma  = (MailAddress) bcc.get(i);
        arraybcc[i] = ma.getAddress();
        //System.out.println("arraybcc["+i+"]"+arraybcc[i]);
      }

      ArrayList cc = mailmessage.getCc();
      //System.out.println("cc"+cc);
      //System.out.println("cc.size()"+cc.size());
      String arraycc[] = new String[cc.size()];
      for (int i = 0; i < arraycc.length; i++)
      {
        MailAddress ma  = (MailAddress) cc.get(i);
        arraycc[i] = ma.getAddress();
        //System.out.println("arraycc["+i+"]"+arraycc[i]);
      }

      ArrayList to = mailmessage.getTo();
     // System.out.println("to"+to);
      //System.out.println("to.size()"+to.size());
      String arrayto[] = new String[to.size()];
      for (int i = 0; i < arrayto.length; i++)
      {
        MailAddress ma = (MailAddress)to.get(i);
        arrayto[i] = ma.getAddress();
        //System.out.println("arrayto["+i+"]"+arrayto[i]);
      }

      long l = (new java.util.Date()).getTime();
      java.sql.Timestamp messagedate = (java.sql.Timestamp) new java.sql.Timestamp(l);
      String subject = mailmessage.getSubject();
      String body = mailmessage.getBody();
      String from = mailmessage.getMailFrom();
      String replyTo = mailmessage.getReplyTo();

      if (replyTo != null && replyTo.length() >0)
      {
        InternetAddress[] ia = new InternetAddress[1];
        ia[0] = new InternetAddress(replyTo);
        message.setReplyTo(ia);
      }

      // extra headers that may be used for some action in to CV
      // eg - Activity send email, Adding individual throough email
      HashMap hhm = mailmessage.getHeadersHM();
      String allHeaderString = null;
      if (hhm != null)
      {
        allHeaderString = new String();
        Iterator it  = hhm.keySet().iterator();
        while (it.hasNext())
        {
          String hmKey    = (String) it.next();
          String hmValue  = (String) hhm.get(hmKey);
          allHeaderString = allHeaderString.concat(hmKey + "=" + hmValue + "::");
         // message.addHeader(hmKey, hmValue);
        }
      }

      //cvdl = new CVDal(dataSource);
      cvdl.setSql("email.savedraft1");

      cvdl.setString(1, from);
      cvdl.setString(2, subject);
      cvdl.setString(3, replyTo);
      cvdl.setString(4, body);
      cvdl.setString(5, allHeaderString);
      cvdl.setInt(6, accountid);
      cvdl.setInt(7, userId);
      cvdl.executeUpdate();

      messageid = cvdl.getAutoGeneratedKey();
      message.addHeader("X-centraviewID", "" + messageid);

      cvdl.clearParameters();

      cvdl.setSql("email.savedraft4");
      cvdl.setInt(1, messageid);
      cvdl.setString(2, "NO");
      cvdl.setInt(3, accountid);
      cvdl.executeUpdate();
      cvdl.clearParameters();

      if (arrayto.length != 0)
      {
        for (int i = 0; i < arrayto.length; i++)
        {
          cvdl.setSql("email.savedraft3");
          cvdl.setInt(1, messageid);
          message.addRecipient(Message.RecipientType.TO, new InternetAddress(arrayto[i]));
          cvdl.setString(2, arrayto[i]);
          cvdl.setString(3, "TO");
          cvdl.setString(4, "NO");
          cvdl.executeUpdate();
          cvdl.clearParameters();

        }
      }

      if (arraybcc.length != 0)
      {

        for (int i = 0; i < arraybcc.length; i++)
        {
          cvdl.setSql("email.savedraft3");
          cvdl.setInt(1, messageid);
          cvdl.setString(2, arraybcc[i]);
          message.addRecipient(Message.RecipientType.BCC, new InternetAddress(arraybcc[i]));
          cvdl.setString(3, "BCC");
          cvdl.setString(4, "NO");
          cvdl.executeUpdate();
          cvdl.clearParameters();
        }
      }

      if (arraycc.length != 0)
      {

        for (int i = 0; i < arraycc.length; i++)
        {
//System.out.println("arraycc[i]"+arraycc[i]        );
          cvdl.setSql("email.savedraft3");
          cvdl.setInt(1, messageid);
          cvdl.setString(2, arraycc[i]);
          message.addRecipient(Message.RecipientType.CC, new InternetAddress(arraycc[i]));
          cvdl.setString(3, "CC");
          cvdl.setString(4, "NO");
          cvdl.executeUpdate();
          cvdl.clearParameters();
        }
      }

      message.setFrom(new InternetAddress(from));

      message.setSubject(subject);
      BodyPart messageBodyPart        = new MimeBodyPart();

      //Added by Ryan Grier <ryan@centraview.com>
      //Need to send email as plain text if
      //it contains no html content.
      String messageContext = "text/plain";

//   Modified by Deepa
//  User Preferences value will decide content type of the email. ie. Plain text or Html text

  InitialContext ic = CVUtility.getInitialContext();
  PreferenceLocalHome home  = (PreferenceLocalHome)ic.lookup("local/Preference");
  PreferenceLocal localPref =  home.create();
  localPref.setDataSource(this.dataSource);

  UserPrefererences userPref = localPref.getUserPreferences(userId);


  if(userPref != null)
  if (userPref.getContentType() != null && userPref.getContentType().equals("PLAIN"))
  {
    messageContext = "text/plain";
  }
  else if ( userPref.getContentType() != null && userPref.getContentType().equals("HTML"))
  {
    messageContext = "text/html";
  }

  if(mailmessage.getContentType() != null && mailmessage.getContentType().equals("HTML") ){
    messageContext = "text/html";
  }

/*    if ((body.indexOf("<") > -1) && (body.indexOf(">") > -1))
      {
        messageContext = "text/html";
      } //end of if statement ((body.indexOf("<") > -1) && (body.indexOf(">") > -1))
      else if ((body.indexOf("&lt;") > -1) && (body.indexOf("&gt;") > -1))
      {
        messageContext = "text/html";
      } //end of if statement ((body.indexOf("&lt;") > -1) && (body.indexOf("&gt;") > -1))
*/

      messageBodyPart.setContent(body, messageContext);

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

      messageBodyPart = new MimeBodyPart();
      HashMap attchmentids            = mailmessage.getAttachFileIDs();

      if ((attchmentids != null) && (attchmentids.size() != 0))
      {

        Set col       = attchmentids.keySet();
        Iterator itt  = col.iterator();
        int i         = 0;
        while (itt.hasNext())
        {
          String fileid        = (String) itt.next();
          String name          = (String) attchmentids.get(fileid);
          cvdl.setSql("email.savedraftattchment");
          cvdl.setInt(1, messageid);
          cvdl.setString(2, name);
          cvdl.setInt(3, Integer.parseInt(fileid));
          cvdl.executeUpdate();
          cvdl.clearParameters();
          i++;

          CvFileFacade cvfile  = new CvFileFacade();
          CvFileVO cvfilevo    = cvfile.getEmailAttachment(userId, Integer.parseInt(fileid), this.dataSource);

          String path          = cvfilevo.getPhysicalFolderVO().getFullPath(null, true) + cvfilevo.getName();

          DataSource source    = new FileDataSource(path);
          messageBodyPart.setDataHandler(new DataHandler(source));
          messageBodyPart.setFileName(name);
          multipart.addBodyPart(messageBodyPart);

        } //end of while loop(itt.hasNext())
      } //end of if statement ((attchmentids != null) && (attchmentids.size() != 0))
      // delete mail from  drafts
      if (mailmessage.getMessageID() != 0)
      {
        String mailIdList[]          = {"" + mailmessage.getMessageID()};

        InitialContext icPref          = CVUtility.getInitialContext();
        EmailManageLocalHome emailManageHome  = (EmailManageLocalHome) icPref.lookup("local/EmailManage");
        EmailManageLocal local     = emailManageHome.create();
        local.setDataSource(this.dataSource);

        int result                 = local.emailDeleteTrash(mailmessage.getFolder(), mailIdList);
      } //end of if statement (mailmessage.getMessageID() != 0)

      message.setContent(multipart);
      try
      {
        Transport.send(message);
      }catch(SendFailedException sendFailed){
        System.out.println("\n\n\nSendFailedException occurred!!!\n");
View Full Code Here

          {
            // TODO HARDCODED SMTP HOST!!
            props.put("mail.smtp.host", "minsksrv");
          }
          Session session                 = Session.getDefaultInstance(props, null);
          MimeMessage message             = new MimeMessage(session);

          ArrayList to                    = mailmessage.getTo();
          String arrayto[]                  = new String[to.size()];
          for (int i = 0; i < arrayto.length; i++)
          {
            MailAddress ma  = (MailAddress) to.get(i);
            arrayto[i] = ma.getAddress();
          }

          long l                          = (new java.util.Date()).getTime();
          java.sql.Timestamp messagedate  = (java.sql.Timestamp) new java.sql.Timestamp(l);
          String subject                  = mailmessage.getSubject();
          String body                     = mailmessage.getBody();
          String from                     = mailmessage.getMailFrom();
              String replyTo                     = mailmessage.getReplyTo();

              if(replyTo != null && replyTo.length() >0)
              {
                    InternetAddress[] ia = new InternetAddress[1];
                    ia[0] = new InternetAddress(replyTo);
                    message.setReplyTo(ia);
              }

          //  extra headers that may be used for some action in to CV
          // eg - Activity send email, Adding individual throough email
          HashMap hhm                     = mailmessage.getHeadersHM();
          String allHeaderString          = null;
          if (hhm != null)
          {
            allHeaderString = new String();
            Iterator it  = hhm.keySet().iterator();
            while (it.hasNext())
            {
              String hmKey    = (String) it.next();
              String hmValue  = (String) hhm.get(hmKey);
              allHeaderString = allHeaderString.concat(hmKey + "=" + hmValue + "::");
   //           message.addHeader(hmKey, hmValue);

            }
          }
          message.addHeader("X-centraviewID", "" + messageid);

          if (arrayto.length != 0)
          {
            for (int i = 0; i < arrayto.length; i++)
            {
              message.addRecipient(Message.RecipientType.TO, new InternetAddress(arrayto[i]));
            }
          }

          message.setFrom(new InternetAddress(from));
          message.setSubject(subject);
          BodyPart messageBodyPart        = new MimeBodyPart();
          String messageContext = "text/plain";
          messageBodyPart.setContent(body, messageContext);
          Multipart multipart             = new MimeMultipart();
          multipart.addBodyPart(messageBodyPart);
          messageBodyPart = new MimeBodyPart();
          message.setContent(multipart);
          Transport.send(message);
        }//end of try block
        catch (Exception e)
        {
          System.out.println("[Exception]: SendMailEJB.sendMailMessage:");
View Full Code Here

//      final Transport transport = session.getTransport();
//      transport.connect();
      for (int i = 0; i < burstingData.getRowCount(); i++)
      {
        final DataRow parameterDataRow = createReportParameterDataRow(burstingData, i);
        final MimeMessage message = createReport(definition, session, parameterDataRow);

        parameterContext.setParameterValues(parameterDataRow);

        final MailHeader[] headers = definition.getHeaders();
        for (int j = 0; j < headers.length; j++)
        {
          final MailHeader header = headers[j];
          message.addHeader(header.getName(), header.getValue(parameterContext));
        }

        processRecipients(definition, message, dataFactory, parameterDataRow);

//        transport.sendMessage(message, message.getAllRecipients());
View Full Code Here

    Properties properties = System.getProperties();
    properties.put("mail.smtp.host", host);
    javax.mail.Session session = Session.getDefaultInstance(properties,null);

    MimeMessage msg = new MimeMessage(session);
    msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));

    if (cc != null && cc.length() > 0) {
      msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
    }
    msg.setSentDate(new Date());
    if (subject != null) {
      msg.setSubject(subject, charset);
    }
    msg.setFrom(new InternetAddress(from));

    if (attachments == null || attachments.size() == 0) {
      msg.setText(body, charset);

    } else {
      MimeMultipart mimeMultipart = new MimeMultipart();
      MimeBodyPart mimeBodyPart = new MimeBodyPart();
      if (body != null) {
          mimeBodyPart.setText(body);
          mimeMultipart.addBodyPart(mimeBodyPart);
      }

      for (int i=0; i<attachments.size(); i++) {
        Object obj = attachments.elementAt(i);
        if (obj instanceof File) {
          File file = (File)obj;
          if (file.isFile() && file.exists() && file.length() > 0) {
            mimeBodyPart = new MimeBodyPart();
            FileDataSource fds = new FileDataSource(file);
            MimetypesFileTypeMap map = new MimetypesFileTypeMap();
            map.addMimeTypes(getTypeEntry(file));
            fds.setFileTypeMap(map);
            mimeBodyPart.setDataHandler(new DataHandler(fds));
            mimeBodyPart.setFileName(file.getName());
            mimeBodyPart.setDisposition(getDisposition(file));
            mimeMultipart.addBodyPart(mimeBodyPart);
          }
        }
      }
      msg.setContent(mimeMultipart);
    }
    Transport.send(msg);
  }
View Full Code Here

        throws NamingException,
                   MessagingException
    {
        InitialContext ic = new InitialContext(  );
        Session        session = ( Session ) ic.lookup( _jndiSession );
        Message        msg = new MimeMessage( session );

        if ( from != null )
        {
            msg.setFrom( from );
        }
        else
        {
            msg.setFrom();
        }
        msg.setRecipients( javax.mail.Message.RecipientType.TO, to );
        msg.setSubject( subject );

        msg.setDataHandler( new DataHandler( body, mimeType ) );

        //msg.setHeader( "X-Mailer", "JavaMailer" );
        msg.setSentDate( new Date(  ) );

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

    try
    {
      /*
       * Creating the message
       */
      MimeMessage msg = new MimeMessage(session);
      if (mUser != null)
      {
        msg.setHeader("AUTH", "PLAIN");
      }
      msg.setFrom(fromAddress);
      msg.setRecipients(Message.RecipientType.TO, toAddresses);
      msg.setSubject(subject);
      msg.setSentDate(new Date());
      msg.setText(callData, getEncoding());

      /*
       * Sending the message
       */
      Transport.send(msg);
 
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.