我必须使用JavaMail API向用户发送电子邮件。
用户可能拥有撇号的电子邮件地址,例如
Michael.O’Hara@sampleDomain.com
当我尝试向这样的用户发送电子邮件时,我遇到了异常。
javax.mail.MessagingException: Can't send command to SMTP host;
nested exception is:
java.net.SocketException: Software caused connection abort: socket write error
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1564)
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1551)
at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:696)
at javax.mail.Transport.send0(Transport.java:191)
at javax.mail.Transport.send(Transport.java:118)
at com.openpages.ext.duke.util.DukeEmailUtil.sendNotification(DukeEmailUtil.java:194)
以下是我用来发送电子邮件的方法
public static void sendNotification(String mailServer, String fromName,
String fromAddress, String toAddress, String ccAddresses,
String subject, String emailContent) throws Exception {
try {
// TODO: The email session should be cached in a future release.
Properties props = new Properties();
props.put("mail.smtp.host", mailServer);
Session session = Session.getInstance(props, null);
MimeMessage msg = new MimeMessage(session);
InternetAddress addressFrom = null;
if (StringUtil.isGood(fromName))
addressFrom = new InternetAddress(fromAddress, fromName);
else
addressFrom = new InternetAddress(fromAddress);
msg.setFrom(addressFrom);
if (toAddress != null && !toAddress.equalsIgnoreCase("")) {
String[] toAddressesArray = toAddress.split(";");
InternetAddress[] addressTO = new InternetAddress[toAddressesArray.length];
for (int i = 0; i < toAddressesArray.length; i++) {
LoggerFactory.getLogger().error("Before InternetAdress Contructor");
addressTO[i] = new InternetAddress(toAddressesArray[i]);
LoggerFactory.getLogger().error("After InternetAdress Contructor");
}
msg.setRecipients(Message.RecipientType.TO, addressTO);
}
// TODO : method signature has to be changed
// to take String[] for toAddress & ccAddresses
if (ccAddresses != null && !ccAddresses.equalsIgnoreCase("")) {
String[] ccAddressesArray = ccAddresses.split(";");
InternetAddress[] addressCC = new InternetAddress[ccAddressesArray.length];
for (int i = 0; i < ccAddressesArray.length; i++) {
addressCC[i] = new InternetAddress(ccAddressesArray[i]);
}
msg.setRecipients(Message.RecipientType.CC, addressCC);
}
msg.setSubject(subject, "utf-8");
msg.setContent(emailContent, "text/html;charset=utf-8");
Transport.send(msg);
msg = null;
session = null;
}
catch (Exception e) {
e.printStackTrace();
throw e;
}
}
如果有人能告诉我如何解决这个问题,那就太棒了。
答案 0 :(得分:1)
假设允许带有`的电子邮件地址,我会尝试更改此
addressTO[i] = new InternetAddress(toAddressesArray[i]);
到这个
addressTO[i] = new InternetAddress(toAddressesArray[i].replace("`", "\\`"));
使用\。
跳过`字符答案 1 :(得分:0)
符合RFC2822“3.4.1。地址规范”&amp; “3.2.5。带引号的字符串”如果它被转义,你可以使用这些字符(')。
要转义单个字符,您可以使用“\”;转义多个字符使用双引号。 以下地址将是有效的RFC2822地址: Michael.O \'Hara@sampleDomain.com “Michael.O'Hara” @ sampleDomain.com
PS很高兴看到完整的堆栈跟踪,可能是因为一些不同的问题你无法发送邮件。
答案 2 :(得分:-2)
这是一个链接,讨论了一些不符合RFC821(SMTP邮件的Internet标准)的电子邮件服务器引起的同一问题
[1]:http://searchexchange.techtarget.com/tip/Beware-of-the-apostrophe