QQ邮箱工具类

264 阅读2分钟

工具类的作用:通过邮箱工具类可以转发文本、文件邮箱到指定的人的QQ邮箱

1、引入依赖到pom文件

<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1</version>
</dependency>
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4</version>
</dependency>

2、获取QQ邮箱的授权码(随便在网上找个教程) www.xitongcheng.com/jiaocheng/d…

3、MailUtil

// 定义发件人的账号和授权码
public static String username = "3584981955@qq.com";
public static String password = "替换成QQ邮箱的授权码";

/**
 * 文本邮箱发送
 * @param str 发送的文本内容
 * @param sjr 收件人
 * @param bt  标题
 * @throws MessagingException
 */
public static void textMail(StringBuffer str,String sjr,String bt)throws MessagingException {
    // 发送邮箱
    Properties properties = new Properties();
    //协议
    properties.put("mail.transport.protocol", "smtp");
    //服务器
    properties.put("mail.smtp.host", "smtp.qq.com");
    //端口
    properties.put("mail.smtp.port", 465);
    properties.put("mail.smtp.auth", "true");
    //SSL加密传输
    properties.put("mail.smtp.ssl.enable", "true");
    //true-->>在控制台显示信息,false-->>不显示
    properties.put("mail.debug", "false");
    //得到回话对象
    Session session = Session.getInstance(properties);
    // 获取邮件对象
    Message message = new MimeMessage(session);
    //发件人邮箱
    message.setFrom(new InternetAddress("3584981955@qq.com"));
    //设置收件人
    message.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress(sjr) });
    //邮箱标题
    message.setSubject(bt);
    //邮箱内容
    message.setText(str.toString());
    //创建对象
    Transport transport = session.getTransport();
    //连接服务
    /**
     * @param 第一个参数为自己QQ邮箱
     * @param 第二个参数为授权码(刚刚QQ空间获取的)
     */
    transport.connect("smtp.qq.com",username, password);
    // 发送邮件
    transport.sendMessage(message, message.getAllRecipients());
    //关闭服务
    transport.close();
}

/**
 * 文件邮箱发送
 * @param path 文件路径
 * @param sjr 收件人
 * @param bt  标题
 * @throws MessagingException
 */
public static void fileMail(String path,String sjr,String bt)throws MessagingException {
    // 收件人邮箱地址
    String recipientEmail = sjr;

    // 邮件主题和正文
    String subject = bt;
    String body = bt;

    // 附件路径
    String attachmentPath = path;

    // 设置邮件服务器属性
    Properties properties = new Properties();
    properties.setProperty("mail.smtp.host", "smtp.qq.com");
    properties.setProperty("mail.smtp.auth", "true");

    // 创建会话对象
    Session session = Session.getInstance(properties, new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });

    try {
        // 创建邮件消息
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipientEmail));
        message.setSubject(subject);

        // 创建多部分消息
        Multipart multipart = new MimeMultipart();

        // 创建文本消息部分
        BodyPart textPart = new MimeBodyPart();
        textPart.setText(body);
        multipart.addBodyPart(textPart);

        // 创建附件消息部分
        BodyPart attachmentPart = new MimeBodyPart();
        DataSource source = new FileDataSource(attachmentPath);
        attachmentPart.setDataHandler(new DataHandler(source));
        attachmentPart.setFileName(source.getName());
        attachmentPart.setFileName(MimeUtility.encodeText(source.getName(), "UTF-8", "B"));
        multipart.addBodyPart(attachmentPart);

        // 设置消息内容
        message.setContent(multipart);

        // 发送邮件
        Transport.send(message);

        System.out.println("邮件发送成功!");
    } catch (MessagingException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}