<%@ page import="java.lang.,java.util.,javax.mail.,javax.mail.internet.,
javax.activation.*" %>
<%
String p_to = "abc@hotmail.com"; // Please fill in your email here
String p_from = "abcd@yahoo.com.sg"; // Please fill in receipient’s email here
String p_subject = "Testing";
String p_message = "This is a test email";
String l_host = "smtp.mail.yahoo.com.sg";
// Gets the System properties
Properties l_props = new Properties();
// Puts the SMTP server name to properties object
l_props.put("mail.smtp.host", l_host);
l_props.put("mail.smtp.port","25");
l_props.put("mail.transport.protocol","smtp");
l_props.put("mail.smtp.auth", "true");
l_props.put("mail.user", "ancde"); 
l_props.put("mail.password","password"); 
// Get the default Session using Properties Object
Session l_session = Session.getInstance(l_props, null);
l_session.setDebug(false); // Enable the debug mode
try {
MimeMessage l_msg = new MimeMessage(l_session); // Create a New message
l_msg.setFrom(new InternetAddress(p_from)); // Set the From address
l_msg.setRecipients( Message.RecipientType.TO
, InternetAddress.parse(p_to, false));
l_msg.setSubject(p_subject); // Sets the Subject
// Create and fill the first message part
MimeBodyPart l_mbp = new MimeBodyPart();
l_mbp.setText(p_message);
// Create the Multipart and its parts to it
Multipart l_mp = new MimeMultipart();
l_mp.addBodyPart(l_mbp);
// Add the Multipart to the message
l_msg.setContent(l_mp);
// Set the Date: header
l_msg.setSentDate(new Date());
// Send the message
Transport.send(l_msg);
// If program reaches this point, then message is successfully sent.
out.print("Success");
} catch (MessagingException mex) { // Trap the MessagingException Error
out.print("Failure: Messaging Exception: " + mex);
} catch (Exception e) {
out.print("Failure: General Exception: " + e);
e.printStackTrace();
}
%>

i get this error message
Failure: Messaging Exception: javax.mail.AuthenticationFailedException
How do i solve this?
All help will be greatly appreciated thanks

Dani AI

Generated

The javax.mail.AuthenticationFailedException shown in ’s trace means the SMTP server refused the login attempt (bad credentials, blocked/auth method mismatch, or a protocol/security requirement). The JavaMail API treats that as an authentication failure when connect/send cannot log in to the remote SMTP service. (docs.oracle.com)

Common root causes and corrective actions:

  • Use the provider’s documented outgoing server, port and security (Yahoo’s SMTP uses smtp.mail.yahoo.com with SSL/TLS on 465 or STARTTLS on 587; the login is the full Yahoo address). (help.yahoo.com)
  • If the account has 2‑step/OAuth protections, a third‑party app password or OAuth2 is required instead of the main account password — generate an app password via Yahoo’s Account Security for non‑OAuth clients. (help.yahoo.com)
  • Make sure credentials are actually supplied to the mail Session (don’t rely on setting only property keys and passing a null Authenticator). The API supports supplying an Authenticator or calling the send/connect variant that accepts username/password. (jakarta.ee)

Example pattern (server-side code; keep out of JSP):

Properties props = new Properties();
// set host/port and enable TLS/SSL as required by the provider
// e.g. STARTTLS on 587 or SSL on 465

Session session = Session.getInstance(props, new javax.mail.Authenticator() {
  protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("fulladdress@yahoo.com", "app-or-account-password");
  }
});

(Use the provider’s documented host/port + the app password or OAuth token when required.) (help.yahoo.com)

Operational advice and best practice: avoid placing SMTP logic or credentials inside a JSP; move it to a servlet/bean and configure a server‑side mail Session (JNDI) so credentials live in server config rather than page source. Enable JavaMail debug or capture the SMTP log to see the server reply (helps pinpoint whether the server requested STARTTLS, rejected the password, or blocked the port). (tomcat.apache.org)

Checklist (quick): correct host/port + SSL vs STARTTLS, full email as username, use app password or OAuth when required, provide credentials via Authenticator or the connect/send API, enable debug and test network reachability to the SMTP port.

Recommended Answers

All 3 Replies

  1. You do not want to do this sort of thing in JSP which is for view, you need to do it in an object or method called by servlet
  2. You need to do some reading may be this can help
  3. You need to find out how Yahoo enable 3rd parties to use their email services

Oh i see thanks for the help.
Do you have any idea how yahoo will allow that?

Nope, but if you search forum there been already few attempts so maybe some of them got it right, otherwise you need check Yahoo resources

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.