in sources/src/main/java/com/google/solutions/jitaccess/apis/clients/SmtpClient.java [60:128]
public void sendMail(
@NotNull Collection<EmailAddress> toRecipients,
@NotNull Collection<EmailAddress> ccRecipients,
@NotNull String subject,
@NotNull Multipart content,
@NotNull EnumSet<Flags> flags
) throws MailException {
Preconditions.checkNotNull(toRecipients, "toRecipients");
Preconditions.checkNotNull(ccRecipients, "ccRecipients");
Preconditions.checkNotNull(subject, "subject");
Preconditions.checkNotNull(content, "content");
PasswordAuthentication authentication;
try {
authentication = this.options.createPasswordAuthentication(this.secretManagerClient);
}
catch (Exception e) {
throw new MailException("Looking up SMTP credentials failed", e);
}
var session = Session.getDefaultInstance(
this.options.smtpProperties,
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
});
try {
var message = new MimeMessage(session);
message.setContent(content);
message.setFrom(new InternetAddress(
this.options.senderAddress.value(),
this.options.senderName));
for (var recipient : toRecipients){
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress(recipient.value(), recipient.value()));
}
for (var recipient : ccRecipients){
message.addRecipient(
Message.RecipientType.CC,
new InternetAddress(recipient.value(), recipient.value()));
}
//
// NB. Setting the Precedence header prevents (some) mail readers to not send
// out of office-replies.
//
message.addHeader("Precedence", "bulk");
if (flags.contains(Flags.REPLY)) {
message.setFlag(jakarta.mail.Flags.Flag.ANSWERED, true);
message.setSubject("Re: " + subject);
}
else {
message.setSubject(subject);
}
Transport.send(message);
}
catch (MessagingException | UnsupportedEncodingException e) {
throw new MailException("The mail could not be delivered", e);
}
}