Current state
I have a Java SpringBoot api, which at the moment is able to dynamically send an E-Mail with javax.mail.MimeMessage. The credentials are passed as parameters (E-Mail-Address, Password, SMTP-Server, SMTP-Port). When I start the application and send the first E-Mail everything works fine.
Problem
However when I try to send another E-Mail from a different SMTP-Server, it seems that the E-Mail gets sended from the first SMTP-Server and credentials. In, for example Googlemail, every E-Mail shows the sender as well as the (SMTP-)Server which it's sended from. While the sended E-Mail-Address displays the new E-Mail-Address, the "Send from" (SMTP-Server) still shows the old SMTP-Server.
Goal
So what I want, is to be able to send multiple E-Mails from individual SMTP-Servers and E-Mail-Addresses, passed by parameters.
Controller
@RequestMapping("/send-email")
public class EmailController {
public EmailController(){}
@PostMapping
public SentEmail sendEmail(@RequestBody SendEmailInformation sendEmailInformation) {
EmailService emailService = new EmailService();
return emailService.sendEmail(sendEmailInformation);
}
}
E-Mail-Service
{
private Transport transport;
public EmailService() {}
public void sendEmail(SendEmailInformation sendEmailInformation) {
try{
String fromEmail = sendEmailInformation.getEmailAddress();
String password = sendEmailInformation.getPassword();
String toEmail = sendEmailInformation.getEmail();
Properties props = new Properties();
props.put("mail.smtp.host", sendEmailInformation.getSmtpServer());
props.put("mail.smtp.port", sendEmailInformation.getSmtpPort());
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
Authenticator auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, password);
}
};
Session session = Session.getDefaultInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("format", "flowed");
msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setFrom(new InternetAddress(fromEmail));
msg.setReplyTo(InternetAddress.parse(sendEmailInformation.getEmail(), false));
msg.setSubject(sendEmailInformation.getSubject(), "UTF-8");
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(sendEmailInformation.getMessage());
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
transport = session.getTransport("smtp");
transport.connect();
transport.sendMessage(msg,msg.getRecipients(Message.RecipientType.TO));
transport.close();
}catch (Exception e) {
try {
transport.close();
}catch (Exception ignored) {
}
e.printStackTrace();
throw new CouldNotSendEmailException();
}
}
}
I already tried
At first, I had the Email-Service as a @Bean and figured, that I should try to create a new Object for every call as I do now. So using a @Bean does not work. Also in my understanding, closing the Transport Object with transport.close(); at the end should help my problem, however it didnt't.
Question
Does anyone have an idea, what could solve my Problem?
Thanks in advance.
Edit
Solution
Looking for my code many days in a row and never saw the mistake, then posting this question and just minutes later having the idea that Session session = Session.getDefaultInstance(props, auth); might be the problem... yes it is! This always uses the same Session object, instead of creating a new one.
For those who are facing the same problem:
Just use Session session = Session.getInstance(props, auth); instead. Hope I could help someone with that.