以下のコードは、SSLを使用してSTMP経由で電子メールを送信するために正常に動作します。 SMTPがTSLに変更されました。メールは送信されません。 私はTSLでSMTP経由で電子メールを送信
props.put("mail.smtp.starttls.enable","true");
を追加するようないくつかのことを試してみましたが、それは全く使用しません。
エラーコードは言う:
「javax.mail.MessagingException:SMTPホストに接続できませんでした:exch.studi.fhws.de、ポート:587; ネストされた例外は次のとおりです。 持つjavax.net.ssl .SSLException:認識されないSSLメッセージ、平文接続? "
アイデア?これは、接続が得る-行くからSSLであることを有することを意味するSSL対応の電子メールポート、である -
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendMail
{
String d_email = "...",
password = "...",
host = "...",
port = "25",
mailTo = "...",
mailSubject = "test",
mailText = "test";
public SendMail()
{
Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
SecurityManager security = System.getSecurityManager();
try
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.setText(mailText);
msg.setSubject(mailSubject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
Transport.send(msg);
}
catch (Exception mex)
{
mex.printStackTrace();
}
}
public static void main(String[] args)
{
TextClass tc = new TextClass();
}
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(d_email, password);
}
}
}
ルック:http://blogs.oracle.com/apanicker/entry/java_code_for_smtp_server – Marcelo
おかげで、コード上記は同じです - 私はこの例を使用しました。 – nerc