我是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.javapackage 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("[email protected]"));
mimeMessage.setFrom(new InternetAddress("[email protected]"));
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
大家帮忙看看 谢谢啦
分数不多 只为求知 再次谢谢了
代码如下:
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.javapackage 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("[email protected]"));
mimeMessage.setFrom(new InternetAddress("[email protected]"));
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
大家帮忙看看 谢谢啦
分数不多 只为求知 再次谢谢了
解决方案 »
- spring注入,但action中service为空,附代码 求帮助
- java使用jxl插件导出excel
- java连Oracle的中文乱码问题
- 求助:resin启动以后,页面报出500Servlet Exception错误。
- jboss负载均衡算法中的family概念??
- netbeans7.0.1自带的glassfish3.1只能启动一次
- ajax问题。跪求
- 急!!!!!middlegen-hibernate 生成映射文件的问题
- 面试题,高手帮我做一下?
- 请问java访问OLAP数据库的开发包在哪儿有下?
- 关于异常的捕获
- 异常了好几天了,求高人指点。(ExceptionInInitializerError Caused by: NullPointerException)
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端口。
如果不可以,尝试关闭下防火墙看看。
telnet 测试一下
220 irxd5-183.sinamail.sina.com.cn ESMTP
是不是没连接上啊
命令行连接sina 连接不上 怎么办??
继续求助中