javax.mail.NoSuchProviderException Java Examples

The following examples show how to use javax.mail.NoSuchProviderException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MxPopTest.java    From mireka with Apache License 2.0 6 votes vote down vote up
private void retrieveMail() throws NoSuchProviderException,
        MessagingException, IOException {
    Properties properties = new Properties();
    Session session = Session.getInstance(properties);
    Store store =
            session.getStore(new URLName("pop3://john:secret@localhost:"
                    + PORT_POP + "/INBOX"));
    store.connect();
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);
    Message[] messages = folder.getMessages();
    assertEquals(1, messages.length);
    Message message = messages[0];
    assertEquals("Hello World!\r\n", message.getContent());
    message.setFlag(Flags.Flag.DELETED, true);
    folder.close(true);
    store.close();
}
 
Example #2
Source File: JMS2JavaMail.java    From javamail with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
	if (session == null) {
		throw new IllegalStateException("JavaMail session is null.");
	}
	javaMailSessionDelegate = new JavaMailSessionDelegate() {
		@Override
		public Transport findTransport(String protocolToUse) throws NoSuchProviderException {
			return session.getTransport(protocolToUse);
		}
		@Override
		public MimeMessage createMimeMessage(InputStream inputStream) throws MessagingException {
			return new MimeMessage(session, inputStream);
		}
	};
}
 
Example #3
Source File: JavaMailSenderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void javaMailSenderWithCustomSession() throws MessagingException {
	final Session session = Session.getInstance(new Properties());
	MockJavaMailSender sender = new MockJavaMailSender() {
		@Override
		protected Transport getTransport(Session sess) throws NoSuchProviderException {
			assertEquals(session, sess);
			return super.getTransport(sess);
		}
	};
	sender.setSession(session);
	sender.setHost("host");
	sender.setUsername("username");
	sender.setPassword("password");

	MimeMessage mimeMessage = sender.createMimeMessage();
	mimeMessage.setSubject("custom");
	mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
	mimeMessage.setSentDate(new GregorianCalendar(2005, 3, 1).getTime());
	sender.send(mimeMessage);

	assertEquals("host", sender.transport.getConnectedHost());
	assertEquals("username", sender.transport.getConnectedUsername());
	assertEquals("password", sender.transport.getConnectedPassword());
	assertTrue(sender.transport.isCloseCalled());
	assertEquals(1, sender.transport.getSentMessages().size());
	assertEquals(mimeMessage, sender.transport.getSentMessage(0));
}
 
Example #4
Source File: SSLUtils.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
public static Store configureStoreWithSSL(Properties props, Authenticator auth, MailInput input) throws NoSuchProviderException {
    configureWithSSL(props, input);
    URLName url = new URLName(input.getProtocol(), input.getHostname(), input.getPort(), StringUtils.EMPTY,
            input.getUsername(), input.getPassword());
    Session session = Session.getInstance(props, auth);
    return session.getStore(url);
}
 
Example #5
Source File: JMS2JavaMailTest.java    From javamail with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailNoTransportForProtocol() throws Exception {
    BytesMessage message = bytesMessageFor(source, new Address[]{new InternetAddress("[email protected]")});
    Exception exception = new NoSuchProviderException();
    when(sessionDelegate.findTransport(eq("testProto"))).thenThrow(exception);
    jms2JavaMail.onMessage(message);
    verify(message, never()).acknowledge();
    ArgumentCaptor<Exception> exceptionArgumentCaptor = ArgumentCaptor.forClass(Exception.class);
    verify(javaMailJMSStatistics, never()).onSuccess(any(MimeMessage.class), any(Address[].class));
    verify(javaMailJMSStatistics, times(1)).onFailure(any(MimeMessage.class), any(Address[].class), exceptionArgumentCaptor.capture());
    Assert.assertEquals(exception, exceptionArgumentCaptor.getValue());
}
 
