JavaMail API为Java提供了邮件发送和接受服务,支持常见的邮件协议 SMTP IMAP POP3,
发送纯文本邮件
public class Main {
public static void main(String[] args) throws AddressException,
MessagingException {
// 直接使用JavaMail API:
Properties props = new Properties();
// 设置SMTP服务器:
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
// 使用SSL安全连接:
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
send(props, "livebookstore", // Username
"LiVe-BoOkStOrE", // Password
"livebookstore@gmail.com", // From
new String[] { "livebookstore2@gmail.com" }, // To
"A test mail", // Subject
"测试使用JavaMail API发送邮件" // Text
);
}
public static void send(Properties props, final String username,
final String password, String from, String[] to, String subject,
String text) throws AddressException, MessagingException {
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setSubject(subject);
message.setText(text);
message.setSentDate(new Date());
Address[] addressTo = new Address[to.length];
for (int i = 0; i < to.length; i++) {
addressTo[i] = new InternetAddress(to[i]);
}
message.setRecipients(Message.RecipientType.TO, addressTo);
message.saveChanges();
Transport.send(message, addressTo);
}
}
Spring提供了非常方便的Mail抽象层,它通过一个
MailSender接口,封装了邮件发送Bean,
而
SimpleMailMessage封装了纯文本的简单邮件
把发送邮件的STMP配置注入Bean中
<!-- 配置MailSender -->
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="465" />
<property name="username" value="livebookstore" />
<property name="password" value="LiVe-BoOkStOrE" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtp.socketFactory.class">
javax.net.ssl.SSLSocketFactory
</prop>
</props>
</property>
</bean>
// 使用Spring提供的MailSender:
ApplicationContext context = new ClassPathXmlApplicationContext(
"config.xml");
JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender");
sentBySpring(mailSender);
private static void sentBySpring(JavaMailSender mailSender) {
// 创建一个纯文本邮件:
SimpleMailMessage mail = new SimpleMailMessage();
mail.setFrom("livebookstore@gmail.com");
mail.setTo("livebookstore2@gmail.com");
mail.setSubject("Another test mail");
mail.setText("测试使用Spring MailSender发送邮件");
// 发送:
mailSender.send(mail);
}
发送Mime邮件
Spring提供助手类MimeMessageHepler,能方便的操作MimeMessage
private static void sentMime(JavaMailSender mailSender)
throws MessagingException {
// 发送Mime邮件:
MimeMessage mime = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mime, true, "UTF-8");
helper.setFrom("livebookstore@gmail.com");
helper.setTo("livebookstore2@gmail.com");
helper.setSubject("Test mime mail");
// 设定为HTML格式:
helper.setText("<html><body>访问Live在线书店:<br>"
+ "<a href='http://www.livebookstore.net' target='_blank'>"
+ "<img src='cid:logo'></a></body></html>", true);
// 添加附件嵌入HTML中显示,在img标签中用"cid:xxx",
// 对应附件通过addInline()添加
helper.addInline("logo", new ClassPathResource("logo.gif"));
helper.addAttachment("freebsd.gif", new ClassPathResource("freebsd.gif"));
// 发送:
mailSender.send(mime);
}
分享到:
相关推荐
java 发送邮件 spring发送邮件Mail
利用spring下的一个类进行邮件发送,内附具体用法
用spring发送邮件,该功能在一般系统监控中经常用到。
spring发送邮件所需jar包
Spring实现各种邮件的发送 简单配置就可发送邮件
包含了spring发送邮件的说明文档和代码案例以及图解。
Spring 封装的JavaMail,使邮件发送变的相当简单,我通过Spring的邮件发送实现了发纯文本形的内容,HTMl,带附件,带图片等示例代码,并自行测试过.希望对初学者有帮助
使用springmvc 调用.JavaMailSenderImpl 发送邮件
本人测试过,完全可运行,里面也有相关异常说明,ps:不是代码问题,呵呵
Spring Boot整合邮件发送并保存历史发送邮箱 项目描述 项目主要是使用 Spring Boot 发送邮件,主要的技术点有: 1、Spring Boot +mybatis的整合 2、Spring Boot项目中jsp的使用 3、Spring Boot 发送邮件...
使用Spring Boot 开发支持多附件邮件发送微服务平台代码
数据库:sqlserver CreateHtmlTable中的生成路径和SendService中的上传路径改成自己的
它包括了发送电子邮件的主要接口MailSender,和值对象SimpleMailMessage,它封装了简单邮件的属性如from, to,cc, subject,text。 包里还包含一棵以MailException为根的checked Exception继承树,它们提供了对底层...
Spring整合Quartz定时发送邮件
NULL 博文链接:https://delinghayiye-163-com.iteye.com/blog/1683812
基于spring mvc 框架,用freemarker创建邮件模板,包括详细邮件发送功能的spring配置和实现代码