spring.profiles.active=@active.mode@
spring.data.mongodb.database=karaoke-db
server.port=5813
base.host=@base.host@
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=yourEmail@gmail.com
spring.mail.password=yourEmailPassword
mail.transport.protocol=smtp
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
mail.from.email=yourEmail@gmail.com
mail.debug=true
在您的 EmailConfig 课程中
package com.tlcreativeltd.karaoke.config.email;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import java.util.Properties;
@Configuration
@PropertySource("classpath:application.properties")
public class MailConfig {
@Value("${spring.mail.host}")
private String host;
@Value("${spring.mail.port}")
private int port;
@Value("${spring.mail.username}")
private String userName;
@Value("${spring.mail.password}")
private String passwd;
@Value("${mail.transport.protocol}")
private String protocol;
@Value("${mail.from.email}")
private String from;
@Bean
public JavaMailSender getJavaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(host);
mailSender.setPort(port);
mailSender.setUsername(userName);
mailSender.setPassword(passwd);
Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol", protocol);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.from.email", from);
props.put("mail.debug", "true");
// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, passwd);
}
};
Session session = Session.getInstance(props, auth);
mailSender.setSession(session);
return mailSender;
}
}