一个使用Spring发送邮件的问题

小鱼的梦 2011-04-15 08:49:08
我是Spring的新手,做个邮件发送遇到问题了,大家帮忙看看
代码如下:
1.beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="mailSender" class="cn.com.action.Senders">
<property name="mailSender"><ref bean="mailSend"/> </property>
</bean>
<bean id="mailSend" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.sina.com"/>
<property name="username" value="itsavour"/>
<property name="password" value="110110"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
</props>
</property>

</bean>
</beans>

2.Senders.java

package cn.com.action;

import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;

public class Senders{

private JavaMailSender mailSender;

public JavaMailSender getMailSender() {
return mailSender;
}
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void placeOrder() {

// Do the business calculations...

// Call the collaborators to persist the order...

MimeMessagePreparator preparator = new MimeMessagePreparator() {

public void prepare(MimeMessage mimeMessage) throws Exception {

mimeMessage.setRecipient(Message.RecipientType.TO,
new InternetAddress("itsavour@sohu.com"));
mimeMessage.setFrom(new InternetAddress("itsavour@sina.com"));
mimeMessage.setText("thank you for placing order. Your order number is");
}
};
try {
this.mailSender.send(preparator);
}
catch (MailException ex) {
// simply log it and go on...
System.err.println(ex.getMessage());
}
}
public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
Senders s = (Senders)cxt.getBean("mailSender");
//Senders s = new Senders();
s.placeOrder();
}
}

我在Senders.java中写了个main方法来做测验。但是运行出错啦
错误如下:
Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.sina.com, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out: connect
大家帮忙看看 谢谢啦
分数不多 只为求知 再次谢谢了
...全文
116 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
小鱼的梦 2011-04-16
  • 打赏
  • 举报
回复
sina的smtp服务器我查了 是对的 smtp.sina.com 端口新浪没要求 我用的是默认的
命令行连接sina 连接不上 怎么办??
继续求助中。。。
小鱼的梦 2011-04-16
  • 打赏
  • 举报
回复
我在命令行下输入了连接命令,结果如下:只有一行
220 irxd5-183.sinamail.sina.com.cn ESMTP
是不是没连接上啊
zhangyy130 2011-04-15
  • 打赏
  • 举报
回复
连接新浪邮件服务器smtp.sina.com, port: 25超时。先看一下sina邮件服务器是这样的吗?还有端口。
telnet 测试一下
O溺水的鱼0 2011-04-15
  • 打赏
  • 举报
回复
错误如下:
Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.sina.com, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out: connect

---------------
连接超时,在自己机器上连接这个端口看看,xp下的命令为: telnet smtp.sina.com 25
先确定自己本机是否可以访问这个地址的25端口。
如果不可以,尝试关闭下防火墙看看。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