实施故障转移电子邮件服务...如果发生故障,请使用其他电子邮件服

时间:2017-07-15 03:10:19

标签: java maven email spring-boot javamail

我需要实施故障转移电子邮件服务。意味着如果一个发生故障,服务可以故障转移到另一个提供商。

提供商

我正在使用弹簧靴,maven。

是否可以像

一样使用应用程序属性

spring.mail.host=smtp.mailgun.org, smtp.sendgrid.org

直到现在: application.properties

spring.mail.host=smtp.mailgun.org
spring.mail.port=587
spring.mail.username=some-username
spring.mail.password=some-password
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000

邮件发送方法实现:

@Override
    public void sendMails(MailDomain mailDomain) {    // MailDomain is class that contains fields useful to configure mail attributes
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);

        try {
            helper.setTo(mailDomain.getSendTo());
            helper.setText(mailDomain.getMailBody());
            helper.setSubject(mailDomain.getSubject());
        } catch (MessagingException e) {
            LOG.debug("Unable to set details of message " + e.getMessage());
        }

        try {
            mailSender.send(message);     // send mail....
        } catch (MailException e) {
            LOG.debug("Unable to sendmail " + e.getMessage());
        }
    }

2 个答案:

答案 0 :(得分:2)

当您提供两台主机时,Spring引导不会执行故障转移,您必须自己定义第二个mailSender并处理故障转移。斯普林斯使这很容易:

  @Bean
  @ConfigurationProperties(prefix = "second.mail")
  public MailSender secondMailSender() {
    return new JavaMailSenderImpl();
  }

这将创建一个从以下属性初始化的新邮件发件人:

second.mail.host=mail.mymail.org

现在,此bean的存在将禁止默认邮件发件人的自动配置,因此您需要自己定义:

@Bean
@ConfigurationProperties(prefix = "first.mail")
public MailSender firstMailSender() {
    return new JavaMailSenderImpl();
}

之后:

@Autowired
private MailSender secondMailSender;

@Autowired
private MailSender firstMailSender;

try {
  firstMailSender.send(message);     // send mail....
} catch (MailException e) {
  LOG.debug("Unable to sendmail " + e.getMessage());
  try {
    secondMailSender.send(message); 
    ....
}

答案 1 :(得分:0)

这是很晚的答案,但是我找到了一个简单的解决方案。

您可以尝试使用这种方法来处理故障转移。

application.properties

hostFirst=email-smtp.us-west-2.amazonaws.com
hostSecond=smtp.gmail.com
portFirst=25
portSecond=587
username=email
password=password
protocol=smtp
mail.auth=true
starttls=true

EmailService.java

package com.emailservice.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import java.util.Properties;

@Service
public class EmailService {
    @Value("${hostFirst}")
    private String hostFirst;

    @Value("${hostSecond}")
    private String hostSecond;

    @Value("${portFirst}")
    private int portFirst;

    @Value("${portSecond}")
    private int portSecond;

    @Value("${username}")
    private String username;

    @Value("${password}")
    private String password;


    @Value("${protocol}")
    private String protocol;

    @Value("${mail.auth}")
    private boolean auth;

    @Value("${starttls}")
    private boolean starttls;

    public void sendSimpleMessage(String from, String to) {
        JavaMailSenderImpl  javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setUsername(username);
        javaMailSender.setPassword(password);
        javaMailSender.setJavaMailProperties(getMailProperties());

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject("Spring Boot Email");
        message.setText("Greetings for the Day :)");
        try {
            System.out.println("Trying To Send from first mail");

            javaMailSender.setHost(hostFirst);
            javaMailSender.setPort(portFirst);
            javaMailSender.send(message);

        } catch (MailException e) {
            System.out.println(e.getLocalizedMessage());
            System.out.println("Trying To Send from second mail");

            javaMailSender.setHost(hostSecond);
            javaMailSender.setPort(portSecond);
            javaMailSender.send(message);

        }
    }

    private Properties getMailProperties() {
        Properties properties = new Properties();
        properties.setProperty("mail.transport.protocol", protocol);
        properties.setProperty("mail.smtp.auth", auth?"true":"false");
        properties.setProperty("mail.smtp.starttls.enable", starttls?"true":"false");
        return properties;
    }
}