Search in sources :

Example 1 with NoSuchProviderException

use of javax.mail.NoSuchProviderException in project jodd by oblac.

the class Pop3Server method createSession.

/**
	 * {@inheritDoc}
	 */
public ReceiveMailSession createSession() {
    Session session = Session.getInstance(sessionProperties, authenticator);
    Store store;
    try {
        store = getStore(session);
    } catch (NoSuchProviderException nspex) {
        throw new MailException("Failed to create POP3 session", nspex);
    }
    return new ReceiveMailSession(session, store);
}
Also used : Store(javax.mail.Store) NoSuchProviderException(javax.mail.NoSuchProviderException) Session(javax.mail.Session)

Example 2 with NoSuchProviderException

use of javax.mail.NoSuchProviderException in project symmetric-ds by JumpMind.

the class MailService method testTransport.

public String testTransport(TypedProperties prop) {
    String error = null;
    Transport transport = null;
    try {
        Session session = Session.getInstance(getJavaMailProperties(prop));
        transport = session.getTransport(prop.get(ParameterConstants.SMTP_TRANSPORT, "smtp"));
        if (prop.is(ParameterConstants.SMTP_USE_AUTH, false)) {
            transport.connect(prop.get(ParameterConstants.SMTP_USER), prop.get(ParameterConstants.SMTP_PASSWORD));
        } else {
            transport.connect();
        }
    } catch (NoSuchProviderException e) {
        error = getNestedErrorMessage(e);
    } catch (MessagingException e) {
        error = getNestedErrorMessage(e);
    } finally {
        try {
            if (transport != null) {
                transport.close();
            }
        } catch (MessagingException e) {
        }
    }
    return error;
}
Also used : MessagingException(javax.mail.MessagingException) Transport(javax.mail.Transport) NoSuchProviderException(javax.mail.NoSuchProviderException) Session(javax.mail.Session)

Example 3 with NoSuchProviderException

use of javax.mail.NoSuchProviderException in project symmetric-ds by JumpMind.

the class MailService method sendEmail.

