javamail發送電子郵件
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("[email protected]", "display title"));
InternetAddress[] address = {new InternetAddress("[email protected]")};
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();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.