9 Replies Latest reply on Nov 13, 2011 7:00 AM by Bill Shannon-Oracle

    Oddity with setFrom() and setFrom(InternetAddress)

      I have an odd behaviour. If I have mail.user set as a session property, I should be able to call setFrom() with no arguments and send mail.

      However if I do that I have an odd behaviour: I can't send mail To: another user in the same domain as mail.user. At least that seems to be the distinguishing feature.

      However if I call MimeMessage.setFrom(new InternetAddress(session.getProperties().get("mail.user"))), it works.

      There are no SMTP errors or exceptions in either case, it's just that the To: doesn't get through. However cc:s to users in other domains do get through in either case.

      This seems pretty odd, as I am only doing in longhand what I would have thought setFrom() (no args) does anyway.

      This could be a peculiarity of the SMTP server, which is "smtpout.secureserver.net:3535", plaintext. I don't know anything about it technically.

      I can provide some userids and authentications offline if that will help, and I can post some code here, but it is basically exactly the same as per the example here modulo how the from-address gets set, and providing a proper Authenticator.
        • 1. Re: Oddity with setFrom() and setFrom(InternetAddress)
          Bill Shannon-Oracle
          How are you creating the Session?

          What does the protocol trace show?

          You can see what the message looks like before you send it by adding something like this right before you send it:

          msg.writeTo(new FileOutputStream("msg.txt"));
          • 2. Re: Oddity with setFrom() and setFrom(InternetAddress)
            I'll have to answer those questions one at a time. Code follows. Protocol messages won't follow till next week, sorry.
                      MimeMessage     mimeMessage = new MimeMessage(session);
                      // DOESN'T WORK
            //          mimeMessage.setFrom();     // i.e. from 'mail.user'
                      String     from = session.getProperties().getProperty("mail.user");
                      mimeMessage.setFrom(new InternetAddress(from));
                      mimeMessage.setRecipients(Message.RecipientType.TO, to);
                      for (String cc : ccs)
                           mimeMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
                      mimeMessage.setSentDate(new Date());
                      // create and fill the first message part
                      MimeBodyPart mbp1 = new MimeBodyPart();
                      mbp1.setText(message, "UTF-8", subType);
                      Multipart mp = new MimeMultipart();
            The Session is essentially a Tomcat Resource configured thusly:
                   mail.user="xxx@yyycom"  // suppressed from publication
            • 3. Re: Oddity with setFrom() and setFrom(InternetAddress)
              Bill Shannon-Oracle
              MimeMessage.setFrom() uses the result of InternetAddress.getLocalAddress(), which says:

              Return an InternetAddress object representing the current user. The entire email address may be specified in the "mail.from" property. If not set, the "mail.user" and "mail.host" properties are tried. If those are not set, the "user.name" property and InetAddress.getLocalHost method are tried. Security exceptions that may occur while accessing this information are ignored. If it is not possible to determine an email address, null is returned.

              I guess it's not explicit enough, but if mail.from isn't set it tries to use an address of the form <mail.user>@<mail.host>.
              If mail.host isn't set, that's not going to be a valid address, so it doesn't use it. If mail.user has a "@" in it, it will try to
              quote it so that the whole property value is taken as the user name.

              Bottom line: set mail.from.
              • 4. Re: Oddity with setFrom() and setFrom(InternetAddress)
                Ah thanks, that explains part of it. Doesn't explain how the CCs got through though, and why no exceptions or SMTP errrors. I'll post logs on Monday.
                • 5. Re: Oddity with setFrom() and setFrom(InternetAddress)
                  Bill Shannon-Oracle
                  Yes, you're right, I'm not sure why there was no exception. If InternetAddress.getLocalAddress() returns null, MimeMessage.setFrom() should throw an exception.

                  What version of JavaMail are you using?

                  What's the output of this program?
                  import javax.mail.*;
                  import javax.mail.internet.*;
                  import java.util.*;
                  public class setfrom {
                      public static void main(String args[]) throws Exception {
                          Session session = Session.getInstance(new Properties());
                          MimeMessage m = new MimeMessage(session);
                  • 6. Re: Oddity with setFrom() and setFrom(InternetAddress)
                    I'll provide more outputs tomorrow but re your comments about mail.user and mail.host, that means that mail.host is badly misdescribed in the Javadoc. From what you say it is used as the host part of the user's mail address, but the Javadoc says it is "The default host name of the mail server".
                    • 8. Re: Oddity with setFrom() and setFrom(InternetAddress)
                      Well exactly but that's not what it says in the javax.mail package documentation. Using it for both also doesn't actually make sense, as my configuration above shows, although I appreciate that isn't going to change.

                      BTW I am using JavaMail 1.4.4.
                      • 9. Re: Oddity with setFrom() and setFrom(InternetAddress)
                        Bill Shannon-Oracle
                        Back in the olden days... :-) user "joe" would connect to his mail server "myserver.com" and his email
                        address would be "joe@myserver.com". Setting mail.host to "myserver.com" and mail.user to "joe",
                        and combining them to form the default From address, often gave the expected result. And it still does
                        in many cases. Just not in your case.

                        It's true that the package level documentation doesn't describe everything the mail.host and mail.user
                        properties are used for, and the InternetAddress documentation could probably be clearer about how it
                        uses them.