<?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><!-- 本地服務(wù)器 如果是其他,請(qǐng)?zhí)钊纾簊mtp.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>
然后建立一個(gè)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只能用來(lái)發(fā)送text格式的郵件
try {
mail.setTo("fantlam@163.com");//接收者
mail.setFrom("sohu@mai.com");//按前面講的,可以隨便起
mail.setSubject("spring mail test!");//主題
mail.setText("springMail的簡(jiǎn)單發(fā)送測(cè)試");//郵件內(nèi)容
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();
// 設(shè)置smtp服務(wù)器
props.setProperty("mail.smtp.host", "smtp.126.com");
// 現(xiàn)在的大部分smpt都需要驗(yàn)證了
props.put("mail.smtp.auth", "true");
Session s = Session.getInstance(props);
// 為了查看運(yùn)行時(shí)的信息
s.setDebug(true);
// 由郵件會(huì)話新建一個(gè)消息對(duì)象
MimeMessage message = new MimeMessage(s);
try {
// 發(fā)件人
InternetAddress from = new InternetAddress("ashutc@126.com");
message.setFrom(from);
// 收件人
InternetAddress to = new InternetAddress("ashutc@126.com");
message.setRecipient(Message.RecipientType.TO, to);
// 郵件標(biāo)題
message.setSubject("test");
String content = "測(cè)試內(nèi)容";
// 郵件內(nèi)容,也可以使純文本"text/plain"
message.setContent(content, "text/html;charset=GBK");
/****下面代碼是發(fā)送附件******
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驗(yàn)證,就是你用來(lái)發(fā)郵件的郵箱用戶名密碼
transport.connect("smtp.126.com", "ashutc", "*******");
// 發(fā)送
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}