javax.mail.Provider Java Examples

The following examples show how to use javax.mail.Provider. 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: MailSenderTest.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Override
public MailSender createSender() throws Exception {
	MailSender mailSender = new MailSender() {
		Session mailSession;
		@Override
		protected Session createSession() throws SenderException {
			try {
				mailSession = super.createSession();
				Provider provider = new Provider(Type.TRANSPORT, "smtp", TransportMock.class.getCanonicalName(), "IbisSource.org", "1.0");
				mailSession.setProvider(provider);

				return mailSession;
			} catch(Exception e) {
				e.printStackTrace();
				throw new SenderException(e);
			}
		}

		@Override
		public Message sendMessage(Message message, IPipeLineSession session) throws SenderException, TimeOutException {
			super.sendMessage(message, session);
			session.put("mailSession", mailSession);
			String correlationID = session.getMessageId();
			return new Message(correlationID);
		}
	};
	mailSender.setSmtpHost("localhost");
	mailSender.setSmtpUserid("user123");
	mailSender.setSmtpPassword("secret321");
	return mailSender;
}
 
Example #2
Source File: Providers.java    From javamail-mock2 with Apache License 2.0 5 votes vote down vote up
public static Provider getSMTPProvider(final String protocol, final boolean secure, final boolean mock) {
    if (mock) {
        return new Provider(Provider.Type.TRANSPORT, protocol, "de.saly.javamail.mock2.MockTransport", "JavaMail Mock2 provider", null);
    }

    return new Provider(Provider.Type.TRANSPORT, protocol, secure ? "com.sun.mail.smtp.SMTPSSLTransport"
            : "com.sun.mail.smtp.SMTPTransport", "Oracle", null);
}
 
Example #3
Source File: Providers.java    From javamail-mock2 with Apache License 2.0 5 votes vote down vote up
public static Provider getPOP3Provider(final String protocol, final boolean secure, final boolean mock) {
    if (mock) {
        return new Provider(Provider.Type.STORE, protocol, secure ? "de.saly.javamail.mock2.POP3SSLMockStore"
                : "de.saly.javamail.mock2.POP3MockStore", "JavaMail Mock2 provider", null);
    }

    return new Provider(Provider.Type.STORE, protocol, secure ? "com.sun.mail.pop3.POP3SSLStore" : "com.sun.mail.pop3.POP3Store",
            "Oracle", null);

}
 
Example #4
Source File: Providers.java    From javamail-mock2 with Apache License 2.0 5 votes vote down vote up
public static Provider getIMAPProvider(final String protocol, final boolean secure, final boolean mock) {
    if (mock) {
        return new Provider(Provider.Type.STORE, protocol, secure ? "de.saly.javamail.mock2.IMAPSSLMockStore"
                : "de.saly.javamail.mock2.IMAPMockStore", "JavaMail Mock2 provider", null);
    }

    return new Provider(Provider.Type.STORE, protocol, secure ? "com.sun.mail.imap.IMAPSSLStore" : "com.sun.mail.imap.IMAPStore",
            "Oracle", null);

}
 
Example #5
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 #6
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 #7
Source File: Session.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
/**
    * Add a provider to the session.
    *
    * @param	provider	the provider to add
    * @since	JavaMail 1.4
    */
   public synchronized void addProvider(Provider provider) {
providers.add(provider);
providersByClassName.put(provider.getClassName(), provider);
if (!providersByProtocol.containsKey(provider.getProtocol()))
    providersByProtocol.put(provider.getProtocol(), provider);
   }
 
Example #8
Source File: Session.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
/**
    * Get a Transport object using the given provider and urlname.
    *
    * @param	provider	the provider to use
    * @param	url		urlname to use (can be null)
    * @return A Transport object
    * @exception	NoSuchProviderException	If no provider or the provider
    *			was the wrong class.	
    */

   private Transport getTransport(Provider provider, URLName url)
				throws NoSuchProviderException {
// make sure we have the correct type of provider
if (provider == null || provider.getType() != Provider.Type.TRANSPORT) {
    throw new NoSuchProviderException("invalid provider");
}

return getService(provider, url, Transport.class);
   }
 
