java mail使用qq邮箱发邮件的配置方法

最近本身折腾了下Java中利用mai发送QQ邮件html

1.QQ邮箱设置session

  1.1 进去QQ邮箱-->设置-->帐号-->进行设置以下图ide

  

2.foxmail设置(因为我要利用它收邮件)this

  2.1 参照官方的设置便可 http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=371spa

  ps:填写的邮箱密码是独立密码:须要注意的就是SSL连接要勾选;smtp端口是465debug

3.Java中代码配置3d

  3.1 发送邮件配置代码code

//发送邮箱验证
        try {
            Properties prop = new Properties();
            prop.setProperty("mail.transport.protocol", "smtp");
            prop.setProperty("mail.smtp.host", "smtp.qq.com");
            prop.setProperty("mail.smtp.auth", "true");
            prop.put("mail.smtp.port","25");
            prop.setProperty("mail.debug", "true");
            Authenticator authenticator = new PopAuthenticator("1274444444@qq.com", "4444444");
            //建立会话
            Session session = Session.getInstance(prop,authenticator);
            //填写信封写信
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("1271099894@qq.com"));
            msg.setRecipient(RecipientType.TO, new InternetAddress(user.getEmail()));
            msg.setSubject(user.getUsername()+"激活邮箱!");
            msg.setText(user.getUsername()+",你好请到这个地址激活你的帐号:http://www.estore.com/ActiveServlet?activecode="+user.getActivecode());
            //验证用户名密码发送邮件
            Transport transport = session.getTransport();
//transport.connect("1274444444@qq.com","4444444");
            transport.send(msg);
        } 
        
View Code

  3.2辅助类htm

public class PopAuthenticator extends Authenticator {
     String userName = null;
        String password = null;
        public PopAuthenticator() {
        }
        public PopAuthenticator(String username, String password) {
            this.userName = username;
            this.password = password;
        }
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
        }
}
View Code

  3.3 若是要发送html能够参考以下代码:对象

MimeMessage mailMessage = new MimeMessage(sendMailSession);
        mailMessage.setFrom(new InternetAddress("1219999@qq.com"));
        // Message.RecipientType.TO属性表示接收者的类型为TO
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        mailMessage.setSubject(subject, "UTF-8");
        mailMessage.setSentDate(new Date());
        // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
        Multipart mainPart = new MimeMultipart();
        // 建立一个包含HTML内容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        html.setContent(content.trim(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        mailMessage.setContent(mainPart);
        Transport.send(mailMessage);
View Code
相关文章
相关标签/搜索