Java:
Private Sub Button1_Click
(Me).As(JavaObject).RunMethod("sendmail",Null)
End Sub
#IF JAVA
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;
import java.io.File;
import java.io.IOException;
//public class EmailSenderFixed {
public void sendmail() {
// 邮箱配置 (需替换为实际值)
final String username = "[email protected]";
final String password = "**************"; // 非登录密码
String smtpHost = "smtp.qq.com"; // 根据邮箱类型设置(如QQ邮箱:smtp.qq.com)
int smtpPort = 465; // 163邮箱使用465端口
// 多收件人配置
List<String> toRecipients = Arrays.asList(
"[email protected]",
"[email protected]"
);
List<String> ccRecipients = Arrays.asList("[email protected]");
List<String> bccRecipients = Arrays.asList("[email protected]");
// 附件文件列表
List<String> attachments = Arrays.asList("C:\\Users\\80789\\Desktop\\20250730分拣差错.xlsx");
// 发送带附件的邮件
sendEmailWithAttachments(smtpHost, smtpPort, username, password,
toRecipients, ccRecipients, bccRecipients,
"测试邮件主题", "这是邮件正文内容 <b>HTML支持</b>", attachments);
}
private static void sendEmailWithAttachments(String smtpHost, int smtpPort,
String username, String password,
List<String> toRecipients,
List<String> ccRecipients,
List<String> bccRecipients,
String subject, String body,
List<String> attachments) {
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", smtpPort);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.trust", smtpHost); // 修复SSL证书信任问题
// 创建邮件会话
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// 创建邮件对象
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setSubject(subject);
// 设置收件人
for (String recipient : toRecipients) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
}
// 设置抄送
for (String cc : ccRecipients) {
message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
}
// 设置密送
for (String bcc : bccRecipients) {
message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
}
// 创建多部件容器 - 修复关键点:使用替代内容结构
BodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setContent(body, "text/html; charset=utf-8");
// 创建多部件容器
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(textBodyPart);
// 添加多个附件
for (String filename : attachments) {
System.out.println("添加附件: " + filename);
File file = new File(filename);
if (!file.exists()) {
System.err.println("警告:附件文件不存在 - " + filename);
continue;
}
BodyPart attachmentPart = new MimeBodyPart();
DataSource source = new FileDataSource(file);
attachmentPart.setDataHandler(new DataHandler(source));
// 处理中文文件名问题
String encodedFileName = MimeUtility.encodeText(file.getName(), "UTF-8", "B");
attachmentPart.setFileName(encodedFileName);
attachmentPart.setDisposition(Part.ATTACHMENT);
multipart.addBodyPart(attachmentPart);
}
// 设置邮件内容
message.setContent(multipart);
message.saveChanges(); // 关键修复:保存更改确保MIME头正确生成
// 发送邮件
Transport.send(message);
System.out.println("邮件已成功发送给 " + toRecipients.size() + " 位收件人!");
} catch (MessagingException | IOException e) {
System.err.println("邮件发送失败:");
e.printStackTrace();
System.err.println("使用的SMTP服务器: " + smtpHost + ":" + smtpPort);
System.err.println("使用的用户名: " + username);
System.err.println("错误详情: " + e.getMessage());
// 输出更详细的诊断信息
if (e instanceof MessagingException) {
MessagingException me = (MessagingException) e;
Exception next = me.getNextException();
if (next != null) {
System.err.println("Next Exception:");
next.printStackTrace();
}
}
}
}
//}
#End If