JavaMail sending problem and solution

Discussion in 'Java' started by Bobby Martin, May 19, 2004.

  1. Bobby Martin

    Bobby Martin Guest

    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 :)
     
    Bobby Martin, May 19, 2004
    #1
    1. Advertising

  2. Bobby Martin

    GaryM Guest

    (Bobby Martin) wrote in
    news::

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


    Bobby, I am curious why Transport.connect(host, user, password) did not
    work for you?

    Gary
     
    GaryM, May 20, 2004
    #2
    1. Advertising

  3. Bobby Martin

    Roedy Green Guest

    On 19 May 2004 12:16:09 -0700, (Bobby Martin)
    wrote or quoted :

    >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


    That technique is astoundingly general. You just set up your
    authenticator and Java deals with any challenges of any sort ever
    after. It is feels almost magic the first time you use it. I used it
    in the Replicator to deal with protected HTTP documents.
    see http://mindprod.com/jgloss/authentication.html

    However, I did not use it in my bulk emailer program. I just used the
    more conventional:

    Properties props = System.getProperties();
    if ( CustConfig.needPasswordToSend )
    {
    props.setProperty ( "mail.smtp.auth", "true" );
    }
    // Get a Session object
    session = Session.getDefaultInstance( props, null );

    ....

    Transport transport = session.getTransport( sendProtocol );
    transport.connect( sendHost, sendPort, sendLoginID, sendPassword );


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, May 20, 2004
    #3
  4. Bobby Martin

    Bobby Martin Guest

    GaryM <> wrote in message news:<Xns94EF4BC168555R3344324357788499939@216.168.3.44>...
    > (Bobby Martin) wrote in
    > news::

    [SNIP]
    > Bobby, I am curious why Transport.connect(host, user, password) did not
    > work for you?
    >
    > Gary


    I ran my smtp connection through an echoing tunnel I built for just
    such occasions. Even using Transport.connect, JavaMail did not send
    AUTH LOGIN to the server followed by an encrypted username and
    password, while Outlook did. When I turned on mail.smtp.auth and
    added an Authenticator, JavaMail did send the AUTH LOGIN.

    I can't tell you why it behaves that way - it surprised me very much
    that Transport.connect seemed took a user & password but didn't use
    them in any way. But I can tell you from experience that, at least
    for me, it does behave that way.

    Bobby
     
    Bobby Martin, May 21, 2004
    #4
  5. Bobby Martin

    Bobby Martin Guest

    Roedy Green <> wrote in message news:<>...
    > On 19 May 2004 12:16:09 -0700, (Bobby Martin)
    > wrote or quoted :

    [SNIP]
    >
    > Properties props = System.getProperties();
    > if ( CustConfig.needPasswordToSend )
    > {
    > props.setProperty ( "mail.smtp.auth", "true" );
    > }
    > // Get a Session object
    > session = Session.getDefaultInstance( props, null );
    >
    > ...
    >
    > Transport transport = session.getTransport( sendProtocol );
    > transport.connect( sendHost, sendPort, sendLoginID, sendPassword );


    Hmm, I should have read both replies before I responded to the first
    one :) I'm not entirely sure that I tried turning on mail.smtp.auth,
    using Transport.connect, and not using an Authenticator. It's
    possible that would have worked for me. Something for me to
    experiment with :)

    Thanks for the insight!
    Bobby
     
    Bobby Martin, May 21, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nilambari

    problem sending javamail

    Nilambari, Dec 23, 2003, in forum: Java
    Replies:
    2
    Views:
    8,379
    Ulas Ergin
    Dec 26, 2003
  2. Steve D. Perkins

    JavaMail sending email with no body content

    Steve D. Perkins, Dec 8, 2004, in forum: Java
    Replies:
    1
    Views:
    3,679
    Graeme Hill
    Dec 8, 2004
  3. Replies:
    1
    Views:
    932
    Roedy Green
    Apr 13, 2006
  4. sickness

    Problems sending email javamail

    sickness, May 16, 2006, in forum: Java
    Replies:
    3
    Views:
    13,482
    sickness
    May 17, 2006
  5. mlabhard

    javamail not sending

    mlabhard, Jun 6, 2006, in forum: Java
    Replies:
    1
    Views:
    536
    Andy Flowers
    Jun 7, 2006
Loading...

Share This Page