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

  • 相关阅读:
    第一篇博客
    Word2vec负采样
    Ubuntu系统为应用建立桌面快捷方式(以Pycharm为例)
    Kaggle入门Titanic——模型建立
    Kaggle入门Titanic——特征工程
    ubuntu系统theano和keras的安装
    win7系统下python安装numpy,matplotlib,scipy和scikit-learn
    ubuntu14.04环境下spyder的安装
    防止IE7,8进入怪异模式
    自定义列表
  • 原文地址:https://www.cnblogs.com/hoobey/p/5369945.html
Copyright © 2011-2022 走看看