2

I have to send email to users using JavaMail API.

User may have email address with apostrophe some like

 Michael.O’Hara@sampleDomain.com 

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.

user3553237
  • 112
  • 1
  • 1
  • 7
  • I think you have to fix this on the server. The server is closing the socket for some reason. It shouldn't. That's a legal email address and there doesn't appear to be anything wrong in the client code you've posted here. See here: http://stackoverflow.com/questions/2049502/what-characters-are-allowed-in-email-address – mttdbrd Apr 28 '14 at 04:04
  • does this work for other email addresses without the ' – AdityaKeyal Apr 28 '14 at 04:14
  • @AdityaKeyal Yes it does work for other email addresses without any issue – user3553237 Apr 28 '14 at 04:15
  • try quoting the local-part of the email: `"Michael.O’Hara"@sampleDomain.com` – morgano Apr 28 '14 at 04:16
  • I did try quoting it, but I get the email address as below after quoting "Michael.O’Hara"@duke-energy.com – user3553237 Apr 28 '14 at 04:17
  • 1
    are you sure it is the character `’` and not `'`? – morgano Apr 28 '14 at 04:30
  • @morgano I am pretty much sure... I am having this issue at client location & I get this information in the log file.I copied directly from log and pasted it here. – user3553237 Apr 28 '14 at 04:40
  • 2
    possible duplicate of [Apostrophe in Email Address throwing Exception](http://stackoverflow.com/questions/23298246/apostrophe-in-email-address-throwing-exception) – Bill Shannon Apr 28 '14 at 06:44
  • 2
    @BillShannon is not "possible duplicate" it is actually the same thing! – morgano Apr 28 '14 at 06:47

3 Answers3

1

Assuming that email address with ` is allowed, I'd try to change this

addressTO[i] = new InternetAddress(toAddressesArray[i]);

to this

addressTO[i] = new InternetAddress(toAddressesArray[i].replace("`", "\\`"));

Skip ` character with \.

Tin
  • 794
  • 5
  • 10
0

In accordance with RFC2822 "3.4.1. Addr-spec specification" & "3.2.5. Quoted strings" you are able to use such characters (') if it is escaped.

To escape single character you can use "\"; to escape multiple characters use double quotes. Following addresses will be valid RFC2822 addresses: Michael.O\’Hara@sampleDomain.com "Michael.O’Hara"@sampleDomain.com

PS would be nice to see full stacktrace, might be you can't send mail due to some different problem.

Grigory
  • 465
  • 5
  • 16
-2

Here is a link which discusses same problem caused by some email servers that are not compliant with RFC821 (the Internet standard for SMTP messages)

[1]: http://searchexchange.techtarget.com/tip/Beware-of-the-apostrophe

Dhan
  • 42
  • 2
  • Link only answers are not the best one (a comment is better or extract the relevant info in your answer). This is one particularly annoying since you need to register to get the content. – RealHowTo Nov 29 '14 at 20:48