同じ問題が発生した場合は、次のコードを書いています。しかし、次の例外が発生します。Javaで電子メールを送信できません
javax.mail.MessagingException:
SMTPホストに接続できませんでした:smtp.gmail.com、port:587; ネストされた例外はある:java.net.ConnectException:接続がタイムアウトしました:誰かが私がやっている間違いを伝えることができ
public static void main(String[] args) {
String to = "[email protected]" // valid gmail address.
String from = "[email protected]"; // valid gmail address
String host = "smtp.gmail.com";
String password = "****"; // password of the gmaill acc used in from
int port = 587;
Properties properties = System.getProperties();
properties.put("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.smtp.host",host);
properties.setProperty("mail.smtp.user", from);
properties.setProperty("mail.smtp.password", password);
properties.setProperty("mail.smtp.port", "587");
properties.setProperty("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(properties,null);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Test Mail");
message.setText("This is just a test mail generated");
Transport transport = session.getTransport("smtp");
transport.connect(host,from,password);
InternetAddress[] addresses = new InternetAddress[1];
addresses[0] = new InternetAddress(to);
transport.sendMessage(message,addresses);
System.out.println("Message Sent Successfully");
}catch(MessagingException excp){
System.out.println(excp);
}
}
を接続します。私のGmailアカウントには、Gmailのsmtpサーバーを使用するように設定する必要のある設定はありますか?
プロキシ/ファイアウォールの背後にいますか? – Santosh
いいえ、「smtp.gmail.com」にpingを実行しようとしましたが、パケットロスなしで応答を返します。だから私はそれがファイアウォールの問題だと思っていない。 – user1043422