springboot 发送邮件 发件人姓名咋设置
时间: 2023-07-07 22:33:42 浏览: 53
在使用Spring Boot发送邮件时,可以通过JavaMailSenderImpl类的setFrom方法设置发件人姓名。示例代码如下:
```java
@Autowired
private JavaMailSenderImpl mailSender;
public void sendEmail(String to, String subject, String text) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom("发件人邮箱地址", "发件人姓名");
helper.setTo(to);
helper.setSubject(subject);
helper.setText(text);
mailSender.send(message);
}
```
其中,setFrom方法的第一个参数是发件人邮箱地址,第二个参数是发件人姓名。
相关问题
springboot发送邮件 outlook
以下是使用SpringBoot发送邮件的示例代码,其中包括了如何发送带附件的邮件和如何开启TLS验证:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@SpringBootApplication
@EnableConfigurationProperties(MailProperties.class)
public class MailApplication {
@Autowired
private JavaMailSender mailSender;
@Autowired
private MailProperties mailProperties;
public static void main(String[] args) {
SpringApplication.run(MailApplication.class, args);
}
public void sendMailWithAttachment() throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(mailProperties.getUsername());
helper.setTo("recipient@example.com");
helper.setSubject("Test email with attachment");
// 添加附件
FileSystemResource file = new FileSystemResource(new File("attachment.txt"));
helper.addAttachment("attachment.txt", file);
// 发送邮件
mailSender.send(message);
}
}
```
在application.properties文件中添加以下配置:
```
spring.mail.username=xxxxxxx@outlook.com
spring.mail.password=xxxxxxxxx
spring.mail.port=587
spring.mail.host=smtp-mail.outlook.com
spring.mail.properties.mail.smtp.starttls.required=true
```
注意:在使用Outlook发送邮件时,需要开启TLS验证,否则会显示匿名用户无法通过验证。
springboot发送邮件
可以使用JavaMailSender类来发送邮件,Spring Boot已经为我们提供了自动配置。首先需要在pom.xml文件中添加依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
```
然后在application.properties文件中配置邮件服务器信息:
```
spring.mail.host=smtp.example.com
spring.mail.port=587
spring.mail.username=yourusername
spring.mail.password=yourpassword
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
```
最后在代码中使用JavaMailSender发送邮件即可:
```
@Autowired
private JavaMailSender javaMailSender;
public void sendEmail() {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo("recipient@example.com");
message.setSubject("Test Email");
message.setText("This is a test email.");
javaMailSender.send(message);
}
```