springboot发邮件,服务器可以通过

spring.mail.host=smtp.163.com
spring.mail.protocol=smtp
spring.mail.port=587
spring.mail.username=你的账号
spring.mail.password=你的密码
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtp.timeout=25000
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory

service方法

	@Autowired
	private JavaMailSender mailSender;
	@Value("${spring.mail.username}")
	private String from;
	@Override
	public boolean send(String email, String title, String text) {
		// TODO Auto-generated method stub
		  MimeMessage message=mailSender.createMimeMessage();
		 MimeMessageHelper helper;
		 
		try {
			helper = new MimeMessageHelper(message,true);
			helper.setFrom(from);
			helper.setTo(email);
			helper.setSubject(title);
			helper.setText(text,true);
			mailSender.send(message);
			return true;
        } catch (Exception e) {
        	return false;
        }
	         
	}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于这个问题,我可以给你一些参考。 首先,你需要在 `pom.xml` 文件中添加邮件发送所需的依赖,例如: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 接下来,你需要在 `application.properties` 文件中添加邮件发送所需的配置,例如: ``` spring.mail.host=smtp.xxx.com spring.mail.username=xxx@xxx.com spring.mail.password=xxx spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true spring.mail.default-encoding=UTF-8 ``` 其中,`spring.mail.host` 为邮件服务器地址,`spring.mail.username` 和 `spring.mail.password` 为登录邮件服务器的用户名和密码。 然后,在你的代码中,你可以使用 `JavaMailSender` 类来发送邮件。例如,以下是一个发送带有附件的邮件的示例代码: ```java @Autowired private JavaMailSender mailSender; public void sendMailWithAttachment() throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom("xxx@xxx.com"); helper.setTo("yyy@yyy.com"); helper.setSubject("测试邮件(带附件)"); helper.setText("这是一封测试邮件(带附件)!"); // 添加附件 FileSystemResource file = new FileSystemResource(new File("path/to/attachment")); helper.addAttachment("附件", file); mailSender.send(message); } ``` 其中,`MimeMessageHelper` 类可以帮助你构建复杂的邮件,`helper.setFrom` 和 `helper.setTo` 分别设置发件人和收件人,`helper.addAttachment` 可以添加附件。 希望这个回答能够帮助你解决问题。如果你还有其他问题,可以继续问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值