很简单的,使用Spring提高的MailSender和MailMessage就可以了,配置代码如下: TestMail.xml
// 下面的我测试可以通过的 要变就是下面红色的地方
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" ><property name="host">
<value> mail.logicsolutions.com</value><props>
<!-- 如果要使用用户名和密码验证,这一步需要 -->
<property name="username">
<!-- 你的Email地址 是去掉了@logicsolutions.com 完整 [email protected] -->
<property name="password">
<!--你的Email密码 -->
</bean>
<!-- 简单的message -->
<property name="to">
<!-- 收件人地址-->
<property name="from">
<!-- 你的地址-->
</beans>
////////////////////
上面的配置好以后就可以直接发送了, 看看TestSenderMail.java的代码:
package com.budco.bmp.core.email;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
public class TestSenderMail {
private MailSender mailSender;
private SimpleMailMessage mailMessage;
public TestSenderMail() {
}
public SimpleMailMessage getMailMessage() {
return mailMessage;
}
public void setMailMessage(final SimpleMailMessage mailMessage) {
this.mailMessage = mailMessage;
}
public MailSender getMailSender() {
return mailSender;
}
public void setMailSender(final MailSender mailSender) {
this.mailSender = mailSender;
}
public void sendMail() {
// set up email contents,
final SimpleMailMessage message = new SimpleMailMessage(mailMessage);
message.setText("Test Spring Send Email...............");
try {
mailSender.send(message);
} catch (final MailException e) {
System.out.println(e.getMessage() + ",O . Send Email failure....");
e.printStackTrace();
}
}
}
//
很简单吧. 下面是测试类: TestApp.java
package com.budco.bmp.core.email;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestApp {
// activation.jar AND mail.jar import
public static void main(final String[] args) {
final ApplicationContext context = new ClassPathXmlApplicationContext("com/budco/bmp/core/email/TestMail.xml");
final TestSenderMail sender = (TestSenderMail) context.getBean("testMailSender");
sender.sendMail();
}
}
最后提醒一下,记得要将:activation.jar 和 mail.jar 两个包导入到工程