Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improvement-16527][alert] Add retry send email validAddresses when abnormal addresses in the email receiving list #16529

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@

import javax.activation.CommandMap;
import javax.activation.MailcapCommandMap;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.event.TransportAdapter;
import javax.mail.event.TransportEvent;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
Expand Down Expand Up @@ -351,8 +356,8 @@ private void attachContent(String title, String content, String partContent,
partList.addBodyPart(part1);
partList.addBodyPart(part2);
msg.setContent(partList);
// 5. send Transport
Transport.send(msg);
// 5. send email
sendMail(msg, msg.getSession());
// 6. delete saved file
deleteFile(file);
}
Expand All @@ -361,7 +366,7 @@ private void attachContent(String title, String content, String partContent,
* the string object map
*/
private AlertResult getStringObjectMap(String title, String content, AlertResult alertResult,
HtmlEmail email) throws EmailException {
HtmlEmail email) throws EmailException, MessagingException {

/*
* the subject of the message to be sent
Expand All @@ -378,13 +383,41 @@ private AlertResult getStringObjectMap(String title, String content, AlertResult

// send
email.setDebug(true);
email.send();

email.buildMimeMessage();
sendMail(email.getMimeMessage(), email.getMailSession());
alertResult.setSuccess(true);

return alertResult;
}

/**
* send mail with validAddresses retry
*
* @param mimeMessage the message
* @param session connectSession
*/
private void sendMail(MimeMessage mimeMessage, Session session) throws MessagingException {
SbloodyS marked this conversation as resolved.
Show resolved Hide resolved
Transport transport = session.getTransport(new SMTPProvider());
transport.addTransportListener(new TransportAdapter() {
@Override
public void messageNotDelivered(TransportEvent event) {
Address[] validUnsentAddresses = event.getValidUnsentAddresses();
if (validUnsentAddresses == null || validUnsentAddresses.length < 1) {
return;
}
try {
transport.sendMessage(mimeMessage, validUnsentAddresses);
} catch (MessagingException e) {
log.error("send mail with validAddresses failed: {}, reason:", validUnsentAddresses, e);
}
}
});
URLName urlName = transport.getURLName();
transport.connect(urlName.getHost(), urlName.getUsername(), urlName.getPassword());
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
transport.close();
}

/**
* file delete
*
Expand Down
Loading