tags:

views:

361

answers:

2

Hi The following code causes an error. Please help me understand what's wrong.

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendMail
{
  public static void main(String [] args)throws MessagingException
  {
    SendMail sm=new SendMail();
     sm.postMail(new String[]{"[email protected]"},"hi","hello","[email protected]");
   }

public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
{
    boolean debug = false;

     //Set the host smtp address
     Properties props = new Properties();
     props.put("mail.smtp.host", "webmail.emailmyname.com");

    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(debug);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
    for (int i = 0; i < recipients.length; i++)
    {
        addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);


    // Optional : You can also set your custom headers in the Email if you Want
    msg.addHeader("MyHeaderName", "myHeaderValue");

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport.send(msg);
}
}

Exception:
<pre>
com.sun.mail.smtp.SMTPSendFailedException: 450 smtpout04.dca.untd.com Authentication required

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886)
    at javax.mail.Transport.send0(Transport.java:191)
    at javax.mail.Transport.send(Transport.java:120)
    at SendMail.postMail(SendMail.java:52)
    at SendMail.main(SendMail.java:10)

A: 

Note that many, many ISPs block access to external port 25 on servers outside of their network. Instead, the ISP forces you to use their SMTP server.

If you're getting "authentication required", you must first enter your user name and password and issue at least one request such as check for new mail. Even though SMTP does not require a username and password to SEND email, many SMTP servers still implement this by making you log in and check your mail via POP or IMAP before you can send any.

Marcus Adams
can I have code for that?
where should I specify username and password?
This is done via POP or IMAP.
Marcus Adams
SMTP Authentication actually doesn't have anything to do with POP or IMAP authentication (although the provider generally uses the same authentication credentials for both SMTP and POP/IMAP).
Craig Walker
@Craig, My web host provider has this configuration. I can only send mail after "checking my mail". I am not required to setup SASL, SSL, SSH, or any such thing. It's called POP before SMTP, and it's way more common than "SMTP Auth". If his SMTP server were using "SMTP Auth", he would get a 530 response, not 450. A 450 response is most likely "POP before SMTP".
Marcus Adams
A: 

The "Authentication required" in the exception message suggests that the target SMTP server requires you to log in (Perhaps via TLS or SSL). This wasn't common on SMTP servers until a few years ago (it's an anti-spam measure) so it's easy to overlook.

To authenticate with JavaMail:

To use SMTP authentication you'll need to set the mail.smtp.auth property (see below) or provide the SMTP Transport with a username and password when connecting to the SMTP server. You can do this using one of the following approaches:

  • Provide an Authenticator object when creating your mail Session and provide the username and password information during the Authenticator callback.

    Note that the mail.smtp.user property can be set to provide a default username for the callback, but the password will still need to be supplied explicitly.

    This approach allows you to use the static Transport send method to send messages.

  • Call the Transport connect method explicitly with username and password arguments.

This approach requires you to explicitly manage a Transport object and use the Transport sendMessage method to send the message. The transport.java demo program demonstrates how to manage a Transport object. The following is roughly equivalent to the static Transport send method, but supplies the needed username and password:

Transport tr = session.getTransport("smtp");

tr.connect(smtphost, username, password);

msg.saveChanges(); // don't forget this

tr.sendMessage(msg, msg.getAllRecipients());

tr.close();

Craig Walker
how to login on smtp server?
I googled up the Javadoc and pasted a quote from it.
Craig Walker
Your server is not using "SMTP Auth". You would get a 530 response, not a 450 response if it were using "SMTP Auth". With a response of 450, the server is most likely using "POP before SMTP". You need to connect via POP or IMAP before sending mail.
Marcus Adams