需要的jar包:spring.jar mail.jar common s-logging-1.1.jar 发送端邮箱的配置: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host"> <value>smtp.163.com</value> </property> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.timeout">25000</prop> <prop key="mail.smtp.socketFactory.fallback">false</prop> <prop key="mail.smtp.starttls.enable">true</prop> <!-- 这里是配置ssl 主要是个,Gmail <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop> --> </props> </property> <property name="username"> <value>gaowm0207</value> </property> <property name="password"> <value>5342488</value> </property> <property name="port"> <value>25</value> </property> <!-- Gmail端口 <property name="port"> <value>465</value> </property> --> </bean> </beans> 发送简单邮件、带附件的邮件、html格式的邮件; package com.beckham.mail; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeUtility; import javax.servlet.ServletException; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; /** * @author Owner * Jan 20, 2010 4:51:41 PM * springMail发送邮件 * SendMail.java */ public class SendMail { public ApplicationContext ctx = null; public SendMail() { ctx = new ClassPathXmlApplicationContext("applicationContext-mail.xml");// 获取上下文 } /** * 主测试方法 * * @throws MessagingException */ public static void main(String[] args) throws ServletException, MessagingException { new SendMail().sendFileMail(); } /** * 发送简单邮件 */ public void sendMail() { JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");// 获取JavaMailSender // bean SimpleMailMessage mail = new SimpleMailMessage(); try { mail.setTo("459978392@qq.com");// 接受者 mail.setFrom("gaowm0207@163.com");// 发送者 mail.setSubject("s邮件主题");// 主题 mail.setText("springMail 的简单发送测试");// 邮件内容 sender.send(mail); System.out.println("发送完毕"); } catch (Exception e) { e.printStackTrace(); } } /** * 发送带有正文是html的邮件 * * @throws MessagingException */ public void sendHTML() throws MessagingException { JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");// 获取JavaMailSender // bean MimeMessage mm = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mm, true, "utf-8"); try { helper.setTo("459978392@qq.com");// 接受者 helper.setFrom("gaowm0207@163.com");// 发送者 helper.setSubject("邮件主题");// 主题 helper.setText("<html><head></head><body><h1>hello!!beckham</h1></body></html>",true);// 第二个参数代表发送的是正文是html sender.send(mm); System.out.println("发送完毕"); } catch (Exception e) { e.printStackTrace(); } } /** * 发送带有附件的邮件 * * @throws MessagingException */ public void sendFileMail() throws MessagingException { JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");// 获取JavaMailSender // bean MimeMessage mm = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mm, true, "utf-8"); try { helper.setTo("459978392@qq.com");// 接受者 helper.setFrom("gaowm0207@163.com");// 发送者 helper.setSubject("邮件主题");// 主题 helper.setText("邮件内容");// 邮件内容 //多个附件文件 ClassPathResource in = new ClassPathResource("附件.txt"); ClassPathResource in2 = new ClassPathResource("struts.xml"); //MimeUtility.encodeWord()解决附件的文件名为中文问题 helper.addAttachment(MimeUtility.encodeWord(in.getFilename()), in); helper.addAttachment(MimeUtility.encodeWord(in2.getFilename()), in2); sender.send(mm); System.out.println("发送完毕"); } catch (Exception e) { e.printStackTrace(); } } }