Package javax.mail

Examples of javax.mail.Transport


            Debug.logVerbose("What would have been sent, the addressee: " + sendTo + " subject: " + subject + " context: " + context, module);
            results.put("messageWrapper", new MimeMessageWrapper(session, mail));
            return results;
        }

        Transport trans = null;
        try {
            trans = session.getTransport("smtp");
            if (!useSmtpAuth) {
                trans.connect();
            } else {
                trans.connect(sendVia, authUser, authPass);
            }
            trans.sendMessage(mail, mail.getAllRecipients());
            results.put("messageWrapper", new MimeMessageWrapper(session, mail));
            results.put("messageId", mail.getMessageID());
            trans.close();
        } catch (SendFailedException e) {
            // message code prefix may be used by calling services to determine the cause of the failure
            Debug.logError(e, "[ADDRERR] Address error when sending message to [" + sendTo + "] from [" + sendFrom + "] cc [" + sendCc + "] bcc [" + sendBcc + "] subject [" + subject + "]", module);
            List<SMTPAddressFailedException> failedAddresses = FastList.newInstance();
            Exception nestedException = null;
            while ((nestedException = e.getNextException()) != null && nestedException instanceof MessagingException) {
                if (nestedException instanceof SMTPAddressFailedException) {
                    SMTPAddressFailedException safe = (SMTPAddressFailedException) nestedException;
                    Debug.logError("Failed to send message to [" + safe.getAddress() + "], return code [" + safe.getReturnCode() + "], return message [" + safe.getMessage() + "]", module);
                    failedAddresses.add(safe);
                    break;
                }
            }
            Boolean sendFailureNotification = (Boolean) context.get("sendFailureNotification");
            if (sendFailureNotification == null || sendFailureNotification) {
                sendFailureNotification(ctx, context, mail, failedAddresses);
                results.put("messageWrapper", new MimeMessageWrapper(session, mail));
                try {
                    results.put("messageId", mail.getMessageID());
                    trans.close();
                } catch (MessagingException e1) {
                    Debug.logError(e1, module);
                }
            } else {
                return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonEmailSendAddressError", UtilMisc.toMap("sendTo", sendTo, "sendFrom", sendFrom, "sendCc", sendCc, "sendBcc", sendBcc, "subject", subject), locale));
View Full Code Here


                Log.error(me.getMessage(), me);
            }
        }

        public void sendMessages() throws MessagingException {
            Transport transport = null;
            try {
                URLName url = new URLName("smtp", host, port, "", username, password);
                if (session == null) {
                    createSession();
                }
                transport = new com.sun.mail.smtp.SMTPTransport(session, url);
                transport.connect(host, port, username, password);
                for (MimeMessage message : messages) {
                    // Attempt to send message, but catch exceptions caused by invalid
                    // addresses so that other messages can continue to be sent.
                    try {
                        transport.sendMessage(message,
                            message.getRecipients(MimeMessage.RecipientType.TO));
                    }
                    catch (AddressException ae) {
                        Log.error(ae.getMessage(), ae);
                    }
                    catch (SendFailedException sfe) {
                        Log.error(sfe.getMessage(), sfe);
                    }
                }
            }
            finally {
                if (transport != null) {
                    try {
                        transport.close();
                    }
                    catch (MessagingException e) { /* ignore */ }
                }
            }
        }
View Full Code Here

          // as
          // RET= after MAIL FROM line.
          // "mail.smtp.dsn.notify" //default to
          // nothing...appended as
          // NOTIFY= after RCPT TO line.
          Transport transport = null;
          try {
            transport = session.getTransport(outgoingMailServer);
            try {
              transport.connect();
            } catch (MessagingException me) {
              log.error(getClass().getSimpleName()+" ("+getName()+").deliver(): Connection failed.",me);
              // Any error on connect should cause the mailet
              // to
              // attempt
              // to connect to the next SMTP server associated
              // with this MX record,
              // assuming the number of retries hasn't been
              // exceeded.
              if (failMessage(qi, rcpt, me, false)) {
                return true;
              } else {
                continue;
              }
            }
            transport.sendMessage(message, addr);
            // log.debug("message sent to " +addr);
            /*TODO: catch failures that should result
             * in failure with no retries
             } catch (SendFailedException sfe){
              qi.failForRecipient(que, );
             */
          } finally {
            if (transport != null) {
              transport.close();
              transport = null;
            }
          }
          logMessageBuffer = new StringBuffer(256)
            .append("Mail (")
