我试图用JavaMail发送邮件。
properties.put("mail.smtp.host", "smtp.estudiantes.ve");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.port",25);
properties.put("mail.smtp.mail.sender","miemail@estudiantes.ve");
properties.put("mail.smtp.user", "miemail@estudiantes.ve");
properties.put("mail.smtp.auth", "true");
session = Session.getDefaultInstance(properties);
session.setDebug(true);
System.setProperty("java.net.preferIPv4Stack", "true");
System.setProperty("javax.net.ssl.trustStore", "C:/Program Files/Java/jdk1.7.0_51/jre/lib/security/cacerts");
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress((String)properties.get("mail.smtp.mail.sender")));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("amigo@estudiantes.ve"));
message.setSubject("Prueba");
message.setText("Texto");
Transport t = session.getTransport("smtp");
t.connect((String)properties.get("mail.smtp.user"), "contrasenna");
t.sendMessage(message, message.getAllRecipients());
t.close();
}catch (MessagingException me){
//Aqui se deberia o mostrar un mensaje de error o en lugar
//de no hacer nada con la excepcion, lanzarla para que el modulo
//superior la capture y avise al usuario con un popup, por ejemplo.
return;
}
然而,它引发了以下异常:
javax.mail.SendFailedException: Invalid Addresses (com.sun.mail.smtp.SMTPAddressFailedException: 554 5.7.1 <cdae-jee-302-01.uci.cu[10.56.14.157]>: Client host rejected: Access denied)
at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1446)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:736)
at uci.dalasqq.vista.AlarmasBean.sendEmail(AlarmasBean.java:116)
at uci.dalasqq.vista.DashboardBean.updatepkicolors(DashboardBean.java:337)
at uci.dalasqq.vista.DashboardBean.mainConfigPKI(DashboardBean.java:661)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
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:947)
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:1009)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
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:744)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 554 5.7.1 <cdae-jee-302-01.uci.cu[10.56.14.157]>: Client host rejected: Access denied
at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1344)
... 36 more
这是如何引起的?如何解决?
答案 0 :(得分:4)
据我所知,您的代码工作正常,但您使用的邮件服务器拒绝您的邮件时出错:
554 5.7.1 <cdae-jee-302-01.uci.cu[10.56.14.157]>: Client host rejected: Access denied
也许您需要登录服务器,或者从其他主机连接?
答案 1 :(得分:1)
首先尝试从简单的命令行客户端进行测试,然后查看协议跟踪
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Test {
public static void main(String[] args) throws Exception{
System.setProperty("java.net.preferIPv4Stack", "true");
System.setProperty("javax.net.ssl.trustStore",
"C:/Program Files/Java/jdk1.7.0_51/jre/lib/security/cacerts");
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.estudiantes.ve");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.port", 25);
properties.put("mail.smtp.mail.sender", "miemail@estudiantes.ve");
properties.put("mail.smtp.user", "miemail@estudiantes.ve");
properties.put("mail.smtp.auth", "true");
Session session = Session.getInstance(properties); // do not use
// .getDefaultInstance
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress((String) properties
.get("mail.smtp.mail.sender")));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
"amigo@estudiantes.ve"));
message.setSubject("Prueba");
message.setText("Texto");
Transport t = session.getTransport("smtp");
t.connect((String) properties.get("mail.smtp.user"), "contrasenna");
message.saveChanges(); //do not forget this
t.sendMessage(message, message.getAllRecipients());
t.close();
}
}
如果它不起作用,请发布协议跟踪和/或堆栈跟踪
答案 2 :(得分:0)
此外,如果您想使用SSL,请添加
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");