ssm框架实现读取服务器文件,ssm框架实现发送邮件

ssm框架实现发送邮件

获取授权码: arpaxwwaeoqvtbceb // 个人经过加密处理, 你们无法使用啊. 可以去自己的qq邮箱获取授权码

1.导入依赖

org.springframework

spring-context-support

5.0.0.RELEASE

com.sun.mail

javax.mail

1.6.1

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容器

${mail.smtp.auth}

${mail.smtp.timeout}

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("

内容

", 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("

邮件内容123345

" +

"",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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值