Spring Mail

IMailService.java

 

package com.springMail.test;

import javax.mail.MessagingException;

/**
 * 
 * @author billzhang
 *
 */
public interface IMailService {
	/**
	 * 发送邮件
	 * @throws MessagingException 
	 */
	public abstract void sendMail() throws MessagingException; 
}

 

MailServiceImpl.java

 

/**
 * 
 */
package com.springMail.test;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

/**
 * @author billzhang
 *
 */
public class MailServiceImpl implements IMailService {

	private JavaMailSenderImpl mailSender;
	
	public void setMailSender(JavaMailSenderImpl mailSender) { 
		this.mailSender = mailSender;
	}

	/* 发送邮件
	 * @see com.springMail.test.IMailService#sendMail()
	 */
	public void sendMail() throws MessagingException {
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message,true,"UTF-8");
		//
		helper.setTo("[email protected]");
		//
		helper.setFrom("[email protected]");
		//设置主题
		helper.setSubject("Spring Mail 测试成功!");
		helper.setText("<html><body color='red'>" +
				"<font size='4' color='red'> hiiiiii  iiiiii 看看我的程序。。。</font>" +
				"<a href='www.google.com'>谷歌Google</a>"+
				"</body></html>",true);
		mailSender.send(message);
		
		System.out.println("发送成功!");
	}

}

 

 mail.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
		  "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
	<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="javaMailProperties">
			<props> 
				<prop key="mail.smtp.auth">true</prop>
				<prop key="mail.transport.protocol">smtp</prop>
				<prop key="mail.host">smtp.gmail.com</prop>
				<prop key="mail.smtp.port">465</prop>
				<prop key="mail.smtp.socketFactory.port">465</prop>
				<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
				<prop key="mail.smtp.socketFactory.fallback">false</prop>
				<prop key="mail.smtp.quitwait">false</prop>
			</props>
		</property>
		<property name="username" value="shoppingbill"/>
		<property name="password" value="********"/>
	</bean>	
	
 	<bean id="mailService" class="com.springMail.test.MailServiceImpl">
 		<property name="mailSender" ref="mailSender"/>
 	</bean>
</beans>		  

 

 Main.java

/**
 * 
 */
package com.springMail.test;

import javax.mail.MessagingException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author billzhang
 *
 */
public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String path="com/springMail/test/mail.xml";
		ApplicationContext factory=new ClassPathXmlApplicationContext(path);
		IMailService mailService = (IMailService) factory.getBean("mailService");
		try {
			mailService.sendMail();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

}

 

你可能感兴趣的:(spring,bean,xml,Google,Gmail)