Java Mail sent the message (with attachment) successfully, but the attachment previewed incorrectly and could not be downloaded
at first I thought it was the file encoding problem. I tried to change the file encoding, but it was still the same. However, when I sent an email from QQ Mail to the yeah mailbox, I found that there was no problem
.here is my code
/**
* +
*/
@Test
public void test3() throws AddressException, Exception{
//1Session -
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);
}
of course, I have also tried to send other files, such as pictures
.@Test
public void test3() throws AddressException, Exception{
//1Session -
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);
}
/ / below is the project directory structure
----- src
---- .txt
---- com.kongzi.test
---- MailTest.java
but there is a long wait on the console. There are
9qgaQxM0cYMKglGIyM98/gfxrkTqzIZQZSyFDsUkkBieT+PFIt4s5XgF22gYO0ZPHJJ4qHWkTGgr
HcSvZtJ5EU8Vyy7gHikBGAPfnpTRYwSSYEmD5mEZ3wpGCMdO/FcZHdyyuQgMbxE7g0nIAOO/etW3
uZlYW22WSQShAXQhVOCcMe3APU1zTrSLVBG9Po0aShZJPJjC7ndVLjq3TA64A49q24NFEAljhl8w
DftZVyQQPcccDNcmtyjx+VKWik4CruCrvY4Kkk8da0LbUokKBJbl25MZeckjhc8Z4AxjNcrqu+pL
os2buCWCSJYonminVSJSoUEEcnjtkH8qxbq1lcFo41STY2WQbAWI46g45P6Vbt7/AH3MUc0jvJLO
E+YBVQc49gB/nmrskyeahhlkQgjJRsA9fb1A/Ku6hV7mfs2noQOJBCxhWOSUIREjDALYOPm28Dn9
KnksY5os7FVmiAfsSOcLnHPJ6Vbe3RVQIZnaZ2Zf3mdqD1GOSc9e2KuwQyK0UlrcMkgwyFXIZTnj
kDrXpQlF6lRTTMmKW50+O3eyRoPKmJjVHwXJVhySvTOCa9X8GfG/xf4W1G98m5v7i0W2HnWF5c7L
edtpCiNwpZGA3AY4BOSOlec6hEWijiZmd+xOCAo6VzModAXcvtywKyKVwQSCcf1r0aVfkR206sY7
n60eA/2gfDWv22nxX01ro2vSBU1XS7oeWkDI+GjjkYKZSQc7mCkZ+7XvFpriajJKtvc/a2WREVbZ
AUQjf8yuBhieO54Ar8MoNRupEt1iaZoYJBcoVY+S7EoQQd3O7Zg8cgV7f4K+NmveGYoYLW61aOKa
4kY20swk2jIby9yfcBTcNxzjHvXfQzaVF6msuSauj9eI1DyXDvFb2d3HE5umwquzAE/dwecucD/a
qV7p0WWENaEyKFkkO0BQRnDED0Yce4r5M8O/tP8AhfVNLmt76We3nu9xN/bQjZJHtyNsYDEbcD5j
1ycV7To3xJ8F+IEhga9juheRAy290qxq0cgwqnPB+8n4H619Lhc4o1t2ctXR2TPRZIbsARyMVQGR
gUTaGbC89SQMsas29kIrV5pZ9txLarEPk2SKqcDliSxyxOenNXLW60+cLdW8odHtQu2C9BJRnALb
MgFRxz3zxUwis7eFBG0mPNAVfODkAkIWyzdAK9uGKpTjozmcZXMMxyQpFEI5Z5doRp5GUtICTkkl
QAAM1tWk08ccTySIiJMHCR8kk7gytkcDnIIPU01klQFbC2s3jaXEss4VWB3A4JByCcnj8axSbx1j
1W5t5vsjT3KyIuVj4kcJnCghdo4x1HSk5czKTS3Oij1jyYZUghWRORJ9ojLsVBwcgkZGM1gSeJ8y
br
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)
.......
I hope the gods can give us some advice