Package javax.mail.internet

Examples of javax.mail.internet.MimeMessage


  /** */
  private MimeMessage createMessage(Session session, String from, String to, String subject, String body)
    throws MessagingException, IOException
  {
    MimeMessage msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(from));
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    msg.setText(body);
    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
    return msg;
  }
View Full Code Here


  {
    Properties mailProps = this.getMailProperties(port);
    Session session = Session.getInstance(mailProps, null);
    // session.setDebug(true);

    MimeMessage msg = this.createMessageWithCharset(session, from, to, subject, body, charset);
    Transport.send(msg);
  }
View Full Code Here

  /** */
  private MimeMessage createMessageWithCharset(Session session, String from, String to, String subject, String body, String charset)
    throws MessagingException
  {
    MimeMessage msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(from));
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    if (charset != null)
    {
      msg.setText(body, charset);
      msg.setHeader("Content-Transfer-Encoding", "7bit");
    }
    else
      msg.setText(body);

    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
    return msg;
  }
View Full Code Here

  }

  /** */
  public void testReceivedHeader() throws Exception
  {
    MimeMessage message = new MimeMessage(this.session);
    message.addRecipient(Message.RecipientType.TO, new InternetAddress("anyone@anywhere.com"));
    message.setFrom(new InternetAddress("someone@somewhereelse.com"));
    message.setSubject("barf");
    message.setText("body");

    Transport.send(message);

    assertEquals(1, this.wiser.getMessages().size());

View Full Code Here

  }

  /** */
  public void testMultipleRecipients() throws Exception
  {
    MimeMessage message = new MimeMessage(this.session);
    message.addRecipient(Message.RecipientType.TO, new InternetAddress("anyone@anywhere.com"));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress("anyone2@anywhere.com"));
    message.setFrom(new InternetAddress("someone@somewhereelse.com"));
    message.setSubject("barf");
    message.setText("body");

    Transport.send(message);

    assertEquals(2, this.wiser.getMessages().size());
  }
View Full Code Here

  }

  /** */
  public void testLargeMessage() throws Exception
  {
    MimeMessage message = new MimeMessage(this.session);
    message.addRecipient(Message.RecipientType.TO, new InternetAddress("anyone@anywhere.com"));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress("anyone2@anywhere.com"));
    message.setFrom(new InternetAddress("someone@somewhereelse.com"));
    message.setSubject("barf");
    message.setText("bodyalksdjflkasldfkasjldfkjalskdfjlaskjdflaksdjflkjasdlfkjl");

    Transport.send(message);

    assertEquals(2, this.wiser.getMessages().size());

