Example usage for javax.mail Session getInstance

List of usage examples for javax.mail Session getInstance

Introduction

In this page you can find the example usage for javax.mail Session getInstance.

Prototype

public static Session getInstance(Properties props) 

Source Link

Document

Get a new Session object.

Usage

From source file:net.prhin.mailman.MailMan.java

public static void main(String[] args) {
    Properties props = new Properties();
    props.setProperty(resourceBundle.getString("mailman.mail.store"),
            resourceBundle.getString("mailman.protocol"));

    Session session = Session.getInstance(props);

    try {//  w  w  w . j a v  a2s.c  o m

        Store store = session.getStore();
        store.connect(resourceBundle.getString("mailman.host"), resourceBundle.getString("mailman.user"),
                resourceBundle.getString("mailman.password"));
        Folder inbox = store.getFolder(resourceBundle.getString("mailman.folder"));
        inbox.open(Folder.READ_ONLY);
        inbox.getUnreadMessageCount();
        Message[] messages = inbox.getMessages();

        for (int i = 0; i <= messages.length / 2; i++) {
            Message tmpMessage = messages[i];

            Multipart multipart = (Multipart) tmpMessage.getContent();

            System.out.println("Multipart count: " + multipart.getCount());

            for (int j = 0; j < multipart.getCount(); j++) {
                BodyPart bodyPart = multipart.getBodyPart(j);

                if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
                    if (bodyPart.getContent().getClass().equals(MimeMultipart.class)) {
                        MimeMultipart mimeMultipart = (MimeMultipart) bodyPart.getContent();

                        for (int k = 0; k < mimeMultipart.getCount(); k++) {
                            if (mimeMultipart.getBodyPart(k).getFileName() != null) {
                                printFileContents(mimeMultipart.getBodyPart(k));
                            }
                        }
                    }
                } else {
                    printFileContents(bodyPart);
                }
            }
        }

        inbox.close(false);
        store.close();

    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:pl.umk.mat.zawodyweb.email.EmailSender.java

public static void send(String address, String subject, String text) {
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");

    Session session = Session.getInstance(props);

    try {//from  w w  w.  j  a  v  a  2s.c  om
        Address[] addresses = InternetAddress.parse(address);

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(addressFrom));
        message.setRecipients(Message.RecipientType.TO, addresses);
        message.setSubject(subjectPrefix + subject);
        message.setSentDate(new Date());
        message.setText(text);

        Transport transport = session.getTransport("smtp");
        transport.connect(smtpHost, smtpPort, smtpUser, smtpPassword);

        transport.sendMessage(message, addresses);

        transport.close();
    } catch (MessagingException ex) {
        log.error(ex);
    }
}

From source file:fr.mycellar.configuration.TestConfiguration.java

@Bean
public Session session() {
    Properties properties = new Properties();

    Session session = Session.getInstance(properties);
    return session;
}

From source file:org.cloudfoundry.identity.uaa.login.test.FakeJavaMailSender.java

public FakeJavaMailSender() {
    session = Session.getInstance(new Properties());
    sentMessages = new ArrayList<>();
}

From source file:org.seedstack.javamail.internal.PropertyFileSessionConfigurer.java

@Override
public Map<String, Session> doConfigure() {
    Map<String, Session> sessions = Maps.newHashMap();
    String mailProviders[] = configuration.getStringArray("providers");

    if (mailProviders != null) {
        for (String provider : mailProviders) {
            Configuration mailProviderConfig = configuration
                    .subset(String.format("provider.%s.property", provider));

            Properties mailProviderProperties = new Properties();
            Iterator<String> keys = mailProviderConfig.getKeys();
            while (keys.hasNext()) {
                String configuredProperty = keys.next();
                mailProviderProperties.put(configuredProperty,
                        mailProviderConfig.getProperty(configuredProperty));
            }/*from ww  w  .j av  a  2s .  c o  m*/

            Session session = Session.getInstance(mailProviderProperties);
            sessions.put(provider, session);
        }
    }

    return sessions;
}

From source file:com.cloudbees.demo.beesshop.util.MailSessionFactoryBean.java

@Override
public void afterPropertiesSet() throws Exception {
    if (Strings.isNullOrEmpty(this.smtpUser)) {
        logger.info("Initialize anonymous mail session");
        mailSession = Session.getInstance(smtpProperties);
    } else {/*ww w  .j a v a2  s.  c o  m*/
        logger.info("Initialize mail session with user='{}', password='xxx'", smtpUser);

        smtpProperties.setProperty("mail.smtp.auth", "true");
        mailSession = Session.getInstance(smtpProperties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(smtpUser, smtpPassword);
            }
        });
    }
    mailSession.setDebug(debug);
}

From source file:com.spartasystems.holdmail.util.TestMailClient.java

public TestMailClient(int port, String smtpHost) {

    Properties props = new Properties();

    props.put("mail.smtp.auth", "false");
    props.put("mail.smtp.starttls.enable", "false");
    props.put("mail.smtp.host", smtpHost);
    props.put("mail.smtp.port", port);

    session = Session.getInstance(props);
}

From source file:io.kamax.mxisd.threepid.connector.email.EmailSmtpConnector.java

@Autowired
public EmailSmtpConnector(EmailSmtpConfig cfg) {
    this.cfg = cfg;
    session = Session.getInstance(System.getProperties());
}

From source file:fr.xebia.cocktail.MailService.java

@Inject
public MailService(@Named("smtpProperties") Properties smtpProperties) throws MessagingException {

    if (Strings.isNullOrEmpty(smtpProperties.getProperty("mail.username"))) {
        logger.info("Initialize anonymous mail session");
        mailSession = Session.getInstance(smtpProperties);
    } else {/*from   w ww . j  a v a  2 s.  c  om*/
        final String username = smtpProperties.getProperty("mail.username");
        final String password = smtpProperties.getProperty("mail.password");
        logger.info("Initialize mail session with username='{}', password='xxx'", username);

        mailSession = Session.getInstance(smtpProperties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
    }

    fromAddress = new InternetAddress(smtpProperties.getProperty("mail.from"));
}

From source file:ru.codemine.ccms.mail.EmailAttachmentExtractor.java

public void saveAllAttachment() {
    String protocol = ssl ? "imaps" : "imap";

    Properties props = new Properties();
    props.put("mail.store.protocol", protocol);

    try {//from w  ww  . j a  v a2 s . c o  m
        Session session = Session.getInstance(props);
        Store store = session.getStore();
        store.connect(host, port, username, password);

        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_WRITE);

        for (Message msg : inbox.getMessages()) {
            if (!msg.isSet(Flags.Flag.SEEN)) {
                String fileNamePrefix = settingsService.getStorageEmailPath() + "msg-" + msg.getMessageNumber();
                Multipart multipart = (Multipart) msg.getContent();
                for (int i = 0; i < multipart.getCount(); i++) {
                    BodyPart bodyPart = multipart.getBodyPart(i);
                    if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())
                            && StringUtils.isNotEmpty(bodyPart.getFileName())) {
                        MimeBodyPart mimimi = (MimeBodyPart) bodyPart; // =(^.^)= 
                        mimimi.saveFile(fileNamePrefix + MimeUtility.decodeText(bodyPart.getFileName()));
                        msg.setFlag(Flags.Flag.SEEN, true);
                    }
                }
            }
        }
    } catch (IOException | MessagingException e) {
        log.error("? ? ?,  : "
                + e.getMessage());
    }

}