-2
私はユーザーにメールを送信しているWebアプリケーションがあります。以下は私のコードです。春のMVCでJavaメールを送信するには
String host = "smtp.gmail.com";
String pwd = "123";
String from = "[email protected]";
String to = "[email protected]";
String subject = "Test";
String messageText = "This is demo mail";
int port = 587;
boolean sessionDebug = false;
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.required", "true");
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] add = {new InternetAddress(to)};
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setRecipients(Message.RecipientType.TO, add);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText); //Actual msg
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, from, pwd);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
このコードはローカルで実行されますが、サーバードメインでは失敗します。私はたくさんの検索をしていますが、その解決策は私のためには機能しませんでした。
transport.connect(host, from, pwd);
をtransport.connect(host, 587, from, pwd);
or 465
と置き換えてみました。また、String host="smtp.gmail.com";
を静的ドメインIPに置き換えました。まだ動作していません。
誰でも私が迷っているものを理解できますか?
は* *動作しないことは、あなたがエラーメッセージが表示されます意味?その場合は、スタックトレースを追加してください。 – Jens
コードにエラーはありません。このコードはローカルでうまく機能します。サーバーでは失敗します。 – Ashish
DNS /ルート/ファイアウォールの問題のように見えますが、Googleのメールサーバーに 'ping 'して、' telnet stmp.gmail.com'もあなたのサーバーから正しく動作することを確認してください。 –