Java程序通过javax.mail发送邮件
java开发遇到发送邮件的功能
一. 添加pom依赖
此处使用java自动的封装的依赖包,联网下载即可
<dependency><groupId>javax.mailgroupId><artifactId>mailartifactId><version>1.5.0-b01version>dependency>
二.发送邮件工具类
直接创建即可
package com.jz.ask.common.email;import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.*;/*** 邮件发送工具类* @author ZhaoRenHui* @since 2020-11-17*/
public class DataEtlEmail {/*** 邮件发送方法* @param myEmailNumber 发件人邮箱 (邮箱需开启SMTP服务)* @param myEmailSMTPPassword SMTP服务唯一密码(开通时获取)* @param emailSMTPHost SMTP 邮箱服务器 (例如: 网易163 smtp.163.com)* @param receiveMailNumber 收件人邮箱* @param sendName 发件人姓名* @param title 邮件标题* @param text 邮件正文* @return map 成功/错误信息*/public static Map<String,Object> sendEmail(String myEmailNumber,String myEmailSMTPPassword,String emailSMTPHost,String receiveMailNumber,String sendName,String title,String text) {Map<String, Object> map = new HashMap<String, Object>();//前提 邮箱账号必须要开启 SMTP 服务//发送邮箱//设置一个发送帐号String myEmailAccount = myEmailNumber;String myEmailPassword = myEmailSMTPPassword;String myEmailSMTPHost = emailSMTPHost; //网易126 SMTP smtp.126.com//收件人String receiveMailAccount = receiveMailNumber;Properties props = new Properties(); // 用于连接邮件服务器的参数配置(发送邮件时才需要用到)props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)props.setProperty("mail.smtp.host", myEmailSMTPHost); // 发件人的邮箱的 SMTP 服务器地址props.setProperty("mail.mime.address.strict", "false");props.setProperty("mail.smtp.auth", "true");props.setProperty("mail.smtp.port","465"); // 设置端口, 默认为 25端口,阿里云服务器不支持25, 改为465, 25不使用ssl链接, 465使用ssl链接props.put("mail.smtp.ssl.enable",true); Session mimeSession = Session.getInstance(props); // 根据参数配置,创建会话对象(为了发送邮件准备的)// 设置为debug模式, 可以查看详细的发送 logmimeSession.setDebug(false);try {// 创建邮件对象// 1. 创建一封邮件MimeMessage message = new MimeMessage(mimeSession);// 发件人message.setFrom(new InternetAddress(myEmailAccount, sendName, "UTF-8"));
// message.setFrom(new InternetAddress(myEmailAccount, currentUser.getPersonName(), "UTF-8")); //// 收件人(可以增加多个收件人、抄送、密送)message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMailAccount, "用户", "UTF-8"));// 邮件主题message.setSubject(title, "UTF-8");// Content: 邮件正文(可以使用html标签)message.setContent(text, "text/html;charset=UTF-8");// 设置发件时间message.setSentDate(new Date());// 保存设置message.saveChanges();Transport transport = mimeSession.getTransport();transport.connect(myEmailAccount, myEmailPassword);transport.sendMessage(message, message.getAllRecipients());transport.close();map.put("code", 200);map.put("message", "发送成功!");} catch (Exception e) {e.printStackTrace();map.put("code", 500);map.put("message", e.getMessage());}return map;}}
三. 使用
在你需要发送邮件的地方直接调用即可
SMTP服务在发件人邮箱开通即可, SMTP密码在开通时会返回
Map<String, Object> map = DataEtlEmail.sendEmail("发件人邮箱", "SMTP密码", "SMTP邮箱服务器", "收件人邮箱", "发件人姓名", "邮件标题", "邮件正文");if (map.get("code") == "200") { //发送成功System.out.println(map.get("message"));} else {System.out.println(map.get("message"));}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
