閰嶇疆浠g爜閮借秴榪囦簡涓諱唬鐮?nbsp;
涓嶈繃閰嶇疆浠g爜閮芥槸甯哥敤鐨勶紝鐣欑潃澶囦喚鍚с?br />mavne pox 閰嶇疆
1 <dependency>
2 <groupId>javax.mail</groupId>
3 <artifactId>mail</artifactId>
4 <version>1.4</version>
5 </dependency>
6 <dependency>
7 <groupId>org.springframework</groupId>
8 <artifactId>spring-beans</artifactId>
9 <version>${org.springframework.version}</version>
10 </dependency>
spring-xml 閰嶇疆
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:cfg/config.properties</value>
</list>
</property>
</bean>
<bean id="mail"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<!-- SMTP鍙戦侀偖浠剁殑鏈嶅姟鍣ㄧ殑IP鍜岀鍙?nbsp;-->
<property name="host" value="${mail.host}" />
<property name="port" value="${mail.port}" />
<!-- 鐧婚檰SMTP閭歡鍙戦佹湇鍔″櫒鐨勭敤鎴峰悕鍜屽瘑鐮?nbsp;-->
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />
<!-- 鑾峰緱閭歡浼?xì)璇濆睘鎬?楠岃瘉鐧誨綍閭歡鏈嶅姟鍣ㄦ槸鍚︽垚鍔?/span>-->
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="prop">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
</bean>
properties 鏂囦歡閰嶇疆
mail.host=smtp.yeah.net
mail.port=25
mail.username=****
mail.password=****
java 綾誨垎鍒負(fù)錛?br />
import org.springframework.context.support.AbstractApplicationContext;
import com.ms.AppContext;
public class SpringHelper {
/**
* 鑾峰彇spring渚濊禆娉ㄥ叆鐨勫璞?br /> *
* @param name
* @return Object Bean
*/
public static Object getBean(String name) {
AbstractApplicationContext ctx = AppContext.getInstance()
.getAppContext();
return ctx.getBean(name);
}
}
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppContext {
private static AppContext instance;
private volatile AbstractApplicationContext appContext;
public synchronized static AppContext getInstance() {
if (instance == null) {
instance = new AppContext();
}
return instance;
}
private AppContext() {
List<String> list = new ArrayList<String>();
list.add("/cfg/*.xml");
String ss[] = list.toArray(new String[] {});
for (int i = 0; i < ss.length; i++) {
System.out.println("ss[" + i + "]" + ss[i]);
}
this.appContext = new ClassPathXmlApplicationContext(ss);
}
public AbstractApplicationContext getAppContext() {
return appContext;
}
}
import java.io.File;
import java.util.Date;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import com.ms.SpringHelper;
import com.util.Configuration;
/**
* 鍙戦侀偖浠?nbsp;宸ュ叿
*
* @author
* @file com.ms.util --- SentMaileUtil.java
* @version 2013-2-28 -涓嬪崍03:42:03
*/public class SendMaileUtil {
private static JavaMailSender javaMailSender;
private static Logger logger = Logger.getLogger(SendMaileUtil.
class);
private static JavaMailSender newIntstance() {
if (javaMailSender ==
null) {
javaMailSender = (JavaMailSender) SpringHelper.getBean("mail");
}
return javaMailSender;
}
/**
* 鍙戦佺殑鏂囨湰嫻嬭瘯閭歡
*
* @param to
* @param mailSubject
* @param mailBody
*/ public static void sendTextMaile(String to, String mailSubject,
String mailBody) {
if (logger.isDebugEnabled())
logger.debug("鍑嗗鍙戦佹枃鏈艦寮忕殑閭歡

");
SimpleMailMessage mail1 =
new SimpleMailMessage();
String from = Configuration.getValue("mail.form");
mail1.setFrom(from);
// 鍙戦佷漢鍚嶇墖
mail1.setTo(to);
// 鏀朵歡浜洪偖綆?/span>
mail1.setSubject(mailSubject);// 閭歡涓婚
mail1.setSentDate(new Date());// 閭歡鍙戦佹椂闂?/span>
mail1.setText(mailBody);
// 緹ゅ彂
SimpleMailMessage[] mailMessages = { mail1 };
newIntstance().send(mailMessages);
if (logger.isDebugEnabled())
logger.debug("鏂囨湰褰㈠紡鐨勯偖浠跺彂閫佹垚鍔燂紒錛侊紒");
}
/**
* 浠?nbsp;HTML鑴氭湰褰㈠紡閭歡鍙戦?br /> *
* @param to
* @param mailSubject
* @param mailBody
*/
public static void sendHtmlMail(String to, String mailSubject,
String mailBody) {
JavaMailSender mailSender = newIntstance();
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
if (logger.isDebugEnabled())
logger.debug("HTML鑴氭湰褰㈠紡閭歡姝e湪鍙戦?img src="http://www.tkk7.com/Images/dot.gif" alt="" />");
// 璁劇疆utf-8鎴朑BK緙栫爜錛屽惁鍒欓偖浠朵細(xì)鏈変貢鐮?/span>
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true,
"UTF-8");
// 璁劇疆鍙戦佷漢鍚嶇墖
String from = Configuration.getValue("mail.form");
helper.setFrom(from);
// 璁劇疆鏀朵歡浜哄悕鐗囧拰鍦板潃
helper.setTo(new InternetAddress("\""
+ MimeUtility.encodeText("gamil閭") + "\" <" + to + ">"));// 鍙戦佽?br /> // 閭歡鍙戦佹椂闂?/span>
helper.setSentDate(new Date());
// 璁劇疆鍥炲鍦板潃
helper.setReplyTo(new InternetAddress(from));
// 璁劇疆鎶勯佺殑鍚嶇墖鍜屽湴鍧
// helper.setCc(InternetAddress.parse(MimeUtility.encodeText("鎶勯佷漢001")
// + " <
@163.com>," + MimeUtility.encodeText("鎶勯佷漢002")
// + " <
@foxmail.com>"));
// 涓婚
helper.setSubject("轂旐敿鞏胳壗");
// 閭歡鍐呭錛屾敞鎰忓姞鍙傛暟true錛岃〃紺哄惎鐢╤tml鏍煎紡
helper
.setText(
"<html><head></head><body><h1>hello!!鎴戞槸涔斿竷鏂?lt;/h1></body></html>",
true);
// 鍙戦?/span>
mailSender.send(mimeMessage);
} catch (Exception e) {
e.printStackTrace();
}
if (logger.isDebugEnabled())
logger.debug("HTML鑴氭湰褰㈠紡閭歡鍙戦佹垚鍔燂紒錛侊紒");
}
/**
* 浠ラ檮浠剁殑褰㈠紡鍙戦侀偖浠?br /> *
* @param to
* 鏀朵歡浜篹amil 鍦板潃
* @param toName
* 鏀朵歡浜烘樀縐?br /> * @param mailSubject
* 涓婚
* @param mailBody
* 鍐呭浣?br /> * @param files
* 闄勪歡
*/
public static void sendFileMail(String to, String toName,
String mailSubject, String mailBody, File[] files) {
JavaMailSender mailSender = newIntstance();
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
if (logger.isDebugEnabled())
logger.debug("甯﹂檮浠跺拰鍥劇墖鐨勯偖浠舵鍦ㄥ彂閫?img src="http://www.tkk7.com/Images/dot.gif" alt="" />");
// 璁劇疆utf-8鎴朑BK緙栫爜錛屽惁鍒欓偖浠朵細(xì)鏈変貢鐮?/span>
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true,
"UTF-8");
// 璁劇疆鍙戦佷漢鍚嶇墖
String from = Configuration.getValue("mail.form");
helper.setFrom(from);
// 璁劇疆鏀朵歡浜洪偖綆?/span>
helper.setTo(new InternetAddress("\""
+ MimeUtility.encodeText(toName) + "\" <" + to + ">"));
// 璁劇疆鍥炲鍦板潃
// helper.setReplyTo(new InternetAddress("
@qq.com"));
// 璁劇疆鏀朵歡浜烘妱閫佺殑鍚嶇墖鍜屽湴鍧(鐩稿綋浜庣兢鍙戜簡)
// helper.setCc(InternetAddress.parse(MimeUtility.encodeText("閭001")
// + " <
@163.com>," + MimeUtility.encodeText("閭002")
// + " <
@foxmail.com>"));
// 涓婚
helper.setSubject(mailSubject);
// 閭歡鍐呭錛屾敞鎰忓姞鍙傛暟true錛岃〃紺哄惎鐢╤tml鏍煎紡
helper.setText(mailBody);
if (files != null && files.length > 0) {
for (int i = 0; i < files.length; i++)
// 鍔犲叆闄勪歡
helper.addAttachment(MimeUtility.encodeText(files[i]
.getName()), files[i]);
}
// 鍔犲叆鎻掑浘
helper.addInline(MimeUtility.encodeText("pic01"), new File(
"c:/temp/2dd24be463.jpg"));
// 鍙戦?/span>
mailSender.send(mimeMessage);
} catch (Exception e) {
e.printStackTrace();
}
if (logger.isDebugEnabled()) {
logger.debug("甯﹂檮浠跺拰鍥劇墖鐨勯偖浠跺彂閫佹垚鍔燂紒錛侊紒");
}
}
public static void main(String[] args) {
PropertyConfigurator.configure(ClassLoader
.getSystemResource("cfg/log4j.properties"));
SendMaileUtil.sendTextMaile("*****@gmail.com",
"Spring Mail 嫻嬭瘯閭歡", "Hello,Boy,This is my Spring Mail,鍝堝搱錛侊紒");
SendMaileUtil.sendHtmlMail("*****@gmail.com", null, null);
File file = new File("c:/temp");
File[] fs = file.listFiles();
SendMaileUtil.sendFileMail("******@yeah.net", "鏄電О", "涓婚", "鍐呭",
fs);
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.pub.Forward;
/**
* 璇誨彇properties鏂囦歡
* @author
*
*/public class Configuration
{
private static Properties propertie;
private InputStream in;
private static Configuration config =
new Configuration();
/**
* 鍒濆鍖朇onfiguration綾?br /> */ public Configuration()
{
propertie =
new Properties();
try {
// System.out.println(System.getProperty("user.dir"));
// inputFile = new FileInputStream("cfg/config.properties");
in = ClassLoader.getSystemResourceAsStream("cfg/config.properties");
propertie.load(in);
in.close();
}
catch (FileNotFoundException ex) {
System.out.println("璇誨彇灞炴ф枃浠?-->澶辮觸錛? 鍘熷洜錛氭枃浠惰礬寰勯敊璇垨鑰呮枃浠朵笉瀛樺湪");
ex.printStackTrace();
}
catch (IOException ex) {
System.out.println("瑁呰澆鏂囦歡--->澶辮觸!");
ex.printStackTrace();
}
}
/**
* 閲嶈澆鍑芥暟錛屽緱鍒発ey鐨勫?br /> * @param key 鍙栧緱鍏跺肩殑閿?br /> * @return key鐨勫?br /> */ public static String getValue(String key)
{
if(propertie.containsKey(key)){
String value = propertie.getProperty(key);
//寰楀埌鏌愪竴灞炴х殑鍊?/span>
return value;
}
else
return "";
}//end getValue(
)
public static void main(String[] args)
{
System.out.println(Configuration.getValue("aaa"));
System.out.println(System.getProperty("user.dir"));
}//end main()
}//end class ReadConfigInfo

]]>