<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host">
<value>127.0.0.1</value><!-- 鏈湴鏈嶅姟鍣?nbsp; 濡傛灉鏄叾浠栵紝璇峰~濡傦細smtp.sohu.com-->
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
<property name="username">
<value>postmaster@mai.com</value> <!-- 鎴戣繖閲岀敤鏈湴鐨勯偖綆卞悕-->
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
</beans>
鐒跺悗寤虹珛涓涓猨ava鏂囦歡
package org.fantlam.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
public class SpringMail1 {
public static void main(String args[]){
ApplicationContext ctx =new ClassPathXmlApplicationContext("applicationContext.xml");
JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");
SimpleMailMessage mail = new SimpleMailMessage();
//榪欓噷SimpleMailMessage鍙兘鐢ㄦ潵鍙戦乼ext鏍煎紡鐨勯偖浠?/span>
try {
mail.setTo("fantlam@163.com");//鎺ユ敹鑰?nbsp;
mail.setFrom("sohu@mai.com");//鎸夊墠闈㈣鐨勶紝鍙互闅忎究璧?/span>
mail.setSubject("spring mail test!");//涓婚
mail.setText("springMail鐨勭畝鍗曞彂閫佹祴璇?/span>");//閭歡鍐呭
sender.send(mail);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
/**
* @param args
*/
public static void main(String[] args) {
Properties props = System.getProperties();
// 璁劇疆smtp鏈嶅姟鍣?nbsp;
props.setProperty("mail.smtp.host", "smtp.126.com");
// 鐜板湪鐨勫ぇ閮ㄥ垎smpt閮介渶瑕侀獙璇佷簡
props.put("mail.smtp.auth", "true");
Session s = Session.getInstance(props);
// 涓轟簡鏌ョ湅榪愯鏃剁殑淇℃伅
s.setDebug(true);
// 鐢遍偖浠朵細璇濇柊寤轟竴涓秷鎭璞?nbsp;
MimeMessage message = new MimeMessage(s);
try {
// 鍙戜歡浜?nbsp;
InternetAddress from = new InternetAddress("ashutc@126.com");
message.setFrom(from);
// 鏀朵歡浜?nbsp;
InternetAddress to = new InternetAddress("ashutc@126.com");
message.setRecipient(Message.RecipientType.TO, to);
// 閭歡鏍囬
message.setSubject("test");
String content = "嫻嬭瘯鍐呭";
// 閭歡鍐呭,涔熷彲浠ヤ嬌綰枃鏈?text/plain"
message.setContent(content, "text/html;charset=GBK");
/****涓嬮潰浠g爜鏄彂閫侀檮浠?*****
String fileName = "d:\\hello.txt";
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Hi");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileName);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);*/
message.saveChanges();
Transport transport = s.getTransport("smtp");
// smtp楠岃瘉錛屽氨鏄綘鐢ㄦ潵鍙戦偖浠剁殑閭鐢ㄦ埛鍚嶅瘑鐮?nbsp;
transport.connect("smtp.126.com", "ashutc", "*******");
// 鍙戦?nbsp;
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

]]>