<?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><!-- 本地服務器 如果是其他,請填如: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>
然后建立一個java文件
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只能用來發送text格式的郵件
try {
mail.setTo("fantlam@163.com");//接收者
mail.setFrom("sohu@mai.com");//按前面講的,可以隨便起
mail.setSubject("spring mail test!");//主題
mail.setText("springMail的簡單發送測試");//郵件內容
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服務器
props.setProperty("mail.smtp.host", "smtp.126.com");
// 現在的大部分smpt都需要驗證了
props.put("mail.smtp.auth", "true");
Session s = Session.getInstance(props);
// 為了查看運行時的信息
s.setDebug(true);
// 由郵件會話新建一個消息對象
MimeMessage message = new MimeMessage(s);
try {
// 發件人
InternetAddress from = new InternetAddress("ashutc@126.com");
message.setFrom(from);
// 收件人
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");
/****下面代碼是發送附件******
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驗證,就是你用來發郵件的郵箱用戶名密碼
transport.connect("smtp.126.com", "ashutc", "*******");
// 發送
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}