Example #6
Source File: ForgotPasswordCommand.java    From FlexibleLogin with MIT License 5 votes vote down vote up
private Session buildSession(MailConfig emailConfig) {
    Properties properties = new Properties();
    properties.setProperty("mail.smtp.host", emailConfig.getHost());
    properties.setProperty("mail.smtp.auth", "true");
    properties.setProperty("mail.smtp.port", String.valueOf(emailConfig.getPort()));

    //ssl
    properties.setProperty("mail.smtp.socketFactory.port", String.valueOf(emailConfig.getPort()));
    properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    properties.setProperty("mail.smtp.socketFactory.fallback", "false");
    properties.setProperty("mail.smtp.starttls.enable", String.valueOf(true));
    properties.setProperty("mail.smtp.ssl.checkserveridentity", "true");

    //we only need to send the message so we use smtps
    properties.setProperty("mail.transport.protocol", "smtps");

    //explicit override stmp provider because of issues with relocation
    Session session = Session.getDefaultInstance(properties);
    try {
        session.setProvider(new Provider(Type.TRANSPORT, "smtps",
                "flexiblelogin.mail.smtp.SMTPSSLTransport", "Oracle", "1.6.0"));
    } catch (NoSuchProviderException noSuchProvider) {
        logger.error("Failed to add SMTP provider", noSuchProvider);
    }

    return session;
}
 
Example #7
Source File: SendMailTask.java    From FlexibleLogin with MIT License 5 votes vote down vote up
@Override
public void run() {
    ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    try (Transport transport = session.getTransport()) {
        // Prevent UnsupportedDataTypeException: no object DCH for MIME type multipart/alternative
        // cf. https://stackoverflow.com/questions/21856211/unsupporteddatatypeexception-no-object-dch-for-mime-type
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
        mc.addMailcap("text/html;; x-java-content-handler=flexiblelogin.mail.handlers.text_html");
        mc.addMailcap("text/xml;; x-java-content-handler=flexiblelogin.mail.handlers.text_xml");
        mc.addMailcap("text/plain;; x-java-content-handler=flexiblelogin.mail.handlers.text_plain");
        mc.addMailcap("multipart/*;; x-java-content-handler=flexiblelogin.mail.handlers.multipart_mixed");
        mc.addMailcap("message/rfc822;; x-java-content- handler=flexiblelogin.mail.handlers.message_rfc822");

        MailConfig mailConfig = plugin.getConfigManager().getGeneral().getMail();

        //connect to host and send message
        if (!transport.isConnected()) {
            String password = mailConfig.getPassword();
            transport.connect(mailConfig.getHost(), mailConfig.getAccount(), password);
        }

        transport.sendMessage(mail, mail.getAllRecipients());
        player.sendMessage(plugin.getConfigManager().getText().getMailSent());
    } catch (NoSuchProviderException providerEx) {
        plugin.getLogger().error("Transport provider not found", providerEx);
        plugin.getLogger().error("Registered providers: {}", Arrays.asList(session.getProviders()));

        player.sendMessage(plugin.getConfigManager().getText().getErrorExecutingCommand());
    } catch (MessagingException messagingEx) {
        plugin.getLogger().error("Error sending mail", messagingEx);
        player.sendMessage(plugin.getConfigManager().getText().getErrorExecutingCommand());
    } finally {
        Thread.currentThread().setContextClassLoader(oldClassLoader);
    }
}
 
Example #8
Source File: EmailReader.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
protected void doInitialize(UimaContext context) throws ResourceInitializationException {
  validateParams();

  Authenticator authenticator =
      new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(user, pass);
        }
      };

  Properties prop = new Properties();
  prop.put("mail.store.protocol", protocol);
  prop.put("mail.host", server);
  prop.put("mail." + protocol + ".port", port);
  prop.put("mail.user", user);

  Session session = Session.getInstance(prop, authenticator);
  try {
    store = session.getStore();
  } catch (NoSuchProviderException e) {
    throw new ResourceInitializationException(e);
  }

  try {
    store.connect();
    inboxFolder = store.getFolder(inbox);
    reopenConnection();
  } catch (MessagingException me) {
    throw new ResourceInitializationException(me);
  }
}
 
Example #9
Source File: DefaultMailService.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
private void sendMessage ( final Message message ) throws MessagingException, NoSuchProviderException
{
    final ClassLoader oldClassLoader = Thread.currentThread ().getContextClassLoader ();
    Thread.currentThread ().setContextClassLoader ( getClass ().getClassLoader () );

    try
    {

        // commit

        message.saveChanges ();

        // connect

        final Transport transport = this.session.getTransport ();
        transport.connect ();

        // send

        try
        {
            transport.sendMessage ( message, message.getAllRecipients () );
        }
        finally
        {
            // close

            transport.close ();
        }
    }
    finally
    {
        Thread.currentThread ().setContextClassLoader ( oldClassLoader );
    }
}
 
