Package javax.mail

Examples of javax.mail.Transport


    }

    @Test
    public void setHandler() throws Exception {
        session.connect();
        Transport rawTransport = getFieldValue(session, "transport", Transport.class);
        assertNotNull(rawTransport);

        // add handler1
        MyHandler handler1 = new MyHandler(null);
        session.setHandler(handler1);
View Full Code Here


    //
  }

  public static void send(final Mail mail, final MailSenderConfig config) {
    Session session = null;
    Transport transport = null;
    try {
      session = MailHelper.getSMTPSession(config);
      transport = MailHelper.getTransport(session, config);
      MimeMessage mimeMessage = MailHelper.toMimeMessage(mail, session);
      if (transport != null) {
        mimeMessage.saveChanges();
        transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
      } else {
        Transport.send(mimeMessage);
      }
    } catch (MessagingException e) {
      throw new MailException(e);
    } finally {
      if (transport != null) {
        if (transport.isConnected()) {
          try {
            transport.close();
          } catch (MessagingException e) {
            CoreLog.getInstance().getLog().warn(e.getMessage(), e);
          }
        }
      }
View Full Code Here

  private static Transport getTransport(final Session session, final MailSenderConfig mailSenderConfig) throws MessagingException {
    if (ConditionUtils.isEmpty(mailSenderConfig.getUser())) {
      return null;
    }

    Transport transport = session.getTransport(Constants.SMTP_PROTOCOL);
    transport.connect(mailSenderConfig.getSmtpServer(), mailSenderConfig.getSmtpPort(), mailSenderConfig.getUser(), mailSenderConfig.getPassword());
    return transport;
  }
View Full Code Here

            this.ignoreHooksCount--;
                return;
            }

            Session session = Session.getDefaultInstance(props, null);
            Transport trans = session.getTransport("smtp");
            trans.connect();

            this.smtpMessage = setUpMessage(session);

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

            trans.close();
        } catch (SAXException e){
          throw e;
        } catch (Exception e) {
            getLogger().error("Exception sending mail", e);
            sendExceptionElement(e);
View Full Code Here

            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
            String errMsg = "[ADDRERR] Address error when sending message to [" + sendTo + "] from [" + sendFrom + "] cc [" + sendCc + "] bcc [" + sendBcc + "] subject [" + subject + "]";
            Debug.logError(e, errMsg, 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() + "]", errMsg);
                    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(errMsg);
View Full Code Here

                attachments.addBodyPart(body);
            }
            msg.setContent(attachments);
            try {
                // Send the message using SMTP, or SMTPS if the host uses SSL
                Transport transport = sesh.getTransport(SSL ? "smtps" : "smtp");
                transport.connect(host, user, password);
                transport.sendMessage(msg, msg.getAllRecipients());
            } catch (SendFailedException sfe) {
                if (!shouldIgnoreInvalidRecipients()) {
                    throw new BuildException(GENERIC_ERROR, sfe);
                } else if (sfe.getValidSentAddresses() == null
                           || sfe.getValidSentAddresses().length == 0) {
View Full Code Here

        this.protocol = protocol;
    }

    @Override
    public void send(MimeMessage mimeMessage) throws MessagingException {
        Transport transport = getTransport(getSession());
        LOG.debug("Connecting to {}:{}", host, port);
        transport.connect(getHost(), getPort(), getUsername(), getPassword());

        try {
            if (mimeMessage.getSentDate() == null) {
                mimeMessage.setSentDate(new Date());
            }
            String messageId = mimeMessage.getMessageID();
            mimeMessage.saveChanges();
            if (messageId != null) {
                // preserve explicitly specified message id, as it may be lost on save
                mimeMessage.setHeader("Message-ID", messageId);
            }
            LOG.debug("Sending MimMessage: {} using host: {}", mimeMessage, host);
            transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
        } finally {
            try {
                transport.close();
            } catch (MessagingException e) {
                LOG.warn("Error closing transport to host " + host + ". This exception will be ignored.", e);
            }
        }
    }
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

        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

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

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

        TestTransport testTransport = (TestTransport) transport;
        assertFalse(testTransport.isEHLO());
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.