public void blockingSend() {
        Properties prop = new Properties();
        prop.setProperty("mail.transport.protocol", protocol);
        prop.setProperty("mail.smtp.host", host);
        prop.setProperty("mail.smtp.port", port);
        prop.setProperty("mail.smtp.auth", "true");//使用smtp身份验证
        //使用SSL，企业邮箱必需！
        //开启安全协议
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
        } catch (GeneralSecurityException e1) {
            LOG.error(e1);
        }
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);
        Session session = Session.getDefaultInstance(prop, new MyAuthenricator(account, pass));
        session.setDebug(true);
        MimeMessage mimeMessage = new MimeMessage(session);
        try {
            mimeMessage.setFrom(new InternetAddress(from, "xian"));
            for (String to : tos) {
                mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            }
            mimeMessage.setSubject(subject);
            mimeMessage.setSentDate(new Date());
            mimeMessage.setText(content);
            mimeMessage.saveChanges();
            Transport.send(mimeMessage);
        } catch (MessagingException | UnsupportedEncodingException e) {
            LOG.error(e);
            throw new RuntimeException(e);
        }
    }
public void blockingSend ( ) { Properties prop = new Properties ( ) ; prop . setProperty ( "mail.transport.protocol" , protocol ) ; prop . setProperty ( "mail.smtp.host" , host ) ; prop . setProperty ( "mail.smtp.port" , port ) ; prop . setProperty ( "mail.smtp.auth" , "true" ) ; MailSSLSocketFactory sf = null ; try { sf = new MailSSLSocketFactory ( ) ; sf . setTrustAllHosts ( true ) ; } catch ( GeneralSecurityException e1 ) { LOG . error ( e1 ) ; } prop . put ( "mail.smtp.ssl.enable" , "true" ) ; prop . put ( "mail.smtp.ssl.socketFactory" , sf ) ; Session session = Session . getDefaultInstance ( prop , new MyAuthenricator ( account , pass ) ) ; session . setDebug ( true ) ; MimeMessage mimeMessage = new MimeMessage ( session ) ; try { mimeMessage . setFrom ( new InternetAddress ( from , "xian" ) ) ; for ( String to : tos ) { mimeMessage . addRecipient ( Message . RecipientType . TO , new InternetAddress ( to ) ) ; } mimeMessage . setSubject ( subject ) ; mimeMessage . setSentDate ( new Date ( ) ) ; mimeMessage . setText ( content ) ; mimeMessage . saveChanges ( ) ; Transport . send ( mimeMessage ) ; } catch ( MessagingException | UnsupportedEncodingException e ) { LOG . error ( e ) ; throw new RuntimeException ( e ) ; } }
blocking sending emails.
TODO Make this asynchronous in the future.