title: 試用環境開發環境不發郵件 tags:
- java
- Invalid Addresses categories: 工作日誌 date: 2017-05-25 18:18:56
對於近期專案出現了奇怪的現象
專案試用環境出現了沒有錯誤郵件的情況。
初期覺著沒有問題,恰逢端午過節,認為沒有人在開發環境測試導致。
到了釋出試用環境也沒有錯誤郵件
![143817_vwAM_871390.png][]
最近一封為05-27日。
釋出後發現生產環境可以正常收到郵件(很詭異)。
參考了前兩次郵件的問題
[maven專案引入新依賴問題][maven]
[slf4j 更新版本導致無日誌輸出][slf4j]
對比後發現沒有任何問題。
可以保證的是maven和郵件配置都沒有被修改。
注意到一個現象,試用環境在05-27之後再無錯誤郵件但是確實包含錯誤日誌的。
並且在05-31日才釋出,可以確定郵件發不出來應該和程式碼的變化沒有關係,
那麼可以比較一下試用環境和生產環境的郵件配置。
<executions>
<execution>
<id>replace</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>
process system.properties: set MAIL.To to ${exception.mail.to};
</echo>
<replaceregexp file="${basedir}/target/classes/system.properties"
match="exception\.mail\.to.*"
replace="exception\.mail\.to = ${exception.mail.to}"
byline="true"/>
<echo>
process system.properties: set MAIL.Subject to
${exception.mail.subject};
</echo>
<replaceregexp file="${basedir}/target/classes/system.properties"
match="exception\.mail\.subject.*"
replace="exception\.mail\.subject = ${exception.mail.subject}"
byline="true"/>
<echo>
process system.properties: set MAIL.Threshold =
${exception.mail.threshold};
</echo>
<replaceregexp file="${basedir}/target/classes/system.properties"
match="exception\.mail\.threshold.*"
replace="exception\.mail\.threshold = ${exception.mail.threshold}"
byline="true"/>
<echo>
process system.properties: set business.additivity =
${business.additivity};
</echo>
<replaceregexp file="${basedir}/target/classes/system.properties"
match="business\.additivity.*"
replace="business\.additivity = ${business.additivity}"
byline="true"/>
</tasks>
</configuration>
</execution>
複製程式碼
發現了唯一的卻別在於接受者配置的不一樣。
試用環境和開發環境均配置的是個人郵箱而生產環境配置的是郵件列表。
靈光閃現最近有個開發離職,會否是某個開發離職導致他的郵箱無法使用,傳送郵件失敗?
果然發現
/**
* Send the contents of the cyclic buffer as an e-mail message.
*/
protected void sendBuffer(CyclicBuffer<E> cb, E lastEventObject) {
// Note: this code already owns the monitor for this
// appender. This frees us from needing to synchronize on 'cb'.
try {
MimeBodyPart part = new MimeBodyPart();
StringBuffer sbuf = new StringBuffer();
String header = layout.getFileHeader();
if (header != null) {
sbuf.append(header);
}
String presentationHeader = layout.getPresentationHeader();
if (presentationHeader != null) {
sbuf.append(presentationHeader);
}
fillBuffer(cb, sbuf);
String presentationFooter = layout.getPresentationFooter();
if (presentationFooter != null) {
sbuf.append(presentationFooter);
}
String footer = layout.getFileFooter();
if (footer != null) {
sbuf.append(footer);
}
String subjectStr = "Undefined subject";
if (subjectLayout != null) {
subjectStr = subjectLayout.doLayout(lastEventObject);
// The subject must not contain new-line characters, which cause
// an SMTP error (LOGBACK-865). Truncate the string at the first
// new-line character.
int newLinePos = (subjectStr != null) ? subjectStr.indexOf('\n') : -1;
if (newLinePos > -1) {
subjectStr = subjectStr.substring(0, newLinePos);
}
}
MimeMessage mimeMsg = new MimeMessage(session);
if (from != null) {
mimeMsg.setFrom(getAddress(from));
} else {
mimeMsg.setFrom();
}
mimeMsg.setSubject(subjectStr, charsetEncoding);
List<InternetAddress> destinationAddresses = parseAddress(lastEventObject);
if (destinationAddresses.isEmpty()) {
addInfo("Empty destination address. Aborting email transmission");
return;
}
InternetAddress[] toAddressArray = destinationAddresses.toArray(EMPTY_IA_ARRAY);
mimeMsg.setRecipients(Message.RecipientType.TO, toAddressArray);
String contentType = layout.getContentType();
if (ContentTypeUtil.isTextual(contentType)) {
part.setText(sbuf.toString(), charsetEncoding, ContentTypeUtil.getSubType(contentType));
} else {
part.setContent(sbuf.toString(), layout.getContentType());
}
Multipart mp = new MimeMultipart();
mp.addBodyPart(part);
mimeMsg.setContent(mp);
mimeMsg.setSentDate(new Date());
addInfo("About to send out SMTP message \"" + subjectStr + "\" to " + Arrays.toString(toAddressArray));
Transport.send(mimeMsg);
} catch (Exception e) {
addError("Error occurred while sending e-mail notification.", e);
}
}
``` plain
發現報錯
<table>
<tbody>
<tr>
<td> <p><code>javax.mail.SendFailedException: Invalid Addresses; nested exception is:</code></p> </td>
</tr>
</tbody>
</table>
刪除掉離職者的郵箱果然可以正常了
![143946_H4wN_871390.png][]
[143817_vwAM_871390.png]: https://static.oschina.net/uploads/space/2017/0601/143817_vwAM_871390.png
[maven]: https://my.oschina.net/qixiaobo025/blog/897424
[slf4j]: https://my.oschina.net/qixiaobo025/blog/875988
[143946_H4wN_871390.png]: https://static.oschina.net/uploads/space/2017/0601/143946_H4wN_871390.png
複製程式碼