Sending SMTP email through James with Authentication
843830May 29 2002 — edited May 30 2002Hello,
I have a problem I have been trying to solve for about a month now. I have an email server James (latest version) setup on Linux and configured to accept authenticated SMTP connections. I know it works fine because I have been using it with Outlook Express for sometime now. I am now trying to send email from Java through James. I used to do it through Yahoo, but Yahoo is not free anymore, so I am trying to use my own server. If I disable authentication on James, I can send emails with no problems. If I enable SMTP authentication, then I can only send email to local users. The exception I am getting is:
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
javax.mail.SendFailedException: 530 Authentication Required
at javax.mail.Transport.send0(Transport.java:219)
at javax.mail.Transport.send(Transport.java:81)
at com.andrey.email.test.main(test.java:51)
Exception in thread "main"
The address I am using is valid. I think the real exception is 530 Authentication Required. I have tried everything I found on this forum and still can't get it to work. The code is below. If anyone has any ideas, I would really appreciate it.
thanks
Andrey
ap148@hotmail.com
--------------------------- CODE ---------------------------------------------------------
import com.sun.mail.smtp.*;
import java.util.* ;
import javax.mail.*;
import javax.mail.internet.*;
public class test
{
public static void main(String [] args)
throws Exception
{
Properties props = System.getProperties();
String to = "to@yahoo.com";
String user = "user";
String pwd = "pwd";
String from = "user@server";
String smtp = "smtp.server.net";
// Setup mail server
props.put("mail.smtp.host", smtp);
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", "smtp");
props.put("mail.user", user);
props.put("mail.from", from);
props.put("mail.to", to);
// Get session
Session session = Session.getInstance(props, new ServerAuthenticate1(user, pwd));
session.setDebug(false);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Hello JavaMail");
message.setText("Welcome to JavaMail");
// save message
message.saveChanges();
Transport tpt = session.getTransport("smtp");
// Send message
tpt.send(message);
}
}
class ServerAuthenticate1 extends Authenticator
{
String smtpUsername = null;
String smtpPassword = null;
public ServerAuthenticate1(String username, String password)
{
smtpUsername = username;
smtpPassword = password;
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(smtpUsername,smtpPassword);
}
}