zoukankan      html  css  js  c++  java
  • 基于java mail实现简单的QQ邮箱发送邮件

    刚学习到java邮件相关的知识,先写下这篇博客,方便以后翻阅学习。

    -----------------------------第一步 开启SMTP服务

    在 QQ 邮箱里的 设置->账户里开启 SMTP 服务 

    完成验证

    获取授权码(后面代码实现时使用)

    -----------------------------第二步 环境配置

    即下载第三方库

    https://github.com/javaee/javamail/releases

    -----------------------------第三步 代码实现

    package com.core;
    
    import java.security.GeneralSecurityException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Properties;
    import javax.mail.Address;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    import com.sun.mail.util.MailSSLSocketFactory;
    
    
    public class MailTool {
    
        public static void main(String[] args) throws MessagingException, GeneralSecurityException {
            
            Properties props = new Properties();
    
            // 开启debug调试
            props.setProperty("mail.debug", "true");
            // 发送服务器需要身份验证
            props.setProperty("mail.smtp.auth", "true");
            // 设置邮件服务器主机名
            props.setProperty("mail.host", "smtp.qq.com");
            // 发送邮件协议名称
            props.setProperty("mail.transport.protocol", "smtp");
    
            // 开启SSL加密,否则会失败
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            props.put("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.ssl.socketFactory", sf);
    
            // 创建session
            Session session = Session.getInstance(props);
            
            // 创建邮件
            Message msg = new MimeMessage(session);
            // 设置标题
            msg.setSubject("测试邮件");
            // 编辑内容
            StringBuilder builder = new StringBuilder();
            builder.append("这是一封java mail测试邮件
    ");
            builder.append("这是第二行");
            builder.append("
    时间 " + getStringDate());
            // 设置内容
            msg.setText(builder.toString());
            // 发送的邮箱地址
            msg.setFrom(new InternetAddress("自己的邮箱@qq.com"));
            // 通过session得到transport对象
            Transport transport = session.getTransport();
            // 连接邮件服务器:邮箱类型,帐号,授权码代替密码(更安全)
            transport.connect("smtp.qq.com", "自己的邮箱@qq.com", "授权码");
            // 发送邮件
            transport.sendMessage(msg, new Address[] { new InternetAddress("目标邮箱@qq.com") });
            transport.close();
        }
        
        /**
         * 获取当前时间
         * @return String
         */
        public static String getStringDate() {
            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(currentTime);
            return dateString;
        }
    
    }

    -----------------------------第四步 效果展示

     

    -----------------------------第五步 推荐

    JAVA发送邮件最全示例

    Java基于JavaMail实现向QQ邮箱发送邮件

  • 相关阅读:
    学习进度
    作业8:单元测试练习
    用户体验设计案例分析
    团队协作一
    需求分析
    结对编程——词频统计 2
    结对编程——词频统计
    个人项目-词频统计
    数组求和
    个人介绍和Github使用流程
  • 原文地址:https://www.cnblogs.com/jinxiaohang/p/7467954.html
Copyright © 2011-2022 走看看