我想用自己的值替换application-dev.hml属性文件中的邮件设置值。 我这样做:
$date = array ('today', 'yesterday', 'in last week', 'in last month');
foreach( $date as $item ) {
$sum = $sums[$item];
$color = '';
if ($sum < 0) {
$color = 'red';
} elseif ($sum > 0) {
$color = 'green';
} else {
$color = 'black';
}
$html .= '<span style="color:'.$color.';">'.$sums[$item].'</span>';
} echo $html;
但是当我收到邮箱中的邮件时。它仍然来自属性文件中设置的地址(userLogin@gmail.com):
Properties properties = new Properties(); properties.put("mail.smtp.host", "ssl0.ovh.net"); properties.put("mail.smtp.user", "myOwnLogin@myApp.fr"); properties.put("mail.smtp.password", "xxx"); properties.put("mail.smtp.port", "25"); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.starttls.enable", "true"); javaMailSender.setJavaMailProperties(properties); MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, CharEncoding.UTF_8); message.setFrom("myOwnLogin@myApp.fr");
如何动态更改Java代码中的邮件设置?
感谢。
更新:
mail:
host: smtp.gmail.com
port: 587
username: userLogin@gmail.com
password: xxxxxx
protocol: smtp
properties.mail.smtp:
auth: true
starttls.enable: true
很奇怪,我需要使用任何工作值重置javaMailSender属性(这里的值与属性文件中的值相同),然后我可以设置&#34; From&#34;值。这很难看,但它确实有效。
答案 0 :(得分:0)
试试这个
mimeMessage.setFrom(new InternetAddress(userName));
mimeMessage.saveChanges(); // add this line here
答案 1 :(得分:0)
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.Properties;
@Configuration
public class EmailConfig {
@Autowired
ApplicationProperties applicationProperties;
@Bean
@Qualifier("primaryMailSender")
@Primary
public JavaMailSender getJavaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.gmail.com");
mailSender.setPort(587);
mailSender.setUsername(applicationProperties.PrimaryEmailConfig().getUsername());
mailSender.setPassword(applicationProperties.PrimaryEmailConfig().getPassword());
Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");
return mailSender;
}
@Bean
@Qualifier("secondaryMailSender")
public JavaMailSender getSecondaryJavaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.gmail.com");
mailSender.setPort(587);
mailSender.setUsername(applicationProperties.getSecondaryEmailConfig().getUsername());
mailSender.setPassword(applicationProperties.getSecondaryEmailConfig().getPassword());
Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");
return mailSender;
}
}
// in MailService.java inject the configs above by adding them as constructor parameters:
private final JavaMailSender secondaryJavaMailSender;
private final JavaMailSender javaMailSender;
public MailService(@Qualifier("primaryMailSender") JavaMailSender javaMailSender
@Qualifier("secondaryMailSender") JavaMailSender secondaryJavaMailSender) {
this.javaMailSender = javaMailSender;
this.secondaryJavaMailSender = secondaryJavaMailSender;
}