发送邮件功能

最近看了maven实战里面写的一个发送邮件的功能,以为很牛了,结果,spring本身就提供这样的方法,这个spring太强大了
<?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:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
	<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host">
			<value>smtp.qq.com</value>
		</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>[email protected]</value>
		</property>
		<property name="password">
			<value>XXX</value>
		</property>
<!-- 	<property name="host" value="smtp.gmail.com" /> -->
<!-- 	<property name="username" value="[email protected]"/> -->
<!-- 	<property name="password" value="XXXX" /> -->
<!-- 	<property name="javaMailProperties"> -->
<!-- 		<props> -->
<!-- 			<prop key="mail.smtp.auth">true</prop> -->
<!-- 			<prop key="mail.smtp.timeout">25000</prop> -->
<!-- 			<prop key="mail.smtp.port">465</prop> -->
<!-- 			<prop key="mail.smtp.socketFactory.port">465</prop> -->
<!-- 			<prop key="mail.smtp.socketFactory.fallback">false</prop> -->
<!-- 			<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop> -->
<!-- 		</props> -->
<!-- 	</property> -->
	</bean>
</beans>


弄好之后,我们需要测试一下,新建测试用例
public class TestSendmail {

	@Test
	public void testSendMailSimple(){
		ApplicationContext context = new ClassPathXmlApplicationContext("application-commons.xml");
		JavaMailSender mailSend = (JavaMailSender) context.getBean("mailSender");
		SimpleMailMessage mail = new SimpleMailMessage();
		
		mail.setText("[email protected]");
		mail.setFrom("[email protected]");
		mail.setTo("[email protected]");
		mail.setSubject("嘎嘎");
		mail.setText("oooooooooo");
		mailSend.send(mail);
	}
	
	@Test
	public void testSendMailHtml() throws MessagingException{
		ApplicationContext context = new ClassPathXmlApplicationContext("application-commons.xml");
		JavaMailSender mailSend = (JavaMailSender) context.getBean("mailSender");
		JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
		MimeMessage mailMsg = senderImpl.createMimeMessage();
		MimeMessageHelper msgHelper = new MimeMessageHelper(mailMsg, true, "UTF-8");
		msgHelper.setText("[email protected]");
		msgHelper.setFrom("[email protected]");
		msgHelper.setTo("[email protected]");
		msgHelper.setSubject("嘎嘎");
		msgHelper.setText("<html><head></head><body><h1>hello!!chao.wang</h1></body></html>",true);
		mailSend.send(mailMsg);
	}
	
	@Test
	public void testSendMailHtmlAndFile() throws MessagingException, UnsupportedEncodingException{
		ApplicationContext context = new ClassPathXmlApplicationContext("application-commons.xml");
		JavaMailSender mailSend = (JavaMailSender) context.getBean("mailSender");
		JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
		MimeMessage mailMsg = senderImpl.createMimeMessage();
		MimeMessageHelper msgHelper = new MimeMessageHelper(mailMsg, true, "UTF-8");
		msgHelper.setText("[email protected]");
		msgHelper.setFrom("[email protected]");
		msgHelper.setTo("[email protected]");
		msgHelper.setSubject("嘎嘎");
		msgHelper.setText("<html><head></head><body><h1>hello!!chao.wang</h1></body></html>",true);
		
		msgHelper.addInline("好吧", new File("D:/mvc.log"));
		File file = new File("d:/我的文档/Downloads/src.rar");
		msgHelper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
		mailSend.send(mailMsg);
	}
	
}

结果为正常

你可能感兴趣的:(发送邮件)