Java Mail无法验证openshift服务器中的smtp设置 [英] Java Mail fails to authenticate smtp setting in openshift server
问题描述
public class Mail_Authenticator {
public Session Get_Auth(){
//设置SMTP服务器属性
ResourceBundle rs_mail = ResourceBundle.getBundle(mail);
final String userName = rs_mail.getString(username);
final String password = rs_mail.getString(password);
属性属性= new Properties();
properties.put(mail.smtp.host,smtp.gmail.com);
properties.put(mail.smtp.port,587);
properties.put(mail.smtp.auth,true);
properties.put(mail.smtp.starttls.enable,true);
properties.put(mail.user,userName);
//创建与验证器的新会话
验证器auth = new Authenticator(){
@Override
public PasswordAuthentication getPasswordAuthentication(){
return新的PasswordAuthentication(userName,password);
}
};
return(Session.getInstance(properties,auth));
}
}
问题出在smtp设置当我在本地服务器上运行应用程序时,可以正常工作。但是当我在openshift服务器上运行应用程序异常是在
Transport.send(msg);
请问任何一个请指出问题,在上述设置中,为什么他们在本地工作机器?
以下是我得到的例外:
javax。 mail.AuthenticationFailedException:534-5.7.14< HTTPS:?//accounts.google.com/ContinueSignIn SARP = 1&安培; SCC = 1&安培; PLT = AKgnsbsWq 534-5.7.14 JJjb_c-FzrtUAccdDqOCMtsPAOL1AwIDSCoireBRoI5X-avznrYbparV84O_zkAvrHXMB9 534-5.7.14 T0Zj8zXP1g1woaWHnTzJQ3vWFn3lwTNl9Kn8Ma9 -d9FPD_xB-bMBSh5FEPdaMqID4WljXW 534-5.7.14 v67IfQzXHolKlY48pEiZF-cpGc6CEgknkET1ciEQf51vQuETuMrrzeC7EDcM7s89Njtm5e 534-5.7.14 crMNRLw>请通过网络浏览器登录,然后重试。 534-5.7.14了解更多信息,请访问https://support.google.com/mail/bin/answer.py?answer=787 534 5.7.14 54 e7sm180653184qag.7 - gsmtp
。 sun.mail.smtp.SMTPTransport $ Authenticator.authenticate(SMTPTransport.java:823)
在com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:756)
在com.sun。 mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:673)
javax.mail.Service.connect(Service.java:317)
javax.mail.Service.connect(Service.java: 176)
在javax.mail.Service.connect(Service.java:125)
在javax.mail.Transport.send0(Transport.java:194)
在javax.mail.Transport .send(Transport.java:124)
在Servlet.Mail.sendEmail(Mail.java:72)
在Servlet.Mail.doGet(Mail.java:206)
在javax。 servlet.http.HttpServlet.service(HttpServlet.java:621)
在javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
在org.apache.catalina.core.ApplicationFilterChain。 internalDoFilter(ApplicationFilterChain.java:305)
在org.apache.catal ina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
在org.apache.catalina.core。 StandardContextValve.invoke(StandardContextValve.java:123)
在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
在org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:171)
在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java: 953)
在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1008)
在org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:589)
在org .apac he.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:310)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
在java.util。 concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)
在java.lang.Thread.run(Thread.java:724)
我也有同样的问题。尝试从浏览器登录到Gmail帐户。如果他认为是非法访问(可能来自不同的地理位置),Gmail会阻止身份验证请求。您可能会看到一条消息或通知。按照通知或消息中的说明进行操作,然后可以解除对认证请求的阻止。您的验证将工作。
我使用过以下代码:
public void sendMail(String mailId,String outputFile){
final String username =a@gmail.com;
final String password =MyPassowrd;
属性props = new Properties();
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);
会话session = Session.getDefaultInstance(props,
new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,密码);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(a@gmail.com));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(mailId));
message.setSubject(MySubject);
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(这是邮件内容);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source =
new FileDataSource(outputFile);
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName(MyFile.pdf);
multipart.addBodyPart(messageBodyPart);
//将零件放入消息
message.setContent(multipart);
Transport.send(message);
System.out.println(Done);
} catch(MessagingException e){
throw new RuntimeException(e);
}
}
I have written the following bean to authenticate my mail.
public class Mail_Authenticator {
public Session Get_Auth() {
// sets SMTP server properties
ResourceBundle rs_mail = ResourceBundle.getBundle("mail");
final String userName=rs_mail.getString("username");
final String password=rs_mail.getString("password");
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.user", userName);
//creates a new session with an authenticator
Authenticator auth = new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
};
return (Session.getInstance(properties, auth));
}
}
The problem is with the smtp settings, when I run the application on my local server it works fine. But when I run application on openshift server exception is caused at
Transport.send(msg);
Could any one please point out the problem, in the above settings and why are they working in my local machine?
Following is the exception that i get
javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbsWq 534-5.7.14 JJjb_c-FzrtUAccdDqOCMtsPAOL1AwIDSCoireBRoI5X-avznrYbparV84O_zkAvrHXMB9 534-5.7.14 T0Zj8zXP1g1woaWHnTzJQ3vWFn3lwTNl9Kn8Ma9-d9FPD_xB-bMBSh5FEPdaMqID4WljXW 534-5.7.14 v67IfQzXHolKlY48pEiZF-cpGc6CEgknkET1ciEQf51vQuETuMrrzeC7EDcM7s89Njtm5e 534-5.7.14 crMNRLw> Please log in via your web browser and then try again. 534-5.7.14 Learn more at https://support.google.com/mail/bin/answer.py?answer=787 534 5.7.14 54 e7sm180653184qag.7 - gsmtp
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:823)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:756)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:673)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at Servlet.Mail.sendEmail(Mail.java:72)
at Servlet.Mail.doGet(Mail.java:206)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1008)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
I had a same problem. Try logging to gmail account from a browser . Gmail blocks the authentication request if he thinks is a illegal access(maybe from a different geography). You might see a message or notification. Follow the instruction in the notification or message and then you can unblock the authentication requests. Your authentication will work.
I had used following code :
public void sendMail(String mailId,String outputFile) {
final String username = "a@gmail.com";
final String password = "MyPassowrd";
Properties props = new Properties();
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");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("a@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(mailId));
message.setSubject("MySubject");
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("This is the mail content");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source =
new FileDataSource(outputFile);
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName("MyFile.pdf");
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
这篇关于Java Mail无法验证openshift服务器中的smtp设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!