目录
ssm框架实现发送邮件
获取授权码: arpaxwwaeoqvtbceb // 个人经过加密处理, 你们无法使用啊. 可以去自己的qq邮箱获取授权码
1.导入依赖
<!--spring支持-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<!--邮件发送-->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.1</version>
</dependency>
2.配置文件(mail.properties)
#服务器主机名 smtp.xx.com
mail.smtp.host=smtp.qq.com
mail.smtp.username=*********@qq.com
#密码/客户端授权码
mail.smtp.password=**********
#编码字符
mail.smtp.defaultEncoding=utf-8
#是否进行用户名密码校验
mail.smtp.auth=true
#设置超时时间
mail.smtp.timeout=20000
3.注入spring容器
<!--邮件配置-->
<!--
读取邮件配置文件,
其中ignore-unresolvable="true"属性是配置文件中存在
多个property-placeholder时出现解析不了的占位符进行忽略掉,
-->
<context:property-placeholder location="classpath:mail.properties" ignore-unresolvable="true"/>
<!--配置邮件接口-->
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.smtp.host}"/>
<property name="username" value="${mail.smtp.username}"/>
<property name="password" value="${mail.smtp.password}"/>
<property name="defaultEncoding" value="${mail.smtp.defaultEncoding}"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
</props>
</property>
</bean>
4.测试
3.1 简单的邮件
@Autowired
private JavaMailSenderImpl javaMailSender;
@Test
void contextLoads() {
SimpleMailMessage message = new SimpleMailMessage(); // 创建消息对象
message.setSubject("标题"); // 标题
message.setText("正文"); // 只支持文本, 不支持html
message.setTo("546279462@qq.com"); // 收件人
message.setFrom("546279462@qq.com"); // 发件人
javaMailSender.send(message); // 发送
}
3.2 复杂的邮件
@Autowired
private JavaMailSenderImpl javaMailSender;
@Test
void testMail() throws MessagingException {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setSubject("标题"); // 标题
// 内容, 第二个参数为true则以html方式发送, 否则以普通文本发送
helper.setText("<h1 style='red'>内容</h1>", true);
//发送附件
helper.addAttachment("1.jpg",new File("C:\Users\zpk\Desktop\loading\加载-063.gif"));
helper.addAttachment("2.jpg",new File("C:\Users\zpk\Desktop\loading\加载-067.gif"));
helper.setTo("546279462@qq.com"); // 收件人
helper.setFrom("546279462@qq.com"); // 发件人
javaMailSender.send(message); // 发送
}
不注入容器方式
不注入容器, 直接读取第二部的配置文件
@Test
public void testMail(){
AbstractApplicationContext as = new ClassPathXmlApplicationContext("applicationContext.xml");
JavaMailSenderImpl javaMailSender = as.getBean("javaMailSender",JavaMailSenderImpl.class);
MimeMessage mailMessage = javaMailSender.createMimeMessage();//创建邮件对象
MimeMessageHelper mimeMessageHelper;
Properties prop = new Properties();
String from;
try {
// 从配置文件读取发件人邮箱地址
prop.load(this.getClass().getResourceAsStream("util/mail.properties"));
from = prop.getProperty("mail.smtp.username");
mimeMessageHelper = new MimeMessageHelper(mailMessage,true);
mimeMessageHelper.setFrom(from); //发件人地址
mimeMessageHelper.setTo("546279462@qq.com"); //收件人邮箱
mimeMessageHelper.setSubject("邮件主题"); //邮件主题
mimeMessageHelper.setText("<p style='color: red'>邮件内容123345</p>" +
"<img src='cid:06'/>",true); //true表示以html方式发送
File file = new File("C:\Users\54627\Pictures\06.jpg"); //导入其他资源
FileSystemResource resource = new FileSystemResource(file);
mimeMessageHelper.addInline("06",resource); //可以指定id,在内容中引用(cid:id)
mimeMessageHelper.addAttachment("06.jsp",resource); //附件
javaMailSender.send(mailMessage);
} catch (IOException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("发送成功");
}
扩展: maven读取properties
try {
//读取配置文件
InputStream is = Test.class.getClassLoader().getResourceAsStream("util/mail.properties");
Properties properties = new Properties();
properties.load(is);
System.out.println(properties.getProperty("mail.smtp.host"));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}