赞
踩
maven 配置
- <dependency>
- <groupId>cn.hutool</groupId>
- <artifactId>hutool-all</artifactId>
- <version>5.8.26</version>
- </dependency>
-
- <dependency>
- <groupId>com.sun.mail</groupId>
- <artifactId>javax.mail</artifactId>
- <version>1.6.2</version>
- </dependency>
java 代码
- package com.bx.api.common.util;
-
- import cn.hutool.core.util.StrUtil;
- import cn.hutool.extra.mail.MailAccount;
- import cn.hutool.extra.mail.MailUtil;
-
- public class EmailSenderUtil {
-
- private static final String SMTP_HOST = "smtp.163.com";
- private static final int SMTP_PORT = 25;
- private static final String EMAIL_USERNAME = "1111a@163.com";
- private static final String EMAIL_PASSWORD = "1111MI";
-
- public static void sendEmail(String to, String subject, String content) {
- if (StrUtil.isBlank(to)) {
- System.err.println("收件人不能为空");
- return;
- }
- if (StrUtil.isBlank(subject)) {
- System.err.println("邮件主题不能为空");
- return;
- }
- if (StrUtil.isBlank(content)) {
- System.err.println("邮件内容不能为空");
- return;
- }
-
- MailAccount account = new MailAccount();
- account.setHost(SMTP_HOST);
- account.setPort(SMTP_PORT);
- account.setAuth(true);
- account.setFrom(EMAIL_USERNAME);
- account.setUser(EMAIL_USERNAME);
- account.setPass(EMAIL_PASSWORD);
-
- try {
- MailUtil.send(account, to, subject, content, false);
- System.out.println("邮件发送成功");
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("邮件发送失败");
- }
- }
-
- public static void main(String[] args) {
- String to = "1111@qq.com";
- String subject = "测试邮件";
- String content = "这是一封测试邮件";
- sendEmail(to, subject, content);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。