protected String sendEmail(String subject, String text, String recipients, Properties prop, String transportType, boolean useAuth, String user, String password) {
    Session session = Session.getInstance(prop);
    ByteArrayOutputStream ba = null;
    if (log.isDebugEnabled()) {
        session.setDebug(true);
        ba = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(ba);
        session.setDebugOut(ps);
    }
    Transport transport;
    try {
        transport = session.getTransport(transportType);
    } catch (NoSuchProviderException e) {
        log.error("Failure while obtaining transport", e);
        return getNestedErrorMessage(e);
    }
    try {
        if (useAuth) {
            transport.connect(user, password);
        } else {
            transport.connect();
        }
    } catch (MessagingException e) {
        log.error("Failure while connecting to transport", e);
        return getNestedErrorMessage(e);
    }
    try {
        MimeMessage message = new MimeMessage(session);
        message.setSentDate(new Date());
        message.setRecipients(RecipientType.BCC, recipients);
        message.setSubject(subject);
        message.setText(text);
        try {
            transport.sendMessage(message, message.getAllRecipients());
        } catch (MessagingException e) {
            log.error("Failure while sending notification", e);
            return getNestedErrorMessage(e);
        }
    } catch (MessagingException e) {
        log.error("Failure while preparing notification", e);
        return e.getMessage();
    } finally {
        try {
            transport.close();
        } catch (MessagingException e) {
        }
    }
    if (log.isDebugEnabled()) {
        log.debug(ba.toString());
    }
    return null;
}
Also used : PrintStream(java.io.PrintStream) MessagingException(javax.mail.MessagingException) MimeMessage(javax.mail.internet.MimeMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Transport(javax.mail.Transport) NoSuchProviderException(javax.mail.NoSuchProviderException) Date(java.util.Date) Session(javax.mail.Session)

Example 4 with NoSuchProviderException

use of javax.mail.NoSuchProviderException in project Gargoyle by callakrsos.

the class MailPopReceiver method receiveEmail.

public void receiveEmail() {
    try {
        //1) get the session object  
        //new Properties();
        Properties properties = System.getProperties();
        properties.put("mail.pop3.host", popHost);
        properties.put("mail.user", userName);
        properties.put("mail.from", hostEmailAddr);
        properties.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.pop3.socketFactory.port", "995");
        properties.put("mail.pop3.port", "995");
        properties.setProperty("mail.pop3.socketFactory.fallback", "false");
        if (this.proxy != null) {
            String hostName = proxy.getHostName();
            int port = proxy.getPort();
            String value = port + "";
            properties.put("proxySet", "true");
            properties.put("socksProxyHost", hostName);
            properties.put("socksProxyPort", value);
            properties.put("http.proxyHost", hostName);
            properties.put("http.proxyPort", port + "");
            properties.put("https.proxyHost", hostName);
            properties.put("https.proxyPort", port + "");
            //#####################################
            properties.setProperty("proxySet", "true");
            properties.setProperty("socksProxyHost", hostName);
            properties.setProperty("socksProxyPort", port + "");
            properties.setProperty("http.proxyHost", hostName);
            properties.setProperty("http.proxyPort", port + "");
            properties.setProperty("https.proxyHost", hostName);
            properties.setProperty("https.proxyPort", port + "");
        }
        Authenticator authenticator = new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(hostEmailAddr, password);
            }
        };
        Session emailSession = Session.getInstance(properties, authenticator);
        //			Session emailSession = Session.getDefaultInstance(properties);
        emailSession.setDebug(debug);
        //2) create the POP3 store object and connect with the pop server  
        //			POP3Store emailStore = (POP3Store) emailSession.getStore(type);
        URLName url = new URLName(type, popHost, 995, "", hostEmailAddr, password);
        POP3SSLStore emailStore = new POP3SSLStore(emailSession, url);
        System.out.println(emailStore.isSSL());
        emailStore.connect();
        System.out.println("connected.");
        //3) create the folder object and open it  
        Folder emailFolder = emailStore.getFolder("INBOX");
        emailFolder.open(Folder.READ_ONLY);
        //4) retrieve the messages from the folder in an array and print it  
        Message[] messages = emailFolder.getMessages();
        for (int i = 0; i < messages.length; i++) {
            Message message = messages[i];
            System.out.println("---------------------------------");
            System.out.println("Email Number " + (i + 1));
            System.out.println("Subject: " + message.getSubject());
            System.out.println("From: " + message.getFrom()[0]);
            System.out.println("Text: " + message.getContent().toString());
        }
        //5) close the store and folder objects  
        emailFolder.close(false);
        emailStore.close();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : Message(javax.mail.Message) MessagingException(javax.mail.MessagingException) URLName(javax.mail.URLName) IOException(java.io.IOException) Properties(java.util.Properties) Folder(javax.mail.Folder) POP3SSLStore(com.sun.mail.pop3.POP3SSLStore) NoSuchProviderException(javax.mail.NoSuchProviderException) Authenticator(javax.mail.Authenticator) PasswordAuthentication(javax.mail.PasswordAuthentication) Session(javax.mail.Session)

Example 5 with NoSuchProviderException

use of javax.mail.NoSuchProviderException in project opennms by OpenNMS.

the class JavaReadMailer method retrieveMessages.

/*
     * TODO: Need readers that:
     *   - use FetchProfiles
     *   - make use of pre-fetch nature of getMessages()
     *   - A reader for the entire system could be implemented using events/event listeners
     * TODO: Need to make this more efficient... probably needs state so that message contents can be retrieved from the
     *       store after they are read via this message.
     */
/**
 * <p>retrieveMessages</p>
 *
 * @param term a {@link javax.mail.search.SearchTerm} object.
 * @return a {@link java.util.List} object.
 * @throws org.opennms.javamail.JavaMailerException if any.
 */
public List<Message> retrieveMessages(SearchTerm term) throws JavaMailerException {
    Message[] msgs;
    Folder mailFolder = null;
    final ReadmailHost readmailHost = getReadmailHost(m_config);
    final UserAuth userAuth = getUserAuth(m_config);
    try {
        Store store = m_session.getStore(readmailHost.getReadmailProtocol().getTransport());
        store.connect(readmailHost.getHost(), (int) readmailHost.getPort(), userAuth.getUserName(), userAuth.getPassword());
        mailFolder = store.getFolder(m_config.getMailFolder());
        mailFolder.open(Folder.READ_WRITE);
        msgs = mailFolder.search(term);
    } catch (NoSuchProviderException e) {
        throw new JavaMailerException("No provider matching:" + readmailHost.getReadmailProtocol().getTransport() + " from config:" + m_config.getName(), e);
    } catch (MessagingException e) {
        throw new JavaMailerException("Problem reading messages from configured mail store", e);
    }
    List<Message> msgList = Arrays.asList(msgs);
    return msgList;
}
Also used : Message(javax.mail.Message) MessagingException(javax.mail.MessagingException) Store(javax.mail.Store) ReadmailHost(org.opennms.netmgt.config.javamail.ReadmailHost) Folder(javax.mail.Folder) UserAuth(org.opennms.netmgt.config.javamail.UserAuth) NoSuchProviderException(javax.mail.NoSuchProviderException)

Aggregations

NoSuchProviderException (javax.mail.NoSuchProviderException)17 MessagingException (javax.mail.MessagingException)10 Session (javax.mail.Session)9 Store (javax.mail.Store)6 Properties (java.util.Properties)5 Transport (javax.mail.Transport)5 Message (javax.mail.Message)4 Folder (javax.mail.Folder)3 UserAuth (org.opennms.netmgt.config.javamail.UserAuth)3 ArrayList (java.util.ArrayList)2 URLName (javax.mail.URLName)2 MimeMessage (javax.mail.internet.MimeMessage)2 ReadmailHost (org.opennms.netmgt.config.javamail.ReadmailHost)2 ActionException (com.axway.ats.action.model.ActionException)1 RbvStorageException (com.axway.ats.rbv.model.RbvStorageException)1 POP3SSLStore (com.sun.mail.pop3.POP3SSLStore)1 SMTPTransport (com.sun.mail.smtp.SMTPTransport)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1