View Full Code Here

  }

  /** */
  private void testEightBitMessage(String body, String charset) throws Exception
  {
    MimeMessage message = new MimeMessage(this.session);
    message.addRecipient(Message.RecipientType.TO, new InternetAddress("anyone@anywhere.com"));
    message.setFrom(new InternetAddress("someone@somewhereelse.com"));
    message.setSubject("hello");
    message.setText(body, charset);
    message.setHeader("Content-Transfer-Encoding", "8bit");

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

  public void testBinaryEightBitMessage() throws Exception
  {
    byte[] body = new byte[64];
    new Random().nextBytes(body);

    MimeMessage message = new MimeMessage(this.session);
    message.addRecipient(Message.RecipientType.TO, new InternetAddress("anyone@anywhere.com"));
    message.setFrom(new InternetAddress("someone@somewhereelse.com"));
    message.setSubject("hello");
    message.setHeader("Content-Transfer-Encoding", "8bit");
    message.setDataHandler(new DataHandler(new ByteArrayDataSource(body, "application/octet-stream")));

    Transport.send(message);

    InputStream in = this.wiser.getMessages().get(0).getMimeMessage().getInputStream();
    ByteArrayOutputStream tmp = new ByteArrayOutputStream();
View Full Code Here

    StringBuffer resultText = new StringBuffer();
    StringBuffer keyText = new StringBuffer();

    try {
      MimeMessage message = new MimeMessage(session, mimeInput);

      resultText.append("Subject: " + message.getSubject()).append("\n");
      if (message.getSentDate() != null) {
        resultText.append("Sent: " + simpleFormat.format(message.getSentDate())).append("\n");
      }

      if (message.getReceivedDate() != null) {
        resultText.append("Received: " + simpleFormat.format(message.getReceivedDate())).append("\n");
      }
      Address[] recipientsArray = null;
      try {
        recipientsArray = message.getAllRecipients();
      } catch (AddressException ae) {
        // KJB: an issue with getAllRecipients if To: contains a semi-colon rather than comma.
        recipientsArray = fixAddress(ae, message, "To");
      }
      if (recipientsArray != null && recipientsArray.length > 0) {
        String recipients = "";
        for (int i = 0; i < recipientsArray.length; i++) {
          recipients += recipientsArray[i].toString() + ", ";
          keyText.append(stripNoneWordChars(recipientsArray[i].toString()) + " ");
        }
        recipients = recipients.substring(0, recipients.length() - 2);
        resultText.append("Recipient(s): " + recipients).append("\n");
      }

      Address[] repliesArray = null;
      try {
        repliesArray = message.getReplyTo();
      } catch (AddressException ae) {
        repliesArray = fixAddress(ae, message, "Reply-to");
      }
      if (repliesArray != null && repliesArray.length > 0) {
        String replies = "";
        for (int i = 0; i < repliesArray.length; i++) {
          replies += repliesArray[i].toString() + ", ";
          keyText.append(stripNoneWordChars(repliesArray[i].toString()) + " ");
        }
        replies = replies.substring(0, replies.length() - 2);
        resultText.append("Reply to: " + replies).append("\n");
      }

      Address[] senderArray = null;
      try {
        senderArray = message.getFrom();
      } catch (AddressException ae) {
        senderArray = fixAddress(ae, message, "From");
      }
      if (senderArray != null && senderArray.length > 0) {
        String sender = "";
        for (int i = 0; i < senderArray.length; i++) {
          sender += senderArray[i].toString() + ", ";
          keyText.append(stripNoneWordChars(senderArray[i].toString()) + " ");
        }
        sender = sender.substring(0, sender.length() - 2);
        setTitle(message.getSubject() + " from " + sender);
        resultText.append("Sender: " + sender).append("\n");
      }

      resultText.append("Header key words: " + keyText.toString()).append("\n");
      resultText.append("-------------------------------------------------------------").append("\n");

      // multipart or not multipart
      if (message.getContent() instanceof Multipart) {
        //contentType = "multipart";
        Multipart mp = (Multipart) message.getContent();

        for (int i = 0; i < mp.getCount(); i++) {
          BodyPart bp = mp.getBodyPart(i);
          String disposition = bp.getDisposition();

          if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT)))) {
            attachments.add("attachment: " + bp.getFileName());
            if (bp.isMimeType("text/*")) {
              textParts.add((String) bp.getContent());
              mLog.debug("added txt from attachment: " + bp.getFileName() + " : " + bp.getContentType());
            }

          } else if (disposition == null || disposition.equals(Part.INLINE)) {

            // Plain Text oder HTML
            if (bp.isMimeType("text/*")) {
              textParts.add((String) bp.getContent());

            } else if (bp.isMimeType("multipart/*")) {
              // another bodypart container
              // we process only depth = 1 and not deeper and only text.
              Multipart mpInner = (Multipart) bp.getContent();

              for (int k = 0; k < mpInner.getCount(); k++) {
                BodyPart bpInner = mpInner.getBodyPart(k);

                if (bpInner != null && (bpInner.getDisposition() == null ||
                        bpInner.getDisposition().equals(Part.INLINE))) {

                  if (bpInner.isMimeType("text/*")) {
                    textParts.add((String) bpInner.getContent());
                  }

                } // end of bodypart which are not attachments
              } // end of iterate over all inner bodyparts
            } // MultipartContainer in a MultipartContainer
            else if (bp.isMimeType("message/*")) {
              Object bpContent = bp.getContent();
              if (bpContent instanceof MimeMessage) {

                // Added by KJB.
                // Message container.
                MimeMessage mmInner = (MimeMessage) bp.getContent();
                // tack on the headers.
                for (Enumeration<String> e = mmInner.getAllHeaderLines(); e.hasMoreElements();) {
                  textParts.add(e.nextElement());
                }

                Object mmContent = mmInner.getContent();
                if (mmContent instanceof String) {
                  textParts.add((String) mmContent);
                } else if (mmContent instanceof InputStream) {
                  textParts.add(RegainToolkit.readStringFromStream((InputStream) mmContent));
                } else {
View Full Code Here

    if (null==oPg) throw new MessagingException("DBFolder.copyMessage() Source Message not found");

    DBFolder oSrcFldr = (DBFolder) oSrcMsg.getFolder();

    MboxFile oMboxSrc = null;
    MimeMessage oMimeSrc;
    String sNewGuid = null;
    try {
      if ((oSrcFldr.mode&MODE_MBOX)!=0) {
        if (DebugFile.trace) DebugFile.writeln("new MboxFile(" + oSrcFldr.getFile() + ", MboxFile.READ_ONLY)");
        oMboxSrc = new MboxFile(oSrcFldr.getFile(), MboxFile.READ_ONLY);
        InputStream oInStrm = oMboxSrc.getMessageAsStream(oPos.longValue(), iLen);
        oMimeSrc = new MimeMessage(Session.getDefaultInstance(new Properties()), oInStrm);
        oInStrm.close();
        oMboxSrc.close();
        oMboxSrc=null;

        String sId2 = oMimeSrc.getMessageID();
        if ((sId!=null) && (sId2!=null)) {
          if (!sId.trim().equals(sId2.trim())) {
            throw new MessagingException("MessageID "+ sId + " at database does not match MessageID " + oMimeSrc.getMessageID() + " at MBOX file " + oSrcFldr.getFile().getName() + " for message index " + oPg.toString());
          }
        } // fi (sId!=null && sId2!=null)

        appendMessage(oMimeSrc);
      }
      else {
        ByteArrayOutputStream oByOutStrm = new ByteArrayOutputStream();
        oSrcMsg.writeTo(oByOutStrm);
        ByteArrayInputStream oByInStrm = new ByteArrayInputStream(oByOutStrm.toByteArray());
        oByOutStrm.close();
        oMimeSrc = new MimeMessage(Session.getDefaultInstance(new Properties()), oByInStrm);
        oByInStrm.close();
        appendMessage(oMimeSrc);
      }
    }
    catch (Exception e) {
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.