Package javax.mail.internet

Examples of javax.mail.internet.MimeMessage


   public Session getSession() {
      return this.session;
   }

   public Message getMessage() {
      return new MimeMessage(getSession());
   }
View Full Code Here


   public void sendEmail(InternetAddress from, InternetAddress to,
         String subject, String attachmentName, byte[] attachment,
         String attachmentName2, String attachment2, String encoding)
         throws XmlBlasterException {
      try {
         MimeMessage message = new MimeMessage(getSession());
         message.setFrom(from);
         InternetAddress tos[] = new InternetAddress[1];
         tos[0] = to;
         message.setRecipients(Message.RecipientType.TO, tos);
         message.setSubject(subject, encoding);

         // MimeBodyPart mbp1 = new MimeBodyPart(attachment);
         MimeBodyPart mbp1 = new MimeBodyPart(); // "application/x-any"
         // "application/xmlBlaster-xbformat"
         DataSource ds = new ByteArrayDataSource(attachment,
               "application/xmlBlaster-xbformat");
         mbp1.setDataHandler(new DataHandler(ds));
         mbp1.setFileName(attachmentName);
         // mbp1.getContentType(); "application/octet-stream"

         // create the Multipart and add its parts to it
         Multipart mp = new MimeMultipart();
         mp.addBodyPart(mbp1);

         if (attachmentName2 != null) {
            MimeBodyPart mbp2 = new MimeBodyPart(); // "text/plain"
            mbp2.setText(attachment2, encoding);
            mbp2.setFileName(attachmentName2);
            mp.addBodyPart(mbp2);
         }

         // add the Multipart to the message
         message.setContent(mp);

         // set the Date: header
         message.setSentDate(new Date());

         // message.setContent("A test mail from xmlBlaster with attachment in
         // xmlBlaster-SOCKET format", encoding); // is automatically
         // "text/plain"
View Full Code Here

    *        Container holding the message to send
    */
   public void sendEmailSync(EmailData emailData, String msgIdFileName) throws XmlBlasterException {
      if (emailData == null) throw new IllegalArgumentException("SmtpClient.sendEmail(): Missing argument emailData");
      try {
         MimeMessage message = new MimeMessage(getSession());
         message.setFrom(emailData.getFromAddress());
         message.setRecipients(Message.RecipientType.TO, emailData.getToAddresses());
         if (emailData.getCc().length > 0)
            message.setRecipients(Message.RecipientType.CC, emailData.getCc());
         if (emailData.getBcc().length > 0)
            message.setRecipients(Message.RecipientType.BCC, emailData.getBcc());
         message.setSubject(emailData.getSubject(), Constants.UTF8_ENCODING);
         AttachmentHolder[] holder = emailData.getAttachments();

         if (holder.length == 0 && emailData.getContent() != null && emailData.getContent().length() > 0) {
            message.setText(emailData.getContent(), Constants.UTF8_ENCODING);
         }
         else {
            // create the Multipart and add its parts to it
            Multipart multi = new MimeMultipart();
  
            if (emailData.getContent() != null && emailData.getContent().length() > 0) {
               MimeBodyPart mbp = new MimeBodyPart();
               mbp.setFileName("content.txt");
               mbp.setText(emailData.getContent(), Constants.UTF8_ENCODING);
               mbp.setDisposition(MimeBodyPart.INLINE);
               multi.addBodyPart(mbp);
            }
  
            for (int i=0; i<holder.length; i++) {
               MimeBodyPart mbp = new MimeBodyPart();
               // 'AA xmlBlasterMessage.xbf' will be automatically quoted to '"AA xmlBlasterMessage.xbf"' by javamail implementation
               // 'xx.xbf' names will be send unquoted
               mbp.setFileName(holder[i].getFileName());
               byte[] content = holder[i].getContent();
               if (this.messageIdForceBase64 && emailData.isMessageIdAttachment(holder[i], msgIdFileName)
                    || this.contentForceBase64 && emailData.isMsgUnitAttachment(holder[i])) {
                  //We don't need to do it, javamail does it for us
                  //content = Base64.encode(holder[i].getContent()).getBytes(Constants.UTF8_ENCODING);
                  //Buggy: is not accepted by javamail: (Why? and How?)
                  mbp.setHeader(Constants.EMAIL_TRANSFER_ENCODING, Constants.ENCODING_BASE64)// "Content-Transfer-Encoding", "base64");
               }
               else {
                  // http://www.ietf.org/rfc/rfc2045.txt
                  // The Quoted-Printable encoding REQUIRES that encoded lines be no more than 76
                  // characters long. (78 with CRLF), the line uses a trailing '=' as a soft line brake
                  mbp.setHeader(Constants.EMAIL_TRANSFER_ENCODING, Constants.ENCODING_QUOTED_PRINTABLE)// "Content-Transfer-Encoding", "quoted-printable");
                  if (holder[i].hasExtensionFromList(this.inlineExtension))
                     mbp.setDisposition(MimeBodyPart.INLINE);
               }
              
               // Encoding violates RFC 2231 but is very common to do so for non-ASCII character sets:
               //mbp.setFileName(MimeUtility.encodeText(holder[i].getFileName()));
               if (holder[i].getContentType().startsWith("text/")) {
                  //String tmp = MimeUtility.encodeText(new String(content, Constants.UTF8_ENCODING), Constants.UTF8_ENCODING, Constants.ENCODING_QUOTED_PRINTABLE);
                  //mbp.setText(tmp, Constants.UTF8_ENCODING);
                  String contentStr = new String(content, Constants.UTF8_ENCODING);
                  if (this.breakLongMessageIdLine && emailData.isMessageIdAttachment(holder[i], msgIdFileName)) {
                     // <messageId><sessionId>unknown</sessionId><requestId>1140597982821000000</requestId><methodName>update</methodName><expires>2006-02-23T08:46:22.821Z</expires></messageId>
                     contentStr = ReplaceVariable.replaceAll(contentStr, "<requestId>", "\r\n<requestId>");
                     contentStr = ReplaceVariable.replaceAll(contentStr, "<methodName>", "\r\n<methodName>");
                     contentStr = ReplaceVariable.replaceAll(contentStr, "<expires>", "\r\n<expires>");
                  }
                  mbp.setText(contentStr, Constants.UTF8_ENCODING);
               }
               else {
                  // "application/xmlBlaster-xbformat"
                  DataSource ds = new ByteArrayDataSource(
                        content,
                        holder[i].getContentType());
                  mbp.setDataHandler(new DataHandler(ds));
               }
               multi.addBodyPart(mbp);
            }
  
            // add the Multipart to the message
            message.setContent(multi);
         } // else multipart
        
         // set the Date: header
         Date date = new Date();
         message.setSentDate(date);

         // Set the xmlBlaster specific expiry header field
         // Expires: Thu, 15 Dec 2005 21:45:01 +0100 (CET)
         // This could be evaluated by MTA plugins
         if (this.addExpiresHeader && emailData.getExpiryTime() != null) {
            //message.setHeader(EmailData.EXPIRES_HEADER, emailData.getExpiryTime().toString());
            message.setHeader(EmailData.EXPIRES_HEADER_RFC2156, MailUtil.dateTime(emailData.getExpiryTime()));
         }

         if (log.isLoggable(Level.FINE))
            log.fine("Trying send email from=" + emailData.getFrom() + " to="
               + emailData.getRecipientsList());
View Full Code Here

      }
      return this.session;
   }

   public Message getMessage() {
      return new MimeMessage(getSession());
   }
View Full Code Here

            msgs = new Message[0];

         EmailData[] datas = new EmailData[msgs.length];
         for (int i = 0; i < msgs.length; i++) {
            log.fine("Reading message #" + (i+1) + "/" + msgs.length + " from INBOX");
            MimeMessage msg = (MimeMessage) msgs[i];
            if (clear)
               msg.setFlag(Flags.Flag.DELETED, true);

            Address[] froms = msg.getFrom();
            String from = (froms != null && froms.length > 0) ? froms[0]
                  .toString() : "";

            Address[] arr = msg.getAllRecipients();
            if (arr == null)
               arr = new Address[0];
            String[] recips = new String[arr.length];
            for (int j = 0; j < arr.length; j++)
               recips[j] = arr[j].toString();
           
            //String content = retrieveContent(msg); // Would sometimes deliver an attachment
            String content = "";
            datas[i] = new EmailData(recips, from, msg.getSubject(), content);
           
            datas[i].setSentDate(msg.getSentDate());
            datas[i].setReplyTo((InternetAddress[])msg.getReplyTo());
           
            /*
            String[] expires = msg.getHeader(EmailData.EXPIRES_HEADER);
            // "X-xmlBlaster-ExpiryDate: 2005-12-24 16:45:55.322"
            if (expires != null && expires.length > 0) {
               // expires[0]="2005-12-24 16:45:55.322"
               String value = expires[0].trim();
               try {
                  datas[i].setExpiryTime(java.sql.Timestamp.valueOf(value)); // Change to IsoDateParser with UTC!
               }
               catch (Throwable e) {
                  System.err.println("xmlBlaster Pop3Driver.java: Ignoring illegal email header '" + expires[0] + "'");
                  e.printStackTrace();
               }
            }
            else {
            */
               // Expires: Thu, 15 Dec 2005 21:45:01 +0100 (CET)
               String[]  expires = msg.getHeader(EmailData.EXPIRES_HEADER_RFC2156);
               if (expires != null && expires.length > 0) {
                  // Date: Thu, 17 Nov 2005 16:45:12 +0100 (CET)
                  String value = expires[0].trim();
                  try {
                     datas[i].setExpiryTime(MailUtil.dateTimeTS(value));
View Full Code Here

        } else {
          currentFolder = (IMAPFolder) imapStore.getFolder(urlName.getFile());
        }

        currentFolder.open(Folder.READ_WRITE);
        MimeMessage cplMessage = (MimeMessage) currentFolder.getMessageByUID(messageUID);
   
        if (cplMessage != null) {
          cplMessage.setFlag(Flags.Flag.SEEN, true);
          cplMessage.writeTo(baos);
          baos.flush();
          bytearrayMessage = baos.toByteArray();
          baos.close();
          mLastModifiedDate = cplMessage.getSentDate();

        }

        currentFolder.close(false);
        imapStore.close();      
View Full Code Here

    Properties props = System.getProperties();
    props.setProperty("mail.smtp.host", "localhost");
    props.setProperty("mail.smtp.port", SMTP_PORT+"");
    Session session = Session.getInstance(props);

    MimeMessage baseMsg = new MimeMessage(session);
    MimeBodyPart bp1 = new MimeBodyPart();
    bp1.setHeader("Content-Type", "text/plain");
    bp1.setContent("Hello World!!!", "text/plain; charset=\"ISO-8859-1\"");

    // Attach the file
    MimeBodyPart bp2 = new MimeBodyPart();
    FileDataSource fileAttachment = new FileDataSource(BIGFILE_PATH);
    DataHandler dh = new DataHandler(fileAttachment);
    bp2.setDataHandler(dh);
    bp2.setFileName(fileAttachment.getName());

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(bp1);
    multipart.addBodyPart(bp2);

    baseMsg.setFrom(new InternetAddress("Ted <ted@home.com>"));
    baseMsg.setRecipient(Message.RecipientType.TO, new InternetAddress(
        "success@subethamail.org"));
    baseMsg.setSubject("Test Big attached file message");
    baseMsg.setContent(multipart);
    baseMsg.saveChanges();

    log.debug("Send started");
    Transport t = new SMTPTransport(session, new URLName("smtp://localhost:"+SMTP_PORT));
    long started = System.currentTimeMillis();
    t.connect();
View Full Code Here

      if (startFolder.exists()) {
        try {
          startFolder.open(Folder.READ_ONLY);
          Message[] msgs = startFolder.getMessages();
          for (int i = 0; i < msgs.length; i++) {
            MimeMessage message = (MimeMessage) msgs[i];
            // It's a message -> Add a index job
            addJob(folderUrl + "/message_" + startFolder.getUID(message), folderUrl, false, true, null);
          }
          startFolder.close(false);
View Full Code Here

      String To = "receiver@there.com";
      String Subject = "Test";
      String body = "Test Body";

      Session session = Session.getInstance(this.getMailProperties(SMTP_PORT), null);
      Message msg = new MimeMessage(session);
      msg.setFrom(new InternetAddress(From));
      msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(To, false));
      msg.setSubject(Subject);

      msg.setText(body);
      msg.setHeader("X-Mailer", "musala");
      msg.setSentDate(new Date());

      Transport transport = null;

      try
      {
        transport = session.getTransport("smtp");
        transport.connect(HOST_NAME, SMTP_PORT, "ddd", "ddd");
        assertEquals(0, this.server.getMessages().size());
        transport.sendMessage(msg, InternetAddress.parse(To, false));
        assertEquals(1, this.server.getMessages().size());
        transport.sendMessage(msg, InternetAddress.parse("dimiter.bakardjiev@musala.com", false));
        assertEquals(2, this.server.getMessages().size());
      }
      catch (javax.mail.MessagingException me)
      {
        me.printStackTrace();
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
      finally
      {
        if (transport != null)
          transport.close();
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }

    Iterator<WiserMessage> emailIter = this.server.getMessages().iterator();
    WiserMessage email = emailIter.next();
    MimeMessage mime = email.getMimeMessage();
    assertEquals("Test", mime.getHeader("Subject")[0]);
    assertEquals("Test Body" + "\r\n", mime.getContent().toString());
  }
View Full Code Here

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

    MimeMessage msg = this.createMessage(session, from, to, subject, body);
    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.