View Full Code Here

    public void processMessage(Object mess) {
        MailMessage mailmess = (MailMessage) mess;

        // Get (or create and cache) mail session
        Session s = null;
    Transport trans=null;
        try {
            s = getMailSession();

            // Create new message
            MimeMessage msg = new MimeMessage(s);

            // Put data from request into message
            msg.setText(mailmess.getMessagetext());
            msg.setSubject(mailmess.getSubject());

            Address fromAddr = new InternetAddress(mailmess.getAddrfrom(),
                    mailmess.getNamefrom());
            msg.setFrom(fromAddr);

            Address toAddr = new InternetAddress(mailmess.getAddrto(),
                    mailmess.getNameto());
            msg.addRecipient(Message.RecipientType.TO, toAddr);
            msg.addHeaderLine("Content-Type: text/html;");
      msg.saveChanges();
            // Send the message
      trans=s.getTransport("smtp");
      trans.connect();
      trans.sendMessage(msg,new Address[]{toAddr});
      trans.close();
        } catch (Exception e) {
            JGossipLog.audit(
        LogLevel.ERROR,
        e.getMessage(),
        e);
View Full Code Here

            this.ignoreHooksCount--;
                return;
            }

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

            trans = session.getTransport("smtp");
            trans.connect();

            this.smtpMessage = setUpMessage(session);

          this.ignoreHooksCount++;
            super.sendStartElementEventNS(ELEMENT_RESULT);

            if (this.sendPartial == true) {
                for (int i = 0; i < this.toAddresses.size(); i++) {
                    Vector v = new Vector(1);
                    v.add(this.toAddresses.elementAt(i));
                    sendMail(v, trans);
                }
            } else {
                sendMail(this.toAddresses, trans);
            }

            trans.close();
            super.sendEndElementEventNS(ELEMENT_RESULT);
      this.ignoreHooksCount--;
        } catch (Exception sE) {
            this.getLogger().error("sendMail-Error", sE);
            this.sendExceptionElement(sE);
View Full Code Here

   * in case of failure when sending a message
   */
  protected void doSend(MimeMessage[] mimeMessages, Object[] originalMessages) throws MailException {
    Map<Object, Exception> failedMessages = new LinkedHashMap<Object, Exception>();

    Transport transport;
    try {
      transport = getTransport(getSession());
      transport.connect(getHost(), getPort(), getUsername(), getPassword());
    }
    catch (AuthenticationFailedException ex) {
      throw new MailAuthenticationException(ex);
    }
    catch (MessagingException ex) {
      // Effectively, all messages failed...
      for (int i = 0; i < mimeMessages.length; i++) {
        Object original = (originalMessages != null ? originalMessages[i] : mimeMessages[i]);
        failedMessages.put(original, ex);
      }
      throw new MailSendException("Mail server connection failed", ex, failedMessages);
    }

    try {
      for (int i = 0; i < mimeMessages.length; i++) {
        MimeMessage mimeMessage = mimeMessages[i];
        try {
          if (mimeMessage.getSentDate() == null) {
            mimeMessage.setSentDate(new Date());
          }
          String messageId = mimeMessage.getMessageID();
          mimeMessage.saveChanges();
          if (messageId != null) {
            // Preserve explicitly specified message id...
            mimeMessage.setHeader(HEADER_MESSAGE_ID, messageId);
          }
          transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
        }
        catch (MessagingException ex) {
          Object original = (originalMessages != null ? originalMessages[i] : mimeMessage);
          failedMessages.put(original, ex);
        }
      }
    }
    finally {
      try {
        transport.close();
      }
      catch (MessagingException ex) {
        if (!failedMessages.isEmpty()) {
          throw new MailSendException("Failed to close server connection after message failures", ex,
              failedMessages);
View Full Code Here

     * @throws IOException
     * @throws InterruptedException
     */
    public void execute(Message message) throws MessagingException, IOException, InterruptedException {

        Transport tr = session.getTransport(getProtocol());
        SynchronousTransportListener listener = null;

        if (synchronousMode) {
            listener = new SynchronousTransportListener();
            tr.addTransportListener(listener);
        }

        if (useAuthentication) {
            tr.connect(smtpServer, username, password);
        } else {
            tr.connect();
        }

        tr.sendMessage(message, message.getAllRecipients());

        if (synchronousMode) {
            listener.attend(); // listener cannot be null here
        }

        tr.close();
        logger.debug("transport closed");

        logger.debug("message sent");
        return;
    }
View Full Code Here

   * in case of failure when sending a message
   */
  protected void doSend(MimeMessage[] mimeMessages, Object[] originalMessages) throws MailException {
    Map failedMessages = new LinkedHashMap();
    try {
      Transport transport = getTransport(getSession());
      transport.connect(getHost(), getPort(), getUsername(), getPassword());
      try {
        for (int i = 0; i < mimeMessages.length; i++) {
          MimeMessage mimeMessage = mimeMessages[i];
          try {
            if (mimeMessage.getSentDate() == null) {
              mimeMessage.setSentDate(new Date());
            }
            String messageId = mimeMessage.getMessageID();
            mimeMessage.saveChanges();
            if (messageId != null) {
              // Preserve explicitly specified message id...
              mimeMessage.setHeader(HEADER_MESSAGE_ID, messageId);
            }
            transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
          }
          catch (MessagingException ex) {
            Object original = (originalMessages != null ? originalMessages[i] : mimeMessage);
            failedMessages.put(original, ex);
          }
        }
      }
      finally {
        transport.close();
      }
    }
    catch (AuthenticationFailedException ex) {
      throw new MailAuthenticationException(ex);
    }
View Full Code Here

   * in case of failure when sending a message
   */
  protected void doSend(MimeMessage[] mimeMessages, Object[] originalMessages) throws MailException {
    Map failedMessages = new HashMap();
    try {
      Transport transport = getTransport(getSession());
      transport.connect(getHost(), getPort(), getUsername(), getPassword());
      try {
        for (int i = 0; i < mimeMessages.length; i++) {
          MimeMessage mimeMessage = mimeMessages[i];
          try {
            if (mimeMessage.getSentDate() == null) {
              mimeMessage.setSentDate(new Date());
            }
            mimeMessage.saveChanges();
            transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
          }
          catch (MessagingException ex) {
            Object original = (originalMessages != null ? originalMessages[i] : mimeMessage);
            failedMessages.put(original, ex);
          }
        }
      }
      finally {
        transport.close();
      }
    }
    catch (AuthenticationFailedException ex) {
      throw new MailAuthenticationException(ex);
    }
View Full Code Here

        Store store = ((Session) proxy).getStore();
        assertNotNull(store);
        assertTrue(store instanceof TestStore);

        Transport transport = ((Session) proxy).getTransport();
        assertNotNull(transport);
        assertTrue(transport instanceof TestTransport);

    }
View Full Code Here

TOP

Related Classes of javax.mail.Transport

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.