Properties prop = new Properties();
prop.put("mail.smtp.host", SMTP);
prop.put("mail.smtp.auth", "true");
prop.put("mail.debug", "true");
Session session = Session.getInstance(prop, new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{ return new PasswordAuthentication("name","password"); }
});
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress("from@qq.com", "display title"));
InternetAddress[] address = {new InternetAddress("target@qq.com")};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(mailTitle);
msg.setSentDate(new Date());
MimeBodyPart part2 = new MimeBodyPart();
part2.setText(mailContent);
MimeBodyPart part1 = new MimeBodyPart();
File attach = new File("D://test.jar");
part1.attachFile(attach);
MimeMultipart mime = new MimeMultipart();
// mime.addBodyPart(part1);
mime.addBodyPart(part2);
msg.setContent(mime);
Transport.send(msg);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}