Example #10
Source File: TransportStrategyFactory.java    From smtp-connection-pool with Apache License 2.0 5 votes vote down vote up
public static TransportStrategy newProviderStrategy(final Provider provider) {
  return new TransportStrategy() {
    @Override
    public Transport getTransport(Session session) throws NoSuchProviderException {
      return session.getTransport(provider);
    }
  };
}
 
Example #11
Source File: TransportStrategyFactory.java    From smtp-connection-pool with Apache License 2.0 5 votes vote down vote up
public static TransportStrategy newAddressStrategy(final Address address) {
  return new TransportStrategy() {
    @Override
    public Transport getTransport(Session session) throws NoSuchProviderException {
      return session.getTransport(address);
    }
  };
}
 
Example #12
Source File: TransportStrategyFactory.java    From smtp-connection-pool with Apache License 2.0 5 votes vote down vote up
public static TransportStrategy newUrlNameStrategy(final URLName urlName) {
  return new TransportStrategy() {
    @Override
    public Transport getTransport(Session session) throws NoSuchProviderException {
      return session.getTransport(urlName);
    }
  };
}
 
Example #13
Source File: TransportStrategyFactory.java    From smtp-connection-pool with Apache License 2.0 5 votes vote down vote up
public static TransportStrategy newProtocolStrategy(final String protocol) {
  return new TransportStrategy() {
    @Override
    public Transport getTransport(Session session) throws NoSuchProviderException {
      return session.getTransport(protocol);
    }
  };
}
 
Example #14
Source File: TransportStrategyFactory.java    From smtp-connection-pool with Apache License 2.0 5 votes vote down vote up
public static TransportStrategy newSessiontStrategy() {
  return new TransportStrategy() {
    @Override
    public Transport getTransport(Session session) throws NoSuchProviderException {
      return session.getTransport();
    }
  };
}
 
Example #15
Source File: JavaMailSenderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void javaMailProperties() throws MessagingException {
	Properties props = new Properties();
	props.setProperty("bogusKey", "bogusValue");
	MockJavaMailSender sender = new MockJavaMailSender() {
		@Override
		protected Transport getTransport(Session sess) throws NoSuchProviderException {
			assertEquals("bogusValue", sess.getProperty("bogusKey"));
			return super.getTransport(sess);
		}
	};
	sender.setJavaMailProperties(props);
	sender.setHost("host");
	sender.setUsername("username");
	sender.setPassword("password");

	MimeMessage mimeMessage = sender.createMimeMessage();
	mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
	sender.send(mimeMessage);

	assertEquals("host", sender.transport.getConnectedHost());
	assertEquals("username", sender.transport.getConnectedUsername());
	assertEquals("password", sender.transport.getConnectedPassword());
	assertTrue(sender.transport.isCloseCalled());
	assertEquals(1, sender.transport.getSentMessages().size());
	assertEquals(mimeMessage, sender.transport.getSentMessage(0));
}
 
Example #16
Source File: JavaMailSenderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void javaMailSenderWithCustomSession() throws MessagingException {
	final Session session = Session.getInstance(new Properties());
	MockJavaMailSender sender = new MockJavaMailSender() {
		@Override
		protected Transport getTransport(Session sess) throws NoSuchProviderException {
			assertEquals(session, sess);
			return super.getTransport(sess);
		}
	};
	sender.setSession(session);
	sender.setHost("host");
	sender.setUsername("username");
	sender.setPassword("password");

	MimeMessage mimeMessage = sender.createMimeMessage();
	mimeMessage.setSubject("custom");
	mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
	mimeMessage.setSentDate(new GregorianCalendar(2005, 3, 1).getTime());
	sender.send(mimeMessage);

	assertEquals("host", sender.transport.getConnectedHost());
	assertEquals("username", sender.transport.getConnectedUsername());
	assertEquals("password", sender.transport.getConnectedPassword());
	assertTrue(sender.transport.isCloseCalled());
	assertEquals(1, sender.transport.getSentMessages().size());
	assertEquals(mimeMessage, sender.transport.getSentMessage(0));
}
 
