gpt4 book ai didi

java - 电子邮件地址中的撇号引发异常

转载 作者:太空宇宙 更新时间:2023-11-04 14:55:07 25 4
gpt4 key购买 nike

我必须使用 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;
}
}

如果有人能告诉我如何解决这个问题,那就太好了。

最佳答案

有两种可能的方法来处理这个问题(不知道你的代码到底是什么样子,如果你可以发布一些代码来显示消息是如何发送的,那就太酷了)。

  • 使用 strict=false 例如new InternetAddress("Michael.O’Hara@sampleDomain.com", false)
  • 除了上述方法之外,还可以将电子邮件地址本身放在双引号中,尽管这只是一种解决方法
  • 放宽对Session对象的严格验证session.setProperty("mail.mime.address.strict", "false");

关于java - 电子邮件地址中的撇号引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23298246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com