我试图对此问题进行了大量的解决,现在作为最后的手段在此处发布。请帮帮我!
问题:我可以通过从main方法调用类来发送邮件。当我尝试从Struts2 Action类调用同一个类时,它不发送电子邮件,而是给出以下错误。我正在使用Jboss EAP 7.0.0.Alpha1。我认为问题可能在于JBoss配置。我还更改了Standalone-full.xml文件。(注意我使用的是standalone-full.xml,而我的其他Web组件工作正常)。 我确实在调试模式下运行它,并看到所有值都已填充。当我们从struts2动作类运行它时,它不会发送消息。
对Standalone-full.xml所做的更改:
<mail-session name="java:jboss/mail/Default"
from="somedummyuser@gmail.com" jndi-
name="java:jboss/mail/Default">
<smtp-server password="******" username="somedummyuser"
ssl="true" outbound-socket-binding-ref="mail-smtp"/>
</mail-session>
</subsystem>
<outbound-socket-binding name="mail-smtp">
<remote-destination host="smtp.gmail.com" port="465"/>
</outbound-socket-binding>
Java代码:
public class SendEmail {
private MailSender mailSender;
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public void sendMail(String from, String to, String subject, String
msg)
throws Exception,NamingException{
//creating message
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(from);
simpleMailMessage.setTo(to);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(msg);
//sending message
System.out.println("SimpleMailMessage object:"+simpleMailMessage);
mailSender.send(simpleMailMessage);
}
}
使用MainMethod的类正在执行上述代码findE并成功发送电子邮件:
public class MainMethod_SendEmail {
public static void main(String[] args) throws Exception{
Resource r=new ClassPathResource("webApplicationContext.xml");
BeanFactory b=new XmlBeanFactory(r);
SendEmail m=(SendEmail)b.getBean("sendEmail");
String sender = AccessPropertiesUtil.getInstance().getProperty("from");
String receiver = AccessPropertiesUtil.getInstance().getProperty("to");
String subject =
AccessPropertiesUtil.getInstance().getProperty("subject");
String message =
AccessPropertiesUtil.getInstance().getProperty("message");
m.sendMail(sender, receiver, subject, message);
System.out.println("success");
}
}
但是没有从Struts2 Action类执行相同的邮件发送代码: Struts2Action类:
public String execute()
{
emailSending();
}
private void emailSending() throws Exception
{
System.out.println("Sending Email");
String sender =
AccessPropertiesUtil.getInstance().getProperty("from");
String receiver =
AccessPropertiesUtil.getInstance().getProperty("to");
String subject =
AccessPropertiesUtil.getInstance().getProperty("subject");
String message =
AccessPropertiesUtil.getInstance().getProperty("message");
sendEmail.sendMail(sender, receiver, subject, message);
}
Error Trace:
org.springframework.mail.MailSendException: Mail server connection
failed;
nested exception is javax.mail.MessagingException: Could not
connect to SMTP host: smtp.gmail.com, port: 465, response: -1. Failed
messages: javax.mail.MessagingException: Could not connect to SMTP
host: smtp.gmail.com, port: 465, response: -1; message exception details
(1) are:
2016-07-13 02:45:33 ERROR stderr:71 - Failed message 1:
2016-07-13 02:45:33 ERROR stderr:71 - javax.mail.MessagingException:
Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
2016-07-13 02:45:33 ERROR stderr:71 - at
com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2042)
2016-07-13 02:45:33 ERROR stderr:71 - at
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
2016-07-13 02:45:33 ERROR stderr:71 - at
javax.mail.Service.connect(Service.java:364)