Support custom SMTP headers in Emailer #PL-4569

This commit is contained in:
Gleb Gorelov 2015-02-04 14:44:40 +00:00
parent fa1ae02f5e
commit 5b31ae36bf
4 changed files with 8 additions and 11 deletions

View File

@ -131,18 +131,14 @@ public class EmailSender implements EmailSenderAPI {
}
}
private void addHeaders(SendingMessage sendingMessage, MimeMessage message) {
private void addHeaders(SendingMessage sendingMessage, MimeMessage message) throws MessagingException {
if (sendingMessage.getHeaders() == null)
return ;
String[] splitHeaders = sendingMessage.getHeaders().split(SendingMessage.SEPARATOR);
return;
String[] splitHeaders = sendingMessage.getHeaders().split(SendingMessage.HEADERS_SEPARATOR);
for (String header : splitHeaders) {
EmailHeader emailHeader = EmailHeader.parse(header);
if (emailHeader != null) {
try {
message.addHeader(emailHeader.getName(), emailHeader.getValue());
} catch (MessagingException e) {
log.warn("Can't add email header: '" + header + "'");
}
message.addHeader(emailHeader.getName(), emailHeader.getValue());
} else {
log.warn("Can't parse email header: '" + header + "'");
}

View File

@ -566,10 +566,10 @@ public class Emailer implements EmailerAPI {
sendingMessage.setAttachments(Collections.<SendingAttachment>emptyList());
}
if (headers != null && headers.size() > 0) {
if (headers != null && !headers.isEmpty()) {
StringBuilder headersLine = new StringBuilder();
for (EmailHeader header : headers) {
headersLine.append(header.toString()).append(SendingMessage.SEPARATOR);
headersLine.append(header.toString()).append(SendingMessage.HEADERS_SEPARATOR);
}
sendingMessage.setHeaders(headersLine.toString());
} else {

View File

@ -28,7 +28,7 @@ public class SendingMessage extends StandardEntity {
private static final long serialVersionUID = -8156998515878702538L;
public static final int CAPTION_LENGTH = 500;
public static final String SEPARATOR = "\n";
public static final String HEADERS_SEPARATOR = "\n";
@Column(name = "ADDRESS_TO")
protected String address;

View File

@ -44,6 +44,7 @@ public class EmailHeader implements Serializable {
* @param line EmailHeader name and value separated with ":" symbol.
* @return Instance of EmailHeader object. Returns {@code null} if string has wrong format or {@code null} value.
*/
@Nullable
public static EmailHeader parse(@Nullable String line) {
if (line == null)
return null;