我必須通過一個servlet發送一封電子郵件,我已經嘗試了一堆代碼,但是他們都沒有使用gmail帳戶,任何想法?使用servlet發送電子郵件
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
//auth = null;
Session sess = Session.getInstance(props, auth);
sess.setDebug(false);
// -- Create a new message --
Message msg = new MimeMessage(sess);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress("[email protected]"));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
"sendtoemail", false));
msg.setSubject("Test");
msg.setText("Yippe");
msg.setSentDate(new Date());
Transport.send(msg);
public class SMTPAuthenticator extends javax.mail.Authenticator {
@Override
public PasswordAuthentication getPasswordAuthentication() {
String username = "[email protected]";
String password = "mypass";
return new PasswordAuthentication(username, password);
}
}
此代碼拋出javax.mail.MessagingException: Could not convert socket to TLS
異常
如何將此項添加到my..netbeans項目中? – user217029 2009-11-23 14:21:56
下載jar並將其添加到構建路徑。 – Bozho 2009-11-23 14:27:19