Example #17
Source File: MailSessionBuilder.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
public Transport getTransport() throws NoSuchProviderException {
	if (securityMode == SecurityMode.SSL) {
		return session.getTransport("smtps");
	} else {
		return session.getTransport("smtp");
	}
}
 
Example #18
Source File: JavaMailSenderImpl.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Obtain a Transport object from the given JavaMail Session,
 * using the configured protocol.
 * <p>Can be overridden in subclasses, e.g. to return a mock Transport object.
 * @see javax.mail.Session#getTransport(String)
 * @see #getSession()
 * @see #getProtocol()
 */
protected Transport getTransport(Session session) throws NoSuchProviderException {
	String protocol	= getProtocol();
	if (protocol == null) {
		protocol = session.getProperty("mail.transport.protocol");
		if (protocol == null) {
			protocol = DEFAULT_PROTOCOL;
		}
	}
	return session.getTransport(protocol);
}
 
Example #19
Source File: JavaMailSenderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void javaMailProperties() throws MessagingException {
	Properties props = new Properties();
	props.setProperty("bogusKey", "bogusValue");
	MockJavaMailSender sender = new MockJavaMailSender() {
		@Override
		protected Transport getTransport(Session sess) throws NoSuchProviderException {
			assertEquals("bogusValue", sess.getProperty("bogusKey"));
			return super.getTransport(sess);
		}
	};
	sender.setJavaMailProperties(props);
	sender.setHost("host");
	sender.setUsername("username");
	sender.setPassword("password");

	MimeMessage mimeMessage = sender.createMimeMessage();
	mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
	sender.send(mimeMessage);

	assertEquals("host", sender.transport.getConnectedHost());
	assertEquals("username", sender.transport.getConnectedUsername());
	assertEquals("password", sender.transport.getConnectedPassword());
	assertTrue(sender.transport.isCloseCalled());
	assertEquals(1, sender.transport.getSentMessages().size());
	assertEquals(mimeMessage, sender.transport.getSentMessage(0));
}
 
Example #20
Source File: JavaMailSenderImpl.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Obtain a Transport object from the given JavaMail Session,
 * using the configured protocol.
 * <p>Can be overridden in subclasses, e.g. to return a mock Transport object.
 * @see javax.mail.Session#getTransport(String)
 * @see #getSession()
 * @see #getProtocol()
 */
protected Transport getTransport(Session session) throws NoSuchProviderException {
	String protocol	= getProtocol();
	if (protocol == null) {
		protocol = session.getProperty("mail.transport.protocol");
		if (protocol == null) {
			protocol = DEFAULT_PROTOCOL;
		}
	}
	return session.getTransport(protocol);
}
 
Example #21
Source File: JavaMailSenderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void javaMailSenderWithCustomSession() throws MessagingException {
	final Session session = Session.getInstance(new Properties());
	MockJavaMailSender sender = new MockJavaMailSender() {
		@Override
		protected Transport getTransport(Session sess) throws NoSuchProviderException {
			assertEquals(session, sess);
			return super.getTransport(sess);
		}
	};
	sender.setSession(session);
	sender.setHost("host");
	sender.setUsername("username");
	sender.setPassword("password");

	MimeMessage mimeMessage = sender.createMimeMessage();
	mimeMessage.setSubject("custom");
	mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
	mimeMessage.setSentDate(new GregorianCalendar(2005, 3, 1).getTime());
	sender.send(mimeMessage);

	assertEquals("host", sender.transport.getConnectedHost());
	assertEquals("username", sender.transport.getConnectedUsername());
	assertEquals("password", sender.transport.getConnectedPassword());
	assertTrue(sender.transport.isCloseCalled());
	assertEquals(1, sender.transport.getSentMessages().size());
	assertEquals(mimeMessage, sender.transport.getSentMessage(0));
}
 
