package wywk.email.util;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
/**
* @author 111
* @date 2024/1/9 13:38
* @description bai
*/
public class TencentEmailWithAttachment {
private String host;
private String port;
private String username;
private String password;
public TencentEmailWithAttachment(String host, String port, String username, String password) {
this.host = host; // SMTP 服务器地址
this.port = port; // SMTP 服务器端口号
this.username = username; // 发送者邮箱用户名
this.password = password; // 授权码或密码
}
public void sendEmailWithAttachment(String toAddress, String subject, String messageText, String attachmentPath) throws MessagingException {
// 设置邮件服务器属性
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true"); // 使用 SSL 加密
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.socketFactory.fallback", "false");
// SSL
properties.put("mail.smtp.starttls.required", "true");
properties.put("mail.transport.protocol", "smtp");
// 创建认证器
Authenticator auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
};
// 创建邮件会话
Session session = Session.getInstance(properties, auth);
// 创建邮件消息和设置字段
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
message.setSubject(subject);
// 创建消息部分
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(messageText);
// 创建多重消息
Multipart multipart = new MimeMultipart();
// 设置文本消息部分
multipart.addBodyPart(messageBodyPart);
// 添加附件部分
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachmentPath);
DataHandler dataHandler = new DataHandler(source);
messageBodyPart.setDataHandler(new DataHandler(source));
String filename = null;
try {
//解决发送带有附件的时候,附件名称乱码
filename = MimeUtility.encodeText("测试附件", "UTF-8", null);
messageBodyPart.setFileName(filename);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
// 添加附件到消息中
multipart.addBodyPart(messageBodyPart);
// 发送完整的消息
message.setContent(multipart);
// 发送邮件
Transport.send(message);
System.out.println("Sent email with attachment successfully...");
}
public static void main(String[] args) {
String host = "smtp.exmail.qq.com"; // 腾讯企业邮箱SMTP服务器
String port = "465";
String username = "你的邮件"; // 您的邮箱账户
String password = "不告诉你"; // 您的邮箱密码或授权码
TencentEmailWithAttachment sender = new TencentEmailWithAttachment(host, port, username, password);
System.out.println("初始化成功");
try {
sender.sendEmailWithAttachment(
"接收人邮箱", // 收件人邮箱
"邮件测试",// 邮件主题
"Hello, this is a test email with an attachment.你好", // 邮件正文
"D:/工作笔记/03OA/01流程表单文档/文本字段带链接方式.txt" // 附件路径
);
} catch (MessagingException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
Java通过腾讯邮箱发送含有附件的邮件
最新推荐文章于 2024-01-24 16:14:31 发布