JavaMailを使用して電子メールを送信しようとしていますが、機能しません。以下は私のコードです。誰か助けてくれますか?前もって感謝します。ここでjavaで電子メールを送信していますが、SMTPに接続しようとしています。
は、電子メールの送信を担当私のコントローラクラスである:
package com;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailService {
private static MailService theService = null;
private static Session mailSession;
private static final String HOST = "localhost";
private static final int PORT =465;
private static final String USER = "[email protected]"; //
private static final String PASSWORD = "admin"; //
private static final String FROM = "[email protected]";
public static void sendMessage(String recipient, String subject, String message) throws MessagingException {
if (theService==null) {
theService = new MailService();
}
MimeMessage mimeMessage = new MimeMessage(mailSession);
mimeMessage.setFrom(new InternetAddress(FROM));
mimeMessage.setSender(new InternetAddress(FROM));
mimeMessage.setSubject(subject);
mimeMessage.setContent(message, "text/plain");
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
Transport transport = mailSession.getTransport("smtps");
transport.connect(HOST,PORT,USER,PASSWORD);
// transport.connect();
transport.sendMessage(mimeMessage, mimeMessage.getRecipients(Message.RecipientType.TO));
transport.close();
}
private MailService() {
Properties props = new Properties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", HOST);
props.put("mail.smtps.user", USER);
props.put("mail.smtps.password", PASSWORD);
props.put("mail.smtps.auth", "false");
props.put("mail.smtp.from", FROM);
props.put("mail.smtps.quitwait", "false");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port",PORT);
mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
}
}
しかし、私はsendMessage()
メソッドを呼び出すときに、このエラーが表示されます。
DEBUG: setDebug: JavaMail version 1.5.6
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "localhost", port 465, isSSL true
を許可する必要があり、より一つのことは、あなたは 'SMTPSSLTransport'という部分を見逃していましたか? SMTP経由でSMTPを使用しているためです。 –
main()メソッドで試してみませんでした。私はフォームを使って電子メールを送信したいです。 –
このSMTPSSLTransportを完全に理解できないので、コードをチェックしてこのSMTPSSLTransportを解決しますか? –