Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

SMTP authorization

843830Jul 8 2001 — edited Feb 25 2010
Can someone tell me if there is any foolproof way for suthorising SMTP using Java Mail. I tried using the authenticator methods outlined in the Java Mail APIs, but it ois not working with few mwil servers.. The typical error message is:

Error: 530, Delivery not permitted for non-local users, try Authorising.

Any clues? Please help.

Thanks

Sridhar

Comments

843830
You have opened a session on a ESNMP server
It means you have to do an uthentification step
see : the description of the Package com.sun.mail.smtp
I have the same problem
If you have solved your problem ....
843830
Well.. I tried with another mail server.. This server is ESMTP CommuniGate Pro 3.4.7

here I am getting this error message..

SMTP Code 473: sridhar_r90@rediffmail.com you should authenticate first


The code snippet i used is as follows:

Properties props = System.getProperties();
props.put("mail.smtp.host", mailOption.outserver);
props.put("mail.smtp.auth","true");
props.put("mail.smtp.sendpartial", "true");

Authenticator auth = new BIMAuthenticator(mailOption.username, mailOption.password);

session = Session.getDefaultInstance(props, auth);
session.setDebug(true);
mimeMsg = makeMessage(session, tmpdir);

Transport.send(mimeMsg);

Even if i use the alternative method of connecting the transport and use sendMessage() method.. i get the same error..

The strange fact is both these servers work fine with MS Outlook.. I am still waiting for an answer

Thanks

sridhar
843830
in the description of the smtp provider :$YOURJAVADOCMAIL\javamail-1_2\javamail-1.2\docs\sundocs\com\sun\mail\smtp\package-summary.html
it says:
The SMTP provider also supports ESMTP (RFC 1651). It can optionally use SMTP Authentication (RFC 2554) using the LOGIN and PLAIN mechanisms (RFC 2592). Note, however, that SASL (RFC 2222 and RFC 2487) is not supported.

if your esmtp server requires an authentification,
The SMTP provider should works ...
I am in the same case as you and it does not work
I tried the two described solutions.
And I tried the two described solutions together.
And it does not work.....



the two described solutions :


To use SMTP authentication you'll need to 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();

843830
The solution is showed in the followind servlet code.

I hope this helps.

import java.io.*;
import java.util.Properties;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.mail.*;
import javax.mail.internet.*;
import com.sun.mail.smtp.*;

public class NuevoMail extends HttpServlet {

//please feel these fields with your data

static final String ToAddress = "xxxxxx@hotmail.com";
static final String host = "smtp.mail.yahoo.com"; //SMTP host
static final String from = "xxxxxxx@yahoo.com";
static final String user = "xxxxxx";
static final String pass = "*******";

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException ,IOException{

res.setContentType("text/html");

try {
//initialize the StringBuffer object within the try/catch loop
StringBuffer sb = new StringBuffer( );

//Get system properties
Properties props = System.getProperties( );

//Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.debug", "true");
props.put("mail.smtp.auth","true");


//Get session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
session.setPasswordAuthentication(new URLName("smtp",host,25,"INBOX",user,pass), new PasswordAuthentication(user,pass));

//Define message
MimeMessage msg = new MimeMessage(session);

//Set the from address
msg.setFrom(new InternetAddress(from));

//Set the to address
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(ToAddress));

//Set the subject
msg.setSubject("Test mail using JavaMail APIs");

//Set the text content for body
sb.append("This is the 1st String line.\n\n");
sb.append("This is the 2nd String line.\n\n");
sb.append("This is the 3rd String line.\n\n");
msg.setText(sb.toString( ));

//Send message
Transport tr = session.getTransport("smtp");

tr.connect(host, user, pass);
msg.saveChanges(); // don't forget this
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
}

catch (MessagingException e) {
System.out.println(e);
}
}

}

843830
I have problem with SMTP authorization and solve it with this code:

Properties props = new Properties();
props.put("mail.smtp.host", SMTPserver);
props.put("mail.smtp.auth","true");
Authenticator auth = new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(SMTPusername, SMTPpassword);
}
};
Session session = Session.getInstance(props, auth);
..............................

and so on...
843834
here is a sample for authorization of SMTP:
[java mail SMTP authentication|http://mhashem.wordpress.com/2010/02/24/java-send-email-using-javamail-api-through-smtp-with-password/]
dcminter
Locking zombie, blocking spam.
1 - 7
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Mar 25 2010
Added on Jul 8 2001
7 comments
292 views