我在论坛上寻求帮助的时间已经很久了,但我已经在这个问题上苦苦挣扎了几天。
阅读所有论坛所做的所有ppl但没有改变。
这是我的代码
我做错了什么?
顺便说一句,我使用的是javamail 5.1和java 8,并且已经发送了文本消息,所以没有属性问题
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", mailHost);
properties.put("mail.smtp.user", mailUserName);
properties.put("mail.smtp.password", mailPassword);
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(mD.mailUserName));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
Multipart multiPart = new MimeMultipart();
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText("This is actual message", "text/html; charset=utf-8");
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(html, "text/html; charset=utf-8");
multiPart.addBodyPart(textPart);
multiPart.addBodyPart(htmlPart);
message.setContent(multiPart);
// Send message
Transport transport = session.getTransport("smtp");
transport.connect( mD.mailHost,mD.mailUserName,mD.mailPassword);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader( javax.mail.Session.class.getClassLoader() );
try
{
message.setContent(multiPart);
//transport.sendMessage(message, message.getAllRecipients());
Transport.send(message, message.getAllRecipients());
}
catch (MessagingException e)
{
e.printStackTrace();
}
finally
{
Thread.currentThread().setContextClassLoader(classLoader);
}
transport.close();
}
catch (MessagingException mex)
{
mex.printStackTrace();
}