现在我想将此otp发送到gmail,然后从那里我必须阅读otp然后进入登录页面并使认证成功。 我正在使用java API发送邮件,这是用于发送邮件的程序(apache版本7本地主机正在使用) 包com.otp;
import java.util.Properties;
import javax.mail.Message; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage;
public class SendMail { public static void send(String to,String sub,String msg, final String user,final String pass){ //创建Properties Class的实例 属性props = new Properties();
/*
* Specifies the IP address of your default mail server for e.g if you
* are using gmail server as an email sever you will pass smtp.gmail.com
* as value of mail.smtp host. As shown here in the code. Change
* accordingly, if your email id is not a gmail id
*/
props.put("mail.smtp.host", "smtp.gmail.com");
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");
props.put("mail.smtp.starttls.enable","true");
/*
* Pass Properties object(props) and Authenticator object for
* authentication to Session instance
*/
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pass);
}
});
try {
/*
* Create an instance of MimeMessage, it accept MIME types and
* headers
*/
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
message.setSubject(sub);
message.setText(msg);
/* Transport class is used to deliver the message to the recipients */
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
但是我得到了以下错误。
com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp.gmail.com,465;超时-1; 嵌套异常是: java.net.UnknownHostException:smtp.gmail.com at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2054) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697) 在javax.mail.Service.connect(Service.java:386) 在javax.mail.Service.connect(Service.java:245) 在javax.mail.Service.connect(Service.java:194) 在javax.mail.Transport.send0(Transport.java:253) 在javax.mail.Transport.send(Transport.java:124) 在com.otp.SendMail.send(SendMail.java:60) 在com.otp.Generate.doGet(Generate.java:57) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:624) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) 在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) 在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) 在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:625) 在org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.doRun(AprEndpoint.java:2522) 在org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.run(AprEndpoint.java:2511) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) 在java.lang.Thread.run(未知来源) 引起:java.net.UnknownHostException:smtp.gmail.com 在java.net.AbstractPlainSocketImpl.connect(未知来源) 在java.net.PlainSocketImpl.connect(未知来源) 在java.net.SocksSocketImpl.connect(未知来源) 在java.net.Socket.connect(未知来源) 在java.net.Socket.connect(未知来源) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:331) 在com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238) 在com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2020) ......还有31个
如何解决它。并将otp代码发送到我的Gmail帐户? 是否有任何其他方法可以将生成的otp数字发送到Gmail或如何解决上述问题。请在这个问题上指导我。