Java实现outlook服务的邮件发送

  • 依赖
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

 

  • POP3接收

  

String  USER_NAME   ="*****@outlook.com";
String  PASSWORD    = "******";    
public static void receivepop3Email() {
        String host = "outlook.office365.com";// change accordingly
        String mailStoreType = "pop3";
        String popPort = "995";
        Store store = null;
        // 配置连接信息
        Properties properties = new Properties();
        properties.put("mail.store.protocol", mailStoreType);
        properties.put("mail.pop3.host", host);
        properties.put("mail.pop3.port", popPort);
        properties.put("mail.pop3.starttls.enable", "true");
        try {
            Session emailSession = Session.getDefaultInstance(properties);
            //打开调式模式,可看到邮件发送过程中各步骤的详细信息
//            emailSession.setDebug(true);
            store = emailSession.getStore("pop3s");
            store.connect(host, USER_NAME, PASSWORD);
            Folder emailFolder = store.getFolder("INBOX");
            emailFolder.open(Folder.READ_ONLY);
            Message[] messages = emailFolder.getMessages();
            for (Message msg:messages) {
                System.out.println(msg.getSubject());
            }
            emailFolder.close(true);
            store.close();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }finally {
            if(null != store){
                try {
                    store.close();
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            }
        }
    }

 

  • IMAP接收

String  USER_NAME   ="*****@outlook.com";
String  PASSWORD    = "******";   
public static void receiveimapEmail() {
        String host = "outlook.office365.com";// change accordingly
        String mailStoreType = "imap";
        String popPort = "993";
        try {
            // 配置连接信息
            Properties properties = new Properties();
            properties.put("mail.store.protocol", mailStoreType);
            properties.put("mail.imap.host", host);
            properties.put("mail.imap.port", popPort);
            properties.put("mail.imap.starttls.enable", "true");
            Session emailSession = Session.getDefaultInstance(properties);
//            emailSession.setDebug(true);
            Store store = emailSession.getStore("imaps");
            store.connect(host, USER_NAME, PASSWORD);
            Folder emailFolder = store.getFolder("INBOX");
            emailFolder.open(Folder.READ_ONLY);
            Message[] messages = emailFolder.getMessages();
            for (Message msg:messages) {
                System.out.println(msg.getSubject());
            }
            emailFolder.close(true);
            store.close();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

 

  • SMTP发送
    static String  USER_NAME   ="****@outlook.com";
    static String  PASSWORD    = "****";

    public static boolean sendSMTPMail(String to, String text, String title) {
            String host = "smtp.office365.com";
            String mailStoreType = "smtp";
            String popPort = "587";
            final Properties props = new Properties();

            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.host", host);
            props.put("mail.store.protocol", mailStoreType);
            props.put( "mail.smtp.port", popPort );
            //开启SSL
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.socketFactory.port",popPort);
            props.put("mail.smtp.socketFactory.fallback","false");
        try {
            Session session = Session.getDefaultInstance(props, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(USER_NAME, PASSWORD);//账号密码
                }
            });
            session.setDebug(true);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(session);
            // 设置发件人
            InternetAddress form = new InternetAddress(USER_NAME);
            message.setFrom(form);
            // 设置收件人
            InternetAddress toAddress = new InternetAddress(to);
            message.setRecipient(Message.RecipientType.TO, toAddress);
            // 设置邮件标题
            message.setSubject(title);
            // 设置邮件的内容体
            message.setContent(text, "text/html;charset=UTF-8");
            // 发送邮件
            Transport.send(message);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

插曲:

  SMTP发送邮件时,第一次发送可能会收到如下的报错:

OutboundSpamException; Failed to process message due to a permanent exception with message WASCL UserAction verdict is not None

  在网上查到这是邮箱厂商的安全策略,出现此异常的同时厂商会给你发送一封确认邮件,只要你登录邮箱并激活该邮件即可使用SMTP发送邮件的功能,邮件内容大致如下:

 

posted @ 2020-11-19 11:17  这,看不懂  阅读(3747)  评论(0编辑  收藏  举报