Mail

作者: 青丝如梦 | 来源:发表于2019-12-05 14:42 被阅读0次

    使用Java验证smtp服务器凭据,无实际操作

    依赖:

            <dependency>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
                <version>1.4.7</version>
            </dependency>
    

    代码:

    import java.util.Properties;
    
    import javax.mail.AuthenticationFailedException;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Transport;
    
    /**
     * @author 
     * 日期 2019/12/5 13:58
     * 描述
     * @version 1.0
     * @since 1.0
     */
    public class ConfirmSmtp {
    
        public static void main(String[] args) {
    
            int port = 25;
            String host = "smtp.qq.cn";
            String user = "xxxxxxxxx@qq.com";
            String pwd = "xxxxxxxxx";
    
            try {
                Properties props = new Properties();
                // required for gmail
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.auth", "true");
                // or use getDefaultInstance instance if desired...
                Session session = Session.getInstance(props, null);
                Transport transport = session.getTransport("smtp");
                transport.connect(host, port, user, pwd);
                transport.close();
                System.out.println("success");
            } catch (AuthenticationFailedException e) {
                System.out.println("AuthenticationFailedException - for authentication failures");
                e.printStackTrace();
            } catch (MessagingException e) {
                System.out.println("for other failures");
                e.printStackTrace();
            } catch (Exception e) {
                System.out.println("other exception");
                e.printStackTrace();
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Mail

          本文链接:https://www.haomeiwen.com/subject/nqbdgctx.html