Example #22
Source File: JavaMailSenderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void javaMailProperties() throws MessagingException {
	Properties props = new Properties();
	props.setProperty("bogusKey", "bogusValue");
	MockJavaMailSender sender = new MockJavaMailSender() {
		@Override
		protected Transport getTransport(Session sess) throws NoSuchProviderException {
			assertEquals("bogusValue", sess.getProperty("bogusKey"));
			return super.getTransport(sess);
		}
	};
	sender.setJavaMailProperties(props);
	sender.setHost("host");
	sender.setUsername("username");
	sender.setPassword("password");

	MimeMessage mimeMessage = sender.createMimeMessage();
	mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
	sender.send(mimeMessage);

	assertEquals("host", sender.transport.getConnectedHost());
	assertEquals("username", sender.transport.getConnectedUsername());
	assertEquals("password", sender.transport.getConnectedPassword());
	assertTrue(sender.transport.isCloseCalled());
	assertEquals(1, sender.transport.getSentMessages().size());
	assertEquals(mimeMessage, sender.transport.getSentMessage(0));
}
 
Example #23
Source File: AlfrescoJavaMailSender.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @return A new {@code PooledTransportWrapper} which borrows a pooled {@link Transport} on connect, and returns it to
 *             the pool on close.  
 */
@Override
protected Transport getTransport(Session session) throws NoSuchProviderException
{
    return new PooledTransportWrapper(transportPool, session, getProtocol());

}
 
Example #24
Source File: MailSender.java    From ats-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the SMTP session
 *
 * @throws ActionException
 */
private void initSession() throws ActionException {

    // initialize the mail session with the current properties
    session = Session.getInstance(mailProperties);
    // user can get more debug info with the session's debug mode
    session.setDebug(configurator.getMailSessionDebugMode());

    // initialize the SMPT transport
    try {
        transport = session.getTransport("smtp");
    } catch (NoSuchProviderException e) {
        throw new ActionException(e);
    }
}
 
Example #25
Source File: JavaMailSenderImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtain a Transport object from the given JavaMail Session,
 * using the configured protocol.
 * <p>Can be overridden in subclasses, e.g. to return a mock Transport object.
 * @see javax.mail.Session#getTransport(String)
 * @see #getSession()
 * @see #getProtocol()
 */
protected Transport getTransport(Session session) throws NoSuchProviderException {
	String protocol	= getProtocol();
	if (protocol == null) {
		protocol = session.getProperty("mail.transport.protocol");
		if (protocol == null) {
			protocol = DEFAULT_PROTOCOL;
		}
	}
	return session.getTransport(protocol);
}
 
Example #26
Source File: JavaMailSenderImpl.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Obtain a Transport object from the given JavaMail Session,
 * using the configured protocol.
 * <p>Can be overridden in subclasses, e.g. to return a mock Transport object.
 * @see javax.mail.Session#getTransport(String)
 * @see #getSession()
 * @see #getProtocol()
 */
protected Transport getTransport(Session session) throws NoSuchProviderException {
	String protocol	= getProtocol();
	if (protocol == null) {
		protocol = session.getProperty("mail.transport.protocol");
		if (protocol == null) {
			protocol = DEFAULT_PROTOCOL;
		}
	}
	return session.getTransport(protocol);
}
 
Example #27
Source File: JavaMailSenderTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected Transport getTransport(Session session) throws NoSuchProviderException {
	this.transport = new MockTransport(session, null);
	return transport;
}
 
Example #28
Source File: Pop3Server.java    From greenmail with Apache License 2.0 4 votes vote down vote up
@Override
public POP3Store createStore() throws NoSuchProviderException {
    return (POP3Store) super.createStore();
}
 
Example #29
Source File: ImapServer.java    From greenmail with Apache License 2.0 4 votes vote down vote up
@Override
public IMAPStore createStore() throws NoSuchProviderException {
    return (IMAPStore) super.createStore();
}
 
Example #30
Source File: SSLUtils.java    From cs-actions with Apache License 2.0 4 votes vote down vote up
static Store configureStoreWithoutSSL(Properties props, Authenticator auth, GetMailInput input) throws NoSuchProviderException {
    props.put(PropNames.MAIL + input.getProtocol() + PropNames.HOST, input.getHostname());
    props.put(PropNames.MAIL + input.getProtocol() + PropNames.PORT, input.getPort());
    Session session = Session.getInstance(props, auth);
    return session.getStore(input.getProtocol());
}