zoukankan      html  css  js  c++  java
  • java mail使用qq邮箱发邮件的配置方法

    1.QQ邮箱设置

      1.1 进去QQ邮箱-->设置-->账号-->进行设置如下图

      

    2.foxmail设置(由于我要利用它收邮件)

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

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

    3.Java中代码配置

      3.1 发送邮件配置代码

    public class MailUtils {

     public static void sendMail(String email, String emailMsg)    throws AddressException, MessagingException {   // 1.创建一个程序与邮件服务器会话对象 Session

      Properties props = new Properties();   

    props.setProperty("mail.transport.protocol", "SMTP");   

    props.setProperty("mail.host", "smtp.sohu.com");   

    props.setProperty("mail.smtp.auth", "true");// 指定验证为true

      // 创建验证器   

    Authenticator auth = new Authenticator() {    

      public PasswordAuthentication getPasswordAuthentication() {    

     return new PasswordAuthentication("hoobey", "1234567890");    

    }  

     };

      Session session = Session.getInstance(props, auth);

      // 2.创建一个Message,它相当于是邮件内容   

    Message message = new MimeMessage(session);           

     message.setFrom(new InternetAddress("hoobey@sohu.com")); // 设置发送者

      message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者

      message.setSubject("用户激活");   // message.setText("这是一封激活邮件,请<a href='#'>点击</a>");

      message.setContent(emailMsg, "text/html;charset=utf-8");

      // 3.创建 Transport用于将邮件发送

      Transport.send(message);  } }

  • 相关阅读:
    HDU-4035 Maze
    poj 3744 Scout YYF I
    HDU 4911 Inversion
    HDU-3001 Travelling
    HDU 4539 郑厂长系列故事——排兵布阵
    poj 3311 Hie with the Pie
    poj-1185 炮兵阵地
    位运算
    HDU-1438 钥匙计数之一
    poj 3254 Corn Fields
  • 原文地址:https://www.cnblogs.com/hoobey/p/5369945.html
Copyright © 2011-2022 走看看