我正在使用Eclipse IDE中的弹簧邮件尝试发送电子邮件,但是控制台总是打印出"连接超时"例外。我已经确认可以通过telnet获得与邮件服务器的连接。我对Spring MVC框架很新。也许是因为我没有配置spring-mvc.xml吗?不过,我没有在代码中使用依赖注入。以下是代码和例外。谢谢您的任何有见地的评论。
package xxx.web.controller;
import java.io.File;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.core.io.FileSystemResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
@Controller
public class SendMailController {
private JavaMailSenderImpl mailSender;
@RequestMapping(value ="/sendMail", method = RequestMethod.GET)
public void sendMail(HttpServletRequest request) throws MessagingException{
mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.sina.com"); //configure mail server
mailSender.setUsername("**********@sina.com");
mailSender.setPassword("*******");
MimeMessage msg = mailSender.createMimeMessage();
MimeMessageHelper msgHelper = new MimeMessageHelper(msg, true, "utf-8");
msgHelper.setTo("********@qq.com");
msgHelper.setFrom("*********@sina.com");
msgHelper.setSubject("Testing Subject");
msgHelper.setText("This is a test mail!");
FileSystemResource file = new FileSystemResource(new File("D:/test.png"));
msgHelper.addAttachment("test.png", file); //add an attachment
Properties prop = new Properties();
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.timeout", "25000");
mailSender.setJavaMailProperties(prop);
mailSender.send(msg);
System.out.println("Email sent successfully!");
}
}
在控制台中打印出的例外:
消息请求处理失败;嵌套异常是org.springframework.mail.mailsendexception:邮件服务器连接失败;嵌套异常是com.sun.mail.util.mailConnectException:**
无法连接到主机,端口:smtp.sina.com,25;超时-1;
**
描述服务器遇到的内部错误,阻止其满足此请求。
例外
org.springframework.web.util.nestedservletexception:请求处理失败;嵌套异常是org.springframework.mail.mailsendexception:邮件服务器连接失败;嵌套异常是com.sun.mail.util.mailconnectexception:无法连接到主机,端口:smtp.sina.com,25;超时-1; 嵌套的例外是: java.net.connectException:连接时计时:连接。失败消息:com.sun.mail.util.mailconnectException:无法连接到主机,端口:smtp.sina.com,25;超时-1; 嵌套的例外是: java.net.connectException:连接时计时:连接;消息例外(1)是:失败消息1:com.sun.mail.util.mailConnectException:无法连接到主机,端口:smtp.sina.com,25;超时-1; 嵌套的例外是: java.net.connectException:连接时计时:连接 org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:948) org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:827) javax.servlet.http.httpservlet.service(httpservlet.java:620) org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:812) javax.servlet.http.httpservlet.service(httpservlet.java:727) org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) org.springframework.web.filter.characterencodingfilter.dofilterinternal(targeenCodingFilter.java:88) org.springframework.web.filter.onceperrequestfilter.dofilter(asherrequestfilter.java:107)
首先,您必须确保您的连接还可以;为了检查它,打开命令行并执行以下命令:
telnet <yourservername> smtp
HELO <yourservername and then enter>
MAIL from: <mailaddr and then enter>
RCPT to: <mailaddr and then enter>
DATA
354 End data with <CR><LF>.<CR><LF>
From: <mailaddr and then enter>
To: <mailaddr and then enter>
Subject: test
This is test
.
250 2.0.0 Ok: queued as 4C67244003
QUIT <and then enter>
221 2.0.0 Bye
Connection closed by foreign host.
如果一切正常,则是春季问题,或者您需要在邮件服务器上进行身份验证(或更简单的邮件服务器不允许在自己的网络之外进行连接)
angelo