Example #9
Source File: Session.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
/**
    * Returns the default Provider for the protocol
    * specified. Checks mail.<protocol>.class property
    * first and if it exists, returns the Provider
    * associated with this implementation. If it doesn't exist, 
    * returns the Provider that appeared first in the 
    * configuration files. If an implementation for the protocol 
    * isn't found, throws NoSuchProviderException
    *
    * @param  protocol  Configured protocol (i.e. smtp, imap, etc)
    * @return Currently configured Provider for the specified protocol
    * @exception	NoSuchProviderException If a provider for the given
    *			protocol is not found.
    */
   public synchronized Provider getProvider(String protocol)
                                throws NoSuchProviderException {

if (protocol == null || protocol.length() <= 0) {
    throw new NoSuchProviderException("Invalid protocol: null");
}

Provider _provider = null;

// check if the mail.<protocol>.class property exists
String _className = props.getProperty("mail."+protocol+".class");
if (_className != null) {
    if (logger.isLoggable(Level.FINE)) {
	logger.fine("mail."+protocol+
			   ".class property exists and points to " + 
			   _className);
    }
    _provider = providersByClassName.get(_className);
} 

if (_provider != null) {
    return _provider;
} else {
    // returning currently default protocol in providersByProtocol
    _provider = providersByProtocol.get(protocol);
}

if (_provider == null) {
    throw new NoSuchProviderException("No provider for " + protocol);
} else {
    if (logger.isLoggable(Level.FINE)) {
	logger.fine("getProvider() returning " + _provider.toString());
    }
    return _provider;
}
   }
 
Example #10
Source File: Session.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
/**
    * Set the passed Provider to be the default implementation
    * for the protocol in Provider.protocol overriding any previous values.
    *
    * @param provider Currently configured Provider which will be 
    * set as the default for the protocol
    * @exception	NoSuchProviderException If the provider passed in
    *			is invalid.
    */
   public synchronized void setProvider(Provider provider)
			throws NoSuchProviderException {
if (provider == null) {
    throw new NoSuchProviderException("Can't set null provider");
}
providersByProtocol.put(provider.getProtocol(), provider);
providersByClassName.put(provider.getClassName(), provider);
props.put("mail." + provider.getProtocol() + ".class", 
	  provider.getClassName());
   }
 
Example #11
Source File: SMTPSSLProvider.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
public SMTPSSLProvider() {
super(Provider.Type.TRANSPORT, "smtps",
    SMTPSSLTransport.class.getName(), "Oracle", null);
   }
 
Example #12
Source File: Session.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
private void loadProvidersFromStream(InputStream is) 
			throws IOException {
if (is != null) {
    LineInputStream lis = new LineInputStream(is);
    String currLine;

    // load and process one line at a time using LineInputStream
    while ((currLine = lis.readLine()) != null) {

	if (currLine.startsWith("#"))
	    continue;
	if (currLine.trim().length() == 0)
	    continue;	// skip blank line
	Provider.Type type = null;
	String protocol = null, className = null;
	String vendor = null, version = null;
	    
	// separate line into key-value tuples
	StringTokenizer tuples = new StringTokenizer(currLine,";");
	while (tuples.hasMoreTokens()) {
	    String currTuple = tuples.nextToken().trim();
		
	    // set the value of each attribute based on its key
	    int sep = currTuple.indexOf("=");
	    if (currTuple.startsWith("protocol=")) {
		protocol = currTuple.substring(sep+1);
	    } else if (currTuple.startsWith("type=")) {
		String strType = currTuple.substring(sep+1);
		if (strType.equalsIgnoreCase("store")) {
		    type = Provider.Type.STORE;
		} else if (strType.equalsIgnoreCase("transport")) {
		    type = Provider.Type.TRANSPORT;
	    	}
	    } else if (currTuple.startsWith("class=")) {
		className = currTuple.substring(sep+1);
	    } else if (currTuple.startsWith("vendor=")) {
		vendor = currTuple.substring(sep+1);
	    } else if (currTuple.startsWith("version=")) {
		version = currTuple.substring(sep+1);
	    }
	}

	// check if a valid Provider; else, continue
	if (type == null || protocol == null || className == null 
	    || protocol.length() <= 0 || className.length() <= 0) {
		
	    logger.log(Level.CONFIG, "Bad provider entry: {0}",
					currLine);
	    continue;
	}
	Provider provider = new Provider(type, protocol, className,
				         vendor, version);

	// add the newly-created Provider to the lookup tables
	addProvider(provider);
    }
}
   }
 
