本文介绍了电子邮件地址中的撇号引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须使用JavaMail API向用户发送电子邮件.
I have to send email to users using JavaMail API.
用户的电子邮件地址可能带有撇号
User may have email address with apostrophe some like
Michael.O’[email protected]
当我尝试向此类用户发送电子邮件时,我遇到了异常情况.
I am getting below exception when I try to send an email to such a user.
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)
以下是我用来发送电子邮件的方法
Following is the method that I am using to send email
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;
}
}
如果任何人都可以告诉我如何解决此问题,那将很棒.
If anyone can tell me how to fix this, that will be great.
推荐答案
RFC 2822规范绝对要求地址的本地部分包含诸如此类的特殊字符,即"Michael.O'Hara" @ sampleDomain.com.
The RFC 2822 spec absolutely requires that the local part of an address that contains special characters such as this be quoted, i.e., "Michael.O’Hara"@sampleDomain.com.
这篇关于电子邮件地址中的撇号引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!