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);  } }

  • 相关阅读:
    动态规划-树形dp
    排队打水
    耍杂技的牛
    合并果子
    贪心问题-区间类
    动态规划-状态压缩dp
    Linux分区知识及企业场景分区76
    企业面试题-find结合sed查找替换
    企业面试题-利用三剑客
    alias-unalias
  • 原文地址:https://www.cnblogs.com/hoobey/p/5369945.html
Copyright © 2011-2022 走看看