Example #13
Source File: GmailSSLProvider.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
public GmailSSLProvider() {
super(Provider.Type.STORE, "gimaps", GmailSSLStore.class.getName(),
    "Oracle", null);
   }
 
Example #14
Source File: GmailProvider.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
public GmailProvider() {
super(Provider.Type.STORE, "gimap", GmailStore.class.getName(),
    "Oracle", null);
   }
 
Example #15
Source File: POP3SSLProvider.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
public POP3SSLProvider() {
super(Provider.Type.STORE, "pop3s", POP3SSLStore.class.getName(),
    "Oracle", null);
   }
 
Example #16
Source File: POP3Provider.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
public POP3Provider() {
super(Provider.Type.STORE, "pop3", POP3Store.class.getName(),
    "Oracle", null);
   }
 
Example #17
Source File: IMAPProvider.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
public IMAPProvider() {
super(Provider.Type.STORE, "imap", IMAPStore.class.getName(),
    "Oracle", null);
   }
 
Example #18
Source File: IMAPSSLProvider.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
public IMAPSSLProvider() {
super(Provider.Type.STORE, "imaps", IMAPSSLStore.class.getName(),
    "Oracle", null);
   }
 
Example #19
Source File: SMTPProvider.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
public SMTPProvider() {
super(Provider.Type.TRANSPORT, "smtp", SMTPTransport.class.getName(),
    "Oracle", null);
   }
 
Example #20
Source File: SupportMailProcessor.java    From camel-quarkus with Apache License 2.0 4 votes vote down vote up
@BuildStep
void process() throws IOException {
    List<String> providers = resources("META-INF/services/javax.mail.Provider")
            .flatMap(this::lines)
            .filter(s -> !s.startsWith("#"))
            .collect(Collectors.toList());

    List<String> imp1 = providers.stream()
            .map(this::loadClass)
            .map(this::instantiate)
            .map(Provider.class::cast)
            .map(Provider::getClassName)
            .collect(Collectors.toList());

    List<String> imp2 = Stream.of("META-INF/javamail.default.providers", "META-INF/javamail.providers")
            .flatMap(this::resources)
            .flatMap(this::lines)
            .filter(s -> !s.startsWith("#"))
            .flatMap(s -> Stream.of(s.split(";")))
            .map(String::trim)
            .filter(s -> s.startsWith("class="))
            .map(s -> s.substring("class=".length()))
            .collect(Collectors.toList());

    List<String> imp3 = resources("META-INF/mailcap")
            .flatMap(this::lines)
            .filter(s -> !s.startsWith("#"))
            .flatMap(s -> Stream.of(s.split(";")))
            .map(String::trim)
            .filter(s -> s.startsWith("x-java-content-handler="))
            .map(s -> s.substring("x-java-content-handler=".length()))
            .collect(Collectors.toList());

    reflectiveClass.produce(new ReflectiveClassBuildItem(true, false, false,
            Stream.concat(providers.stream(), Stream.concat(imp1.stream(), Stream.concat(imp2.stream(), imp3.stream())))
                    .distinct()
                    .toArray(String[]::new)));

    resource.produce(new NativeImageResourceBuildItem(
            "META-INF/services/javax.mail.Provider",
            "META-INF/javamail.charset.map",
            "META-INF/javamail.default.address.map",
            "META-INF/javamail.default.providers",
            "META-INF/javamail.address.map",
            "META-INF/javamail.providers",
            "META-INF/mailcap"));
}
 
