
以下为163邮箱的设置:设置->POP3/SMTP/IMAP
然后进入会有一个开启的 开关,需要发送短信给指定账户拿到特定编码,这个编码就是我们需要自行配置的邮箱密码(注意这个密码不是真实邮箱密码,是这里提供的安全码)
邮箱服务器与端口邮箱服务器一般为: smtp.xx.com,163邮箱此处写smtp.163.com
端口不加密:25,端口加密:465
编写代码EmailData:保存邮箱内容
EmailUtil:邮件相关操作处理
WeekPaper:主程序最后一步发送邮件
EmailData.java
❤李癩❤李癩❤李癩
public class EmailData {
private String title;
private String filePath;
private String from;
private String pwd;
private String to;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
}
package com.weekpaper.util;
import com.weekpaper.bean.EmailData;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
public class EmailUtil {
public static void sendEmail(EmailData emailData) throws AddressException, MessagingException, UnsupportedEncodingException {
Properties properties = new Properties();
// String emailServer = "mail."+emailData.getFrom().split("@")[1];
String url = "smtp.163.com";
properties.put("mail.transport.protocol", "smtp");// 连接协议
properties.put("mail.smtp.host", url);// 邮件服务器主机名
properties.put("mail.smtp.port", 465);// 端口号
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
// 得到回话对象
Session session = Session.getInstance(properties);
// 获取邮件对象
Message message = new MimeMessage(session);
// 设置发件人邮箱地址
message.setFrom(new InternetAddress(emailData.getFrom()));
// 设置收件人邮箱地址
InternetAddress[] addresses = new InternetAddress[1];
addresses[0] = new InternetAddress(emailData.getTo());
message.setRecipients(Message.RecipientType.TO, addresses);
// 设置邮件标题
message.setSubject(emailData.getTitle());
if (emailData.getFilePath() != null && emailData.getFilePath().length() > 0) {
// 创建消息部分
BodyPart messageBodyPart = new MimeBodyPart();
// 消息
messageBodyPart.setText("1");
// 创建多重消息
Multipart multipart = new MimeMultipart();
// 设置文本消息部分
multipart.addBodyPart(messageBodyPart);
// 附件部分
messageBodyPart = new MimeBodyPart();
// 设置要发送附件的文件路径
DataSource source = new FileDataSource(emailData.getFilePath());
messageBodyPart.setDataHandler(new DataHandler(source));
// messageBodyPart.setFileName(filename);
// 处理附件名称中文(附带文件路径)乱码问题
messageBodyPart.setFileName(MimeUtility.encodeText(new File(emailData.getFilePath()).getName()));
multipart.addBodyPart(messageBodyPart);
// 发送完整消息
message.setContent(multipart);
// 得到邮差对象
Transport transport = session.getTransport();
// 连接自己的邮箱账户
transport.connect(url, emailData.getFrom(), emailData.getPwd());// 客户端授权码
// 发送邮件
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} else {
message.setText("1");
// 得到邮差对象
Transport transport = session.getTransport();
// 连接自己的邮箱账户
transport.connect(url, emailData.getFrom(), emailData.getPwd());// 密客户端授权码
// 发送邮件
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}
}
❤李癩❤李癩❤李癩
EmailUtil .java
❤李癩❤李癩❤李癩
import com.weekpaper.bean.EmailData;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
public class EmailUtil {
public static void sendEmail(EmailData emailData) throws AddressException, MessagingException, UnsupportedEncodingException {
Properties properties = new Properties();
// String emailServer = "mail."+emailData.getFrom().split("@")[1];
String url = "smtp.163.com";
properties.put("mail.transport.protocol", "smtp");// 连接协议
properties.put("mail.smtp.host", url);// 邮件服务器主机名
properties.put("mail.smtp.port", 465);// 端口号
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
// 得到回话对象
Session session = Session.getInstance(properties);
// 获取邮件对象
Message message = new MimeMessage(session);
// 设置发件人邮箱地址
message.setFrom(new InternetAddress(emailData.getFrom()));
// 设置收件人邮箱地址
InternetAddress[] addresses = new InternetAddress[1];
addresses[0] = new InternetAddress(emailData.getTo());
message.setRecipients(Message.RecipientType.TO, addresses);
// 设置邮件标题
message.setSubject(emailData.getTitle());
if (emailData.getFilePath() != null && emailData.getFilePath().length() > 0) {
// 创建消息部分
BodyPart messageBodyPart = new MimeBodyPart();
// 消息
messageBodyPart.setText("1");
// 创建多重消息
Multipart multipart = new MimeMultipart();
// 设置文本消息部分
multipart.addBodyPart(messageBodyPart);
// 附件部分
messageBodyPart = new MimeBodyPart();
// 设置要发送附件的文件路径
DataSource source = new FileDataSource(emailData.getFilePath());
messageBodyPart.setDataHandler(new DataHandler(source));
// messageBodyPart.setFileName(filename);
// 处理附件名称中文(附带文件路径)乱码问题
messageBodyPart.setFileName(MimeUtility.encodeText(new File(emailData.getFilePath()).getName()));
multipart.addBodyPart(messageBodyPart);
// 发送完整消息
message.setContent(multipart);
// 得到邮差对象
Transport transport = session.getTransport();
// 连接自己的邮箱账户
transport.connect(url, emailData.getFrom(), emailData.getPwd());// 客户端授权码
// 发送邮件
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} else {
message.setText("1");
// 得到邮差对象
Transport transport = session.getTransport();
// 连接自己的邮箱账户
transport.connect(url, emailData.getFrom(), emailData.getPwd());// 密客户端授权码
// 发送邮件
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}
}
❤李癩❤李癩❤李癩
WeekPaper.java
❤李癩❤李癩❤李癩
import com.weekpaper.bean.ConfigData;
import com.weekpaper.bean.EmailData;
import com.weekpaper.bean.LogContent;
import com.weekpaper.util.*;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import java.io.UnsupportedEncodingException;
import java.util.*;
public class WeekPaper {
public static void main(String[] args) {
if(args==null || args.length<1){
LogUtil.severe("找不到配置文件!");
System.exit(0);
}
//1.加载配置文件信息
ConfigData cd = loadConfigFile(args[0]);
//2. 创建Git日志文件读取日志内容
Map> projectLogs = GitDealUtil.createLogTxt(cd);
//3. 创建 excel 周报文件
String sendFileName = DateUtil.toString(new Date())+"_"+cd.getAuthor()+".xls";
String excelFile = FileUtil.getFilePath(cd.getOutputPath(),sendFileName);
ExcelUtil.createExcel(excelFile,projectLogs,cd);
//4. 发送邮件
System.out.println("请输入任意字符,按回车键发送,若为空字符,则");
Scanner sc = new Scanner(System.in);
//下周工作内容等,可能需要填写,这里做个卡点,随便输入点什么就可以发送邮件
String input = sc.nextLine();
if(!input.isEmpty()){
sendEmail(cd,sendFileName);
}
}
private static void sendEmail(ConfigData cd ,String filePath){
try{
String from = cd.getEmailSender();
String pwd = cd.getEmailPassword();
String to = cd.getEmailSender();
if(isEmpty(from) || isEmpty(pwd) || isEmpty(to)){
LogUtil.severe("邮件配置信息不全!发送失败!");
System.exit(0);
}
String title = cd.getAuthor()+"_"+DateUtil.toString(new Date())+"_周报";
System.out.println("邮件信息:");
System.out.println("标题:"+title);
System.out.println("发件人:"+from);
System.out.println("收件人:");
System.out.print(to+" ");
System.out.println();
System.out.println("文件:"+filePath);
System.out.println("内容:"+"");
System.out.println();
EmailData emailData = new EmailData();
emailData.setTitle(title);
emailData.setFilePath(FileUtil.getFilePath(FileUtil.getFilePath(cd.getOutputPath(),filePath)));
emailData.setFrom(from);
emailData.setTo(to);
emailData.setPwd(pwd);
EmailUtil.sendEmail(emailData);
System.exit(0);
} catch (UnsupportedEncodingException | MessagingException e) {
LogUtil.severe(e.getMessage());
System.exit(0);
}
}
private static boolean isEmpty(String str){
return str == null || str.length() == 0;
}
private static ConfigData loadConfigFile(String path) {
ConfigData cd = FileUtil.loadConfigData(path);
if(cd == null){
LogUtil.severe("配置内容有误!");
System.exit(0);
}else{
LogUtil.info("获取配置文件"+ FileUtil.getFilePath(path)+"成功!");
// LogUtil.info("获取配置文件"+ FileUtil.getFilePath(args[0])+"成功!信息如下:");
// LogUtil.info(cd.toString());
}
return cd;
}
}
❤李癩❤李癩❤李癩
看到下面这句后运行成功,(自己发给自己),成功收到邮件:
尿寮
尿寮