mail.jar spring-core.jar
application-mail.xml file
<?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-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.exmail.sina.com"></property> <property name="port" value="25"></property> <property name="username" value="*******@ssreader.cn"></property> <property name="password" value="******"></property> <property name="defaultEncoding" value="UTF-8"></property> <property name="javaMailProperties"> <props> <prop key="mail.debug">false</prop> <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> </beans>
SendMailBySpringMail.java
package test.email; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.UrlResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import javax.activation.URLDataSource; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeUtility; import java.io.File; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.util.Date; public class SendMailBySpringMail { public static JavaMailSender javaMailSender; static { javaMailSender = (JavaMailSender) new ClassPathXmlApplicationContext("application-mail.xml").getBean("javaMailSender"); } /** * Send simple text mail * SimpleMailMessage */ public static void sendSimpleMailMessage() { SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setFrom("******@ssreader.cn");//Optional mailMessage.setTo("[email protected]"); mailMessage.setSubject("Hello******"); mailMessage.setText("Just talk about it" + new Date()); javaMailSender.send(mailMessage); } /** * Send html type mail * MimeMessage */ public static void sendMimeMessage() throws MessagingException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8"); // optional, can be used to modify the name displayed to the recipient helper.setFrom("******@ssreader.cn"); helper.setTo("[email protected]"); helper.setSubject("This is an experiment"); helper.setText("<h1>你好</h1>", true); javaMailSender.send(mimeMessage); } /** * Send emails with attachments */ public static void sendMimeMessageAttachment() throws MessagingException, UnsupportedEncodingException, MalformedURLException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); //The second parameter is set to true, which means that attachments are allowed to be added MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8"); helper.setFrom("******@ssreader.cn"); helper.setTo("[email protected]"); helper.setSubject("Send email with image attachment"); //The second parameter is true to indicate that the content needs to be in HTML format helper.setText("<h1>你好</h1>", true); // need to transcode the file name helper.addAttachment(MimeUtility.encodeText("王.png"), new File("D:\\我的文档\\My Pictures\\logo.png")); helper.addAttachment(MimeUtility.encodeText("Apache网站图标.gif"), new URLDataSource(new URL("http://www.apache.org/images/asf_logo_wide.gif"))); javaMailSender.send(mimeMessage); } /** * Send emails with pictures in HTML format */ public static void sendMimeMessageInline() throws MessagingException, MalformedURLException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); //The second parameter is set to true, which means that attachments are allowed to be added MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8"); helper.setFrom("******@ssreader.cn"); helper.setTo("[email protected]"); helper.setSubject("Send an email with pictures in HTML format"); //The second parameter is true to indicate that the content needs to be in HTML format helper.setText("<h1>你好</h1><br><img src='cid:fileId'><img src='cid:abc'>", true); helper.addInline("fileId", new File("D:\\我的文档\\My Pictures\\logo.png")); helper.addInline("abc", new UrlResource("http://www.apache.org/images/asf_logo_wide.gif")); javaMailSender.send(mimeMessage); } public static void main(String[] args) throws UnsupportedEncodingException, MessagingException, MalformedURLException { // sendSimpleMailMessage(); sendMimeMessage(); // sendMimeMessageAttachment(); // sendMimeMessageInline(); } }