私のアプリケーションで次のようにコードを書いています
public void send_email(String email)
{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.sendgrid.net");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("my_username","my_password");
}
});
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress("from@no-spam.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("ramesh@abc.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +
"\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
これは、サーブレット クラスで呼び出されるメソッドです。その後、正常に実行されDone
、コンソールに " " というメッセージが表示されましたが、Email-inbox にメールが届きませんでした。
この同じコードを Java アプリケーションとして実行すると、正常に動作し、電子メールを受け取りました。
しかし、Google Webサーバーで実行すると機能しません..そして、ここでlibからjavaee.jarファイルとmail.jarファイルの両方を削除しましたが、それでもエラーは発生しませんでした..
何か提案をください....