JavaMail sending problem and solution

From: Bobby Martin (bobbymartin_at_hotmail.com)
Date: 05/19/04


Date: 19 May 2004 12:16:09 -0700

I had chronic problems with sending email via smtp. I could sign in
via a mail client and send to a remote user just fine, but JavaMail
refused to send to any but local users, citing a 550 relaying problem.
 All of the references I could find on the web told me to talk to my
mail admin, but I'm he and I also knew that if my mail client could do
it, so could I.

This solution requires that you log into the mail server as a normal
email user - you will have to create an (or use an existing) account.
You turn on smtp authorization by setting the a property you pass in
to get a session: the property is mail.smtp.auth and you set it to
"true". Then you create an Authenticator that returns a
PasswordAuthentication with your username and password in it.

Here's an excerpt of working code:

    Authenticator authenticator = new Authenticator() {
      public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(FROM, PASSWORD);
      }
    };
    
    Session mailSession = Session.getInstance(getMailProperties(),
authenticator);

    Message email = new MimeMessage(mailSession);
    try {
      email.setFrom(new InternetAddress(FROM));
      email.setSentDate(new Date());

      email.setRecipients(Message.RecipientType.TO,
stringsToInternetAddresses(to));
      email.setRecipients(Message.RecipientType.CC,
stringsToInternetAddresses(cc));
      email.setRecipients(Message.RecipientType.BCC,
stringsToInternetAddresses(bcc));
      email.setSubject(subject);
      email.setText(body);
      
      Transport.send(email);
      retval = true;
    } catch (Exception e) {
    }

My getMailProperties includes the following:
mail.smtp.host=some.mail.host.com
mail.smtp.auth=true

I just post this here for posterity so maybe someone else doesn't go
through the hell I did to figure this out :)



Relevant Pages

  • Re: Generate email from user
    ... The URL you are connecting to is an IP address over a SMTP port ... Grab the UID from the default mail client, ... that doesn't quite identify that you're using a mail server to send the ...
    (microsoft.public.vb.general.discussion)
  • Re: Changing the Display Name causes users to see the old and the new Names as two mailboxes
    ... I was talking about the mail client side (outlook) that doesn't account ... using SMTP at this point. ...
    (microsoft.public.exchange.admin)
  • Re: double line spacing in smtp
    ... >> MikeS wrote: ... >> IMHO it's best not to use an RDBMS as a mail client. ... > I'm not sure what is simpler than sending emails from Oracle. ... What's insecure about the unix mail client that isn't with Oracle's SMTP? ...
    (comp.databases.oracle.server)
  • Re: Cingular/AT&T blocks outside mail server use
    ... is to encrypt my login data, which is not used for SMTP, I'm OK with ... not using SSL for outbound email. ... authentication and my mail client doesn't support that, ...
    (alt.cellular.cingular)
  • Re: Cant change smtp port with ssl protection
    ... I didn't mention mail client because we have had similar ... but yes I have successfully setup SMTP with SSL. ... > submission port). ... No SMTP connector defined. ...
    (microsoft.public.exchange.admin)