Discussions

EJB design: problem sending mails to external domains with Java Mail

  1. Hi All,

    I am writing a Java mail program which sends mails to different people. I am able to send mails to email ids of the same domain as of my company but couldnot send to external addresses. But I am able to send to external email addresses through my outlook and Lotus Notes.

    If my SMTP server doesn't relay, can I still send mails to external domains through my mail client like outlook and Lotus? If not, why is my program failing.

    Is there any work around??

    Please Help

    Thanks in Advance
    shankar
  2. Hi Shankar,

    This is really a configuration option on your SMTP server. I would force authentication for relaying for a given user. The other thing to note, some servers do this implicitly, so they expect you to do your pop3 "read"
    and you authenticate there, then when you come back to send it "knows" who
    you are. This is horribly insecure. Have a read of your SMTP servers docs.

    Regards,
    Max
  3. all depends on your smtp relaying server configuration.. You could have setup where the relaying server only allows relaying within the domain, hence all your mails are not going out..

    Try accessing the smtp sever through telnetting on port 25 and see what the error says.

    Hope this helps
    a
  4. Hi,
      When you send email to a non local client(external addresses) from SMTP server , there are two conditions in which the email will be sent by the smtp server.

    1> Relay is turned on, and so the smtp server relays to non local addresses.

    2> Relay is not turned on , BUT the smtp server knows that the sender of the email is a real email address and is authenticated ( you must have entered your password when you logged in your lotus/outlook client).

    In order to send an email to non local email addresses without relaying, your java program must use a user id which can be authenticated with a password. An example code is pasted below.

    props.put("mail.smtp.host",host);
    props.put("mail.smtp.auth","true");
    ...
    Session session = Session.getDefaultInstance(mProps,null);
    ...
    Transport transport = session.getTransport("smtp");
    transport.connect(host,userName,password);
    msg.saveChanges();
    transport.sendMessage(msg,msg.getAllRecipients());

    Note that userName and password are valid userids on the SMTP server.

    This worked for me.

    Also you may want to read the docs about the differences in using the static send method of Transport, versus doing it this way.

    hope this helps.
    satish






    > Hi All,
    >
    > I am writing a Java mail program which sends mails to different people. I am able to send mails to email ids of the same domain as of my company but couldnot send to external addresses. But I am able to send to external email addresses through my outlook and Lotus Notes.
    >
    > If my SMTP server doesn't relay, can I still send mails to external domains through my mail client like outlook and Lotus? If not, why is my program failing.
    >
    > Is there any work around??
    >
    > Please Help
    >
    > Thanks in Advance
    > shankar