码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA 使用qq邮箱发送邮件

时间:2017-06-25 11:57:09      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:身份验证   ica   htm   sage   tor   char   support   log   smtp发送邮件   

引入一个架包:

 

代码如下:

    private static final String QQ_EMAIL_HOST="smtp.qq.com";//qq SMTP服务器 地址
    private static final String QQ_EMAIL_PORT="587";//qq SMTP服务器 端口(465这个端口有问题)
    private static final String QQ_EMAIL_FROM="xxxxx@qq.com";//qq 发件人邮箱
    private static final String QQ_EMAIL_PASSWORD_CODE="xxxxxxxxxxxxxxx";//qq 16位的 授权码
    
    public static  void sendQQEmail(String to_address, String title, String content) throws UnsupportedEncodingException, MessagingException {
        // 创建Properties 类用于记录邮箱的一些属性
        Properties props = new Properties();
        // 表示SMTP发送邮件,必须进行身份验证
        props.put("mail.smtp.auth", "true");
        //此处填写SMTP服务器
        props.put("mail.smtp.host",QQ_EMAIL_HOST);
        //端口号,QQ邮箱给出了两个端口,465这个端口用的有问题,用这个587
        props.put("mail.smtp.port", QQ_EMAIL_PORT);
        // 此处填写你的发件人账号
        props.put("mail.user", QQ_EMAIL_FROM);
        // 此处的密码就是前面说的16位STMP口令(授权码)
        props.put("mail.password", QQ_EMAIL_PASSWORD_CODE);
        // 构建授权信息,用于进行SMTP进行身份验证
        Authenticator authenticator = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                String userName = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };
        // 使用环境属性和授权信息,创建邮件会话
        Session mailSession = Session.getInstance(props, authenticator);
        // 创建邮件消息
        MimeMessage message = new MimeMessage(mailSession);
        // 设置发件人
        InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
        message.setFrom(form);
        // 设置收件人的邮箱:收件人的邮箱不限于qq邮箱,也可以是163邮箱……
        InternetAddress to = new InternetAddress(to_address);
        message.setRecipient(Message.RecipientType.TO, to);
        // 设置邮件标题
        message.setSubject(title);
        // 设置邮件的内容体
        message.setContent(content, "text/html;charset=UTF-8");
        // 最后当然就是发送邮件啦
        Transport.send(message);
    }

 

JAVA 使用qq邮箱发送邮件

标签:身份验证   ica   htm   sage   tor   char   support   log   smtp发送邮件   

原文地址:http://www.cnblogs.com/dwb91/p/7076310.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!