Example #21
Source File: Session.java    From FairEmail with GNU General Public License v3.0 3 votes vote down vote up
/**
    * Get an instance of the store specified by Provider. If the URLName
    * is not null, uses it, otherwise creates a new one. Instantiates
    * the store and returns it. This is a private method used by
    * getStore(Provider) and getStore(URLName)
    * 
    * @param provider Store Provider that will be instantiated
    * @param url      URLName used to instantiate the Store
    * @return Instantiated Store
    * @exception	NoSuchProviderException If a provider for the given
    *			Provider/URLName is not found.
    */
   private Store getStore(Provider provider, URLName url) 
throws NoSuchProviderException {

// make sure we have the correct type of provider
if (provider == null || provider.getType() != Provider.Type.STORE ) {
    throw new NoSuchProviderException("invalid provider");
}

return getService(provider, url, Store.class);
   }
 
Example #22
Source File: Session.java    From FairEmail with GNU General Public License v3.0 2 votes vote down vote up
/**
    * Get a Store object for the given URLName. If the requested Store
    * object cannot be obtained, NoSuchProviderException is thrown.
    *
    * The "scheme" part of the URL string (Refer RFC 1738) is used 
    * to locate the Store protocol. <p>
    *
    * @param	url	URLName that represents the desired Store
    * @return		a closed Store object
    * @see		#getFolder(URLName)
    * @see		javax.mail.URLName
    * @exception	NoSuchProviderException If a provider for the given
    *			URLName is not found.
    */
   public Store getStore(URLName url) throws NoSuchProviderException {
String protocol = url.getProtocol();
Provider p = getProvider(protocol);
return getStore(p, url);
   }
 
Example #23
Source File: Session.java    From FairEmail with GNU General Public License v3.0 2 votes vote down vote up
/**
    * Get an instance of the store specified by Provider. Instantiates
    * the store and returns it.
    * 
    * @param provider Store Provider that will be instantiated
    * @return Instantiated Store
    * @exception	NoSuchProviderException If a provider for the given
    *			Provider is not found.
    */
   public Store getStore(Provider provider) throws NoSuchProviderException {
return getStore(provider, null);
   }
 
Example #24
Source File: Session.java    From FairEmail with GNU General Public License v3.0 2 votes vote down vote up
/**
    * Get a Transport object for the given URLName. If the requested 
    * Transport object cannot be obtained, NoSuchProviderException is thrown.
    *
    * The "scheme" part of the URL string (Refer RFC 1738) is used 
    * to locate the Transport protocol. <p>
    *
    * @param	url	URLName that represents the desired Transport
    * @return		a closed Transport object
    * @see		javax.mail.URLName
    * @exception	NoSuchProviderException If a provider for the given
    *			URLName is not found.
    */
   public Transport getTransport(URLName url) throws NoSuchProviderException {
String protocol = url.getProtocol();
Provider p = getProvider(protocol);
return getTransport(p, url);
   }
 
Example #25
Source File: Session.java    From FairEmail with GNU General Public License v3.0 2 votes vote down vote up
/**
    * Get an instance of the transport specified in the Provider. Instantiates
    * the transport and returns it.
    * 
    * @param provider Transport Provider that will be instantiated
    * @return Instantiated Transport
    * @exception	NoSuchProviderException If provider for the given
    *			provider is not found.
    */
   public Transport getTransport(Provider provider) 
                                     throws NoSuchProviderException {
return getTransport(provider, null);
   }
 
Example #26
Source File: Session.java    From FairEmail with GNU General Public License v3.0 2 votes vote down vote up
/**
    * This method returns an array of all the implementations installed 
    * via the javamail.[default.]providers files that can
    * be loaded using the ClassLoader available to this application.
    *
    * @return Array of configured providers
    */
   public synchronized Provider[] getProviders() {
Provider[] _providers = new Provider[providers.size()];
providers.toArray(_providers);
return _providers;
   }