下面列出了javax.mail.Provider.Type#javax.mail.Provider 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
}
/**
* 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());
}
/**
* 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);
}
/**
* 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);
}
public static TransportStrategy newProviderStrategy(final Provider provider) {
return new TransportStrategy() {
@Override
public Transport getTransport(Session session) throws NoSuchProviderException {
return session.getTransport(provider);
}
};
}
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;
}
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);
}
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);
}
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);
}
@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;
}
@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"));
}
public SMTPSSLProvider() {
super(Provider.Type.TRANSPORT, "smtps",
SMTPSSLTransport.class.getName(), "Oracle", null);
}
public SMTPProvider() {
super(Provider.Type.TRANSPORT, "smtp", SMTPTransport.class.getName(),
"Oracle", null);
}
public IMAPSSLProvider() {
super(Provider.Type.STORE, "imaps", IMAPSSLStore.class.getName(),
"Oracle", null);
}
public IMAPProvider() {
super(Provider.Type.STORE, "imap", IMAPStore.class.getName(),
"Oracle", null);
}
public POP3Provider() {
super(Provider.Type.STORE, "pop3", POP3Store.class.getName(),
"Oracle", null);
}
public POP3SSLProvider() {
super(Provider.Type.STORE, "pop3s", POP3SSLStore.class.getName(),
"Oracle", null);
}
public GmailProvider() {
super(Provider.Type.STORE, "gimap", GmailStore.class.getName(),
"Oracle", null);
}
public GmailSSLProvider() {
super(Provider.Type.STORE, "gimaps", GmailSSLStore.class.getName(),
"Oracle", null);
}
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);
}
}
}
/**
* 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);
}
/**
* 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;
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}