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發送郵件的功能,郵件內容大致如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM