mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-06 08:36:01 +00:00
fix garbled mail headers (From/To/ReplyTo).
This commit is contained in:
1 parent
5a730a5962
commit
00a221d42f
3 files changed
+36
-14
No files matched your search
@@ -43,6 +43,7 @@ import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -120,6 +121,8 @@ public class MailSender {
|
||||
}
|
||||
} catch (MessagingException e) {
|
||||
e.printStackTrace(listener.error(e.getMessage()));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace(listener.error(e.getMessage()));
|
||||
} finally {
|
||||
CHECKPOINT.report();
|
||||
}
|
||||
@@ -145,7 +148,7 @@ public class MailSender {
|
||||
return b.getResult();
|
||||
}
|
||||
|
||||
protected MimeMessage getMail(AbstractBuild<?, ?> build, BuildListener listener) throws MessagingException, InterruptedException {
|
||||
protected MimeMessage getMail(AbstractBuild<?, ?> build, BuildListener listener) throws MessagingException, UnsupportedEncodingException, InterruptedException {
|
||||
if (build.getResult() == Result.FAILURE) {
|
||||
return createFailureMail(build, listener);
|
||||
}
|
||||
@@ -169,7 +172,7 @@ public class MailSender {
|
||||
return null;
|
||||
}
|
||||
|
||||
private MimeMessage createBackToNormalMail(AbstractBuild<?, ?> build, String subject, BuildListener listener) throws MessagingException {
|
||||
private MimeMessage createBackToNormalMail(AbstractBuild<?, ?> build, String subject, BuildListener listener) throws MessagingException, UnsupportedEncodingException {
|
||||
MimeMessage msg = createEmptyMail(build, listener);
|
||||
|
||||
msg.setSubject(getSubject(build, Messages.MailSender_BackToNormalMail_Subject(subject)),charset);
|
||||
@@ -180,7 +183,7 @@ public class MailSender {
|
||||
return msg;
|
||||
}
|
||||
|
||||
private MimeMessage createUnstableMail(AbstractBuild<?, ?> build, BuildListener listener) throws MessagingException {
|
||||
private MimeMessage createUnstableMail(AbstractBuild<?, ?> build, BuildListener listener) throws MessagingException, UnsupportedEncodingException {
|
||||
MimeMessage msg = createEmptyMail(build, listener);
|
||||
|
||||
String subject = Messages.MailSender_UnstableMail_Subject();
|
||||
@@ -219,7 +222,7 @@ public class MailSender {
|
||||
buf.append(Messages.MailSender_Link(baseUrl, url)).append("\n\n");
|
||||
}
|
||||
|
||||
private MimeMessage createFailureMail(AbstractBuild<?, ?> build, BuildListener listener) throws MessagingException, InterruptedException {
|
||||
private MimeMessage createFailureMail(AbstractBuild<?, ?> build, BuildListener listener) throws MessagingException, UnsupportedEncodingException, InterruptedException {
|
||||
MimeMessage msg = createEmptyMail(build, listener);
|
||||
|
||||
msg.setSubject(getSubject(build, Messages.MailSender_FailureMail_Subject()),charset);
|
||||
@@ -304,19 +307,19 @@ public class MailSender {
|
||||
return msg;
|
||||
}
|
||||
|
||||
private MimeMessage createEmptyMail(AbstractBuild<?, ?> build, BuildListener listener) throws MessagingException {
|
||||
private MimeMessage createEmptyMail(AbstractBuild<?, ?> build, BuildListener listener) throws MessagingException, UnsupportedEncodingException {
|
||||
MimeMessage msg = new MimeMessage(Mailer.descriptor().createSession());
|
||||
// TODO: I'd like to put the URL to the page in here,
|
||||
// but how do I obtain that?
|
||||
msg.addHeader("X-Jenkins-Job", build.getProject().getDisplayName());
|
||||
msg.addHeader("X-Jenkins-Result", build.getResult().toString());
|
||||
msg.setContent("", "text/plain");
|
||||
msg.setFrom(new InternetAddress(Mailer.descriptor().getAdminAddress()));
|
||||
msg.setFrom(Mailer.StringToAddress(Mailer.descriptor().getAdminAddress(), charset));
|
||||
msg.setSentDate(new Date());
|
||||
|
||||
String replyTo = Mailer.descriptor().getReplyToAddress();
|
||||
if (StringUtils.isNotBlank(replyTo)) {
|
||||
msg.setHeader("Reply-To", replyTo);
|
||||
msg.setReplyTo(new Address[]{Mailer.StringToAddress(replyTo, charset)});
|
||||
}
|
||||
|
||||
Set<InternetAddress> rcp = new LinkedHashSet<InternetAddress>();
|
||||
@@ -342,7 +345,7 @@ public class MailSender {
|
||||
}
|
||||
|
||||
try {
|
||||
rcp.add(new InternetAddress(address));
|
||||
rcp.add(Mailer.StringToAddress(address, charset));
|
||||
} catch (AddressException e) {
|
||||
// report bad address, but try to send to other addresses
|
||||
listener.getLogger().println("Unable to send to address: " + address);
|
||||
@@ -377,7 +380,7 @@ public class MailSender {
|
||||
return msg;
|
||||
}
|
||||
|
||||
void includeCulpritsOf(AbstractProject upstreamProject, AbstractBuild<?, ?> currentBuild, BuildListener listener, Set<InternetAddress> recipientList) throws AddressException {
|
||||
void includeCulpritsOf(AbstractProject upstreamProject, AbstractBuild<?, ?> currentBuild, BuildListener listener, Set<InternetAddress> recipientList) throws AddressException, UnsupportedEncodingException {
|
||||
AbstractBuild<?,?> upstreamBuild = currentBuild.getUpstreamRelationshipBuild(upstreamProject);
|
||||
AbstractBuild<?,?> previousBuild = currentBuild.getPreviousBuild();
|
||||
AbstractBuild<?,?> previousBuildUpstreamBuild = previousBuild!=null ? previousBuild.getUpstreamRelationshipBuild(upstreamProject) : null;
|
||||
@@ -398,14 +401,14 @@ public class MailSender {
|
||||
} while ( b != upstreamBuild && b != null );
|
||||
}
|
||||
|
||||
private Set<InternetAddress> buildCulpritList(BuildListener listener, Set<User> culprits) throws AddressException {
|
||||
private Set<InternetAddress> buildCulpritList(BuildListener listener, Set<User> culprits) throws AddressException, UnsupportedEncodingException {
|
||||
Set<InternetAddress> r = new HashSet<InternetAddress>();
|
||||
for (User a : culprits) {
|
||||
String adrs = Util.fixEmpty(a.getProperty(Mailer.UserProperty.class).getAddress());
|
||||
if(debug)
|
||||
listener.getLogger().println(" User "+a.getId()+" -> "+adrs);
|
||||
if (adrs != null)
|
||||
r.add(new InternetAddress(adrs));
|
||||
r.add(Mailer.StringToAddress(adrs, charset));
|
||||
else {
|
||||
listener.getLogger().println(Messages.MailSender_NoAddress(a.getFullName()));
|
||||
}
|
||||
|
||||
@@ -50,12 +50,16 @@ import org.kohsuke.stapler.export.Exported;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Date;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.Matcher;
|
||||
import javax.mail.Address;
|
||||
import javax.mail.Authenticator;
|
||||
import javax.mail.Message;
|
||||
import javax.mail.MessagingException;
|
||||
@@ -141,6 +145,18 @@ public class Mailer extends Notifier {
|
||||
return BuildStepMonitor.NONE;
|
||||
}
|
||||
|
||||
private static Pattern ADDRESS_PATTERN = Pattern.compile("\\s*([^<]*)<([^>]+)>\\s*");
|
||||
public static InternetAddress StringToAddress(String strAddress, String charset) throws AddressException, UnsupportedEncodingException {
|
||||
Matcher m = ADDRESS_PATTERN.matcher(strAddress);
|
||||
if(!m.matches()) {
|
||||
return new InternetAddress(strAddress);
|
||||
}
|
||||
|
||||
String personal = m.group(1);
|
||||
String address = m.group(2);
|
||||
return new InternetAddress(address, personal, charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated as of 1.286
|
||||
* Use {@link #descriptor()} to obtain the current instance.
|
||||
@@ -472,12 +488,12 @@ public class Mailer extends Notifier {
|
||||
MimeMessage msg = new MimeMessage(createSession(smtpServer,smtpPort,useSsl,smtpAuthUserName,Secret.fromString(smtpAuthPassword)));
|
||||
msg.setSubject(Messages.Mailer_TestMail_Subject(++testEmailCount), charset);
|
||||
msg.setText(Messages.Mailer_TestMail_Content(testEmailCount, Jenkins.getInstance().getDisplayName()), charset);
|
||||
msg.setFrom(new InternetAddress(adminAddress));
|
||||
msg.setFrom(StringToAddress(adminAddress, charset));
|
||||
if (StringUtils.isNotBlank(replyToAddress)) {
|
||||
msg.setHeader("Reply-To", replyToAddress);
|
||||
msg.setReplyTo(new Address[]{StringToAddress(replyToAddress, charset)});
|
||||
}
|
||||
msg.setSentDate(new Date());
|
||||
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(sendTestMailTo));
|
||||
msg.setRecipient(Message.RecipientType.TO, StringToAddress(sendTestMailTo, charset));
|
||||
|
||||
Transport.send(msg);
|
||||
return FormValidation.ok(Messages.Mailer_EmailSentSuccessfully());
|
||||
|
||||
Reference in new issue
Block a user