本文共 4676 字,大约阅读时间需要 15 分钟。
百度找了半天发现问题关键在于使用固定的格式来填充From属性,如下:
代码如下
// 设置收件人,寄件人
String nick = javax.mail.internet.MimeUtility.encodeText("您的昵称");
messageHelper.setFrom(new InternetAddress(nick + " "));
messageHelper.setTo(toMail);
messageHelper.setSubject(subject);
全部实例代码
spring 对mail的支持是org.springframework.mail.MailSender接口来定义的,其中的 org.springframework.mail.javamail.JavamailSenderImpl这个实现类提供了对javamail的支持,下面简单介绍其用法,首先看下不用注入的方法,再看用注入的方法。
1、发送简单邮件的例子
首先,我们要需要springframwork 1.2.8中lib目录下的j2ee目录的activation.jar和mail.jar文件,将它们放到工程的lib目录下去。
发送一个简单邮件的例子如下
代码如下
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.SimpleMailMessage;
public class SimpleMailDemo {
public static void main(String[] args) throws Exception {
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
// 设定 Mail Server
senderImpl.setHost("mail.xxxxx");
//SMTP验证时,需要用户名和密码
senderImpl.setUsername("abc");
senderImpl.setPassword("defdf");
// 建立邮件讯息
SimpleMailMessage mailMessage = new SimpleMailMessage();
// 设定收件人、寄件人、主题与内文
mailMessage.setTo(hello@abc.com);
mailMessage.setFrom(der@def.com);
mailMessage.setSubject("Test");
mailMessage.setText("This is a test!!!");
// 传送邮件
senderImpl.send(mailMessage);
System.out.println("邮件传送OK..");
}
}
2、发送HTML格式的邮件
要发送HTML格式的话,要用
代码如下
org.springframework.mail.javamail.MimeMessageHelper来建立HTML邮件
import org.springframework.mail.javamail.JavaMailSenderImpl;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
public class HTMLMailDemo {
public static void main(String[] args) throws Exception {
JavaMailSenderImpl senderImpl =
new JavaMailSenderImpl();
// 设定 Mail Server
senderImpl.setHost("mail.xxxxx");
//SMTP验证时,需要用户名和密码
senderImpl.setUsername("abc");
senderImpl.setPassword("defdf");
// 建立邮件讯息
MimeMessage mailMessage =
senderImpl.createMimeMessage();
MimeMessageHelper messageHelper =
new MimeMessageHelper(mailMessage);
// 设定收件人、寄件人、主题与内文
messageHelper.setTo("xxx@your_mail_server.com");
messageHelper.setFrom("xxx@your_mail_server.com");
messageHelper.setSubject("Test");
messageHelper.setText(
"
Hello! Spring!"
+ "
", true);
// 传送邮件
senderImpl.send(mailMessage);
System.out.println("邮件传送OK...");
}
}
3、发送带附件的邮件
这时要用
代码如下
MimeMessageHelper的addAttachement()方法
import java.io.File;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import javax.mail.internet.MimeMessage;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.MimeMessageHelper;
public class AttachedFileDemo {
public static void main(String[] args) throws Exception {
JavaMailSenderImpl senderImpl =
new JavaMailSenderImpl();
//SMTP验证时,需要用户名和密码
senderImpl.setUsername("abc");
senderImpl.setPassword("defdf");
MimeMessage mailMessage = senderImpl.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true);
messageHelper.setTo("def@erer.net");
messageHelper.setFrom("bfg@tom.com");
messageHelper.setSubject("Test");
messageHelper.setText(
"
Hello! Spring!"
+ "
", true);
FileSystemResource file =
new FileSystemResource(
new File("d:/test.rar"));
messageHelper.addAttachment("test.rar", file);
senderImpl.send(mailMessage);
System.out.println("OK");
}
}
4、用注入的方式来配置邮件,和之前的差不多拉
smtp.xxx.com
true
你的Email地址
你的Email密码
收件人地址
你的地址
A Spring Mail sender
上面的配置好以后就可以直接发送了, 看看TestSenderMail.java的代码:
代码如下
package test.mail;
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(SimpleMailMessage mailMessage) {
this.mailMessage = mailMessage;
}
public MailSender getMailSender() {
return mailSender;
}
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public void sendMail() {
SimpleMailMessage message = new SimpleMailMessage(mailMessage);
//设置email内容,
message.setText("测试Spring 发送Email.");
try {
mailSender.send(message);
} catch (MailException e) {
// TODO Auto-generated catch block
System.out.println("O . 发送Email失败了....");
e.printStackTrace();
}
}
}
TestApp.java
代码如下
package test.mail;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext(
"test/mail/mail.xml");
TestSenderMail sender = (TestSenderMail) context
.getBean("testMailSender");
sender.sendMail();
}
}
转载地址:http://lhqhp.baihongyu.com/