Link to home
Start Free TrialLog in
Avatar of nguyen_85
nguyen_85

asked on

JavaMail

I'm trying to use the JavaMail to send and receive message through  my yahoo account which has pop3 and smtp but I keep getting this error:
"javax.mail.MessagingException: 530 authentication required ".
How do I set this authentication on my code?

Below is the code that I use:

<%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*" %>
<%
  Properties props = new Properties();
  props.put("mail.smtp.host", "smtp.mail.yahoo.com");
  Session s = Session.getInstance(props);

  MimeMessage message = new MimeMessage(s);

  InternetAddress from = new InternetAddress("myemailaddress@yahoo.com");
  message.setFrom(from);
  InternetAddress to = new InternetAddress("myemailaddress@yahoo.com");
  message.addRecipient(Message.RecipientType.TO, to);

  message.setSubject("Test from JavaMail.");
  message.setText("Hello from JavaMail!");

  // Next two lines are specific to Yahoo
  Store store = s.getStore("pop3");
  store.connect("pop.mail.yahoo.com", "my_username", "my_password");

  Transport.send(message);

  // Next line is specific to Yahoo
  store.close();
%>

Thanks!

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nguyen_85
nguyen_85

ASKER

Sorry I don't know too much about authentication.The "nguyen_85" and the "secret" that you wrote on the method parameter, do I replace that with my username and password of my yahoo account?

Thanks!
yes sorry, they are username and password.
Sorry object, now i get this error:
javax.servlet.ServletException: Sending failed;
  nested exception is:
 javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
 javax.mail.SendFailedException: 550 5.7.1 Unable to relay for nguyen_8585@yahoo.com

my codes below:

<%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*" %>
<%
  Properties props = new Properties();
  props.put("mail.smtp.auth", "true");
  Authenticator auth = new com.wrox.authentication.SMTPAuthenticator();
  Session s = Session.getDefaultInstance(props, auth);

  MimeMessage message = new MimeMessage(s);

  InternetAddress from = new InternetAddress("my_yahoo_address.com");
  message.setFrom(from);
  InternetAddress to = new InternetAddress("my_yahoo_address.com");
  message.addRecipient(Message.RecipientType.TO, to);

  message.setSubject("Test from JavaMail.");
  message.setText("Hello from JavaMail!");

  // Next two lines are specific to Yahoo
  Store store = s.getStore("pop3");
  store.connect("pop.mail.yahoo.com", "my_username", "mypassword");

  Transport.send(message);

  // Next line is specific to Yahoo
  store.close();

%>
Does your mail server allow relaying?
where do you specify the smtp server?
sorry, How do I find  out if my mail server allow relaying? I'm just using the free yahoo mail account accept I bought the upgrade in order to use the pop3 and smtp.
that error tends to indicate that relaying is not supported.
make sure your from address is your valid address.
Do you know any site that offer this feature so I can test my JavaMail. It's for a school project. I raise up your point to 450 for you trouble.
Thanks!
what is the name of the server you are currently using?
I don't know if this make any sense, but I'm using the server on yahoo on the the regular email account that you get for free. I think it's call
pop.mail.yahoo.com(for pop3)  and smtp.mail.yahoo.com(fpr smtp).
> and smtp.mail.yahoo.com(fpr smtp).

where do u specify that in your code?

Can you send mail using that server from other mail programs (outlook, eudora etc)
I can send mail using that server from programs like outlook.

The code that I used:
<%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*" %>
<%
  Properties props = new Properties();
  props.put("mail.smtp.auth", "smtp.mail.yahoo.com");

//I create a class that you wrote
  Authenticator auth = new com.wrox.authentication.SMTPAuthenticator();

  Session s = Session.getDefaultInstance(props, auth);

  MimeMessage message = new MimeMessage(s);

  InternetAddress from = new InternetAddress("nguyen_8585@yahoo.com");
  message.setFrom(from);
  InternetAddress to = new InternetAddress("nguyen_8585@yahoo.com");
  message.addRecipient(Message.RecipientType.TO, to);

  message.setSubject("Test from JavaMail.");
  message.setText("Hello from JavaMail!");

  // Next two lines are specific to Yahoo
  Store store = s.getStore("pop3");
  store.connect("pop.mail.yahoo.com", "my"_user_name, "my_pass_word");

  Transport.send(message);

  // Next line is specific to Yahoo
  store.close();

%>
try using the following properties:

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.mail.yahoo.com");
props.put("mail.smtp.auth", "true");
Thanks a million objects it works! I raise your points to 500. I needed this code!