⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.java

📁 Spring发邮件
💻 JAVA
字号:
package mail;

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

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
		JavaMailSender mailSender= (JavaMailSender) context.getBean("mailSender");
		MimeMessage mime = mailSender.createMimeMessage();
		MimeMessageHelper helper;
		try {
			helper = new MimeMessageHelper(mime,true,"utf-8");
			helper.setFrom("abcd@163.com");
			helper.setTo("abcd@gmail.com");
			helper.setSubject(" 测试spring Mail的附件功能2");
			//需要将附件显示在html中
			//在标签中用cid:xx 标记,使用helper.addInline()方法添加
			helper.setText("<html><body>javaeye是个好网站:<br>"+
					"<a href='http://www.javaeye.com'>" +
					"<img src='cid:logo'></a></body></html>",true);
			helper.addInline("logo", new ClassPathResource("logo.gif"));
			helper.addAttachment("javaeye.gif", new ClassPathResource("javaeye.gif"));
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		mailSender.send(mime);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -