Java Mail 发送邮件(带附件) 已成功,但是附件预览错误且无法下载
一开始我以为是文件编码的问题,尝试过改变文件的编码,但是依旧如此;但是,当我反过来从 QQ 邮箱发送邮件到 yeah 邮箱时,发现是没有问题的
下面是我的代码
/**
* 发送带附件的邮件(多个+正文)
*/
@Test
public void test3() throws AddressException, Exception{
//1)创建Session - 连接邮件服务器
Properties props = new Properties();
//邮件服务器地址 // smtp
props.setProperty("mail.smtp.host", "smtp.yeah.net");
//是否开启加密验证登录
props.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("synchronization@yeah.net","hdljd1lzsx");
}
});
//开启debug,可以看到发送邮件的过程
session.setDebug(true);
//2)创建一封邮件 -- 设置邮件内容
MimeMessage mail = new MimeMessage(session);
//发件人
mail.setFrom(new InternetAddress("synchronization@yeah.net"));
//收件人
mail.setRecipient(RecipientType.TO, new InternetAddress("1286640174@qq.com"));
//标题
mail.setSubject("周末远程会议研讨");
//装载邮件数据
MimeMultipart mmp = new MimeMultipart();
MimeBodyPart content = new MimeBodyPart();
MimeBodyPart attachment1 = new MimeBodyPart();
String filename = "缓存.txt";
attachment1.attachFile(new File("src\\缓存.txt"));
attachment1.setFileName(MimeUtility.encodeWord(filename));
content.setText("带附件邮件的正文");
mmp.addBodyPart(content, 0);
mmp.addBodyPart(attachment1, 1);
mail.setContent(mmp);
//3)发送邮件
Transport.send(mail);
}
当然我也尝试过发送其他文件,如图片
@Test
public void test3() throws AddressException, Exception{
//1)创建Session - 连接邮件服务器
Properties props = new Properties();
//邮件服务器地址 // smtp
props.setProperty("mail.smtp.host", "smtp.qq.com");
//是否开启加密验证登录
props.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("1286640174@qq.com","kdfaltvozuzybaae");
}
});
//开启debug,可以看到发送邮件的过程
session.setDebug(true);
//2)创建一封邮件 -- 设置邮件内容
MimeMessage mail = new MimeMessage(session);
//发件人
mail.setFrom(new InternetAddress("1286640174@qq.com"));
//收件人
mail.setRecipient(RecipientType.TO, new InternetAddress("synchronization@yeah.net"));
//标题
mail.setSubject("周末远程会议研讨");
//装载邮件数据
MimeMultipart mmp = new MimeMultipart();
MimeBodyPart content = new MimeBodyPart();
MimeBodyPart attachment1 = new MimeBodyPart();
String filename = "1.jpg";
attachment1.attachFile(new File("src\\1.jpg"));
attachment1.setFileName(MimeUtility.encodeWord(filename));
content.setText("带附件邮件的正文");
mmp.addBodyPart(content, 0);
mmp.addBodyPart(attachment1, 1);
mail.setContent(mmp);
//3)发送邮件
Transport.send(mail);
}
//下面的是项目目录结构
----- src
---- 缓存.txt
---- com.kongzi.test
---- MailTest.java
但是出现长时间的等待,控制台一堆乱码
9qgaQxM0cYMKglGIyM98/gfxrkTqzIZQZSyFDsUkkBieT+PFIt4s5XgF22gYO0ZPHJJ4qHWkTGgr
HcSvZtJ5EU8Vyy7gHikBGAPfnpTRYwSSYEmD5mEZ3wpGCMdO/FcZHdyyuQgMbxE7g0nIAOO/etW3
uZlYW22WSQShAXQhVOCcMe3APU1zTrSLVBG9Po0aShZJPJjC7ndVLjq3TA64A49q24NFEAljhl8w
DftZVyQQPcccDNcmtyjx+VKWik4CruCrvY4Kkk8da0LbUokKBJbl25MZeckjhc8Z4AxjNcrqu+pL
os2buCWCSJYonminVSJSoUEEcnjtkH8qxbq1lcFo41STY2WQbAWI46g45P6Vbt7/AH3MUc0jvJLO
E+YBVQc49gB/nmrskyeahhlkQgjJRsA9fb1A/Ku6hV7mfs2noQOJBCxhWOSUIREjDALYOPm28Dn9
KnksY5os7FVmiAfsSOcLnHPJ6Vbe3RVQIZnaZ2Zf3mdqD1GOSc9e2KuwQyK0UlrcMkgwyFXIZTnj
kDrXpQlF6lRTTMmKW50+O3eyRoPKmJjVHwXJVhySvTOCa9X8GfG/xf4W1G98m5v7i0W2HnWF5c7L
edtp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MzABEYZLY9ByMVajmEBhtlS1tzNKzIN/wA+4nc4ZAMAYI+b
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:101)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:126)
.......
希望大神们能够指点一二