简体   繁体   中英

Unable to send email using servlet and JavaMail

I am working on sending emails using JavaMail ,but getting error as javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials g188sm3298732pfc.24 - gsmtp javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials g188sm3298732pfc.24 - gsmtp

  • What i am doing is, i have a class as EmailUtility .
  • The above class has one static method, sendEmail() – which takes SMTP server settings and message details as its arguments.
  • I am putting SMTP server settings in my web.xml file but don't know what is going wrong

My EmailUtility class

public class EmailUtility {
public static void sendEmail(String host, String port, final String userName, final String password,
        String toAddress, String subject, String message) throws AddressException, MessagingException {

    // setting SMTP server properties
    Properties properties = new Properties();
    properties.put("mail.smtp.host", host);
    properties.put("mail.smtp.port", port);
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
        }
    });
    // creates a new e-mail message
    Message msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(userName));
    InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
    msg.setRecipients(Message.RecipientType.TO, toAddresses);
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    msg.setText(message);

    // sending the e-mail
    Transport.send(msg);

}

}

here is my web.xml file

 <context-param>
    <param-name>host</param-name>
    <param-value>smtp.gmail.com</param-value>
</context-param>

<context-param>
    <param-name>port</param-name>
    <param-value>587</param-value>
</context-param>

<context-param>
    <param-name>user</param-name>
    <param-value>test.123@gmail.com</param-value>
</context-param>
<context-param>
    <param-name>pass</param-name>
    <param-value>12345698</param-value>
</context-param>

And here is my servlet class

public void init() {
    // reading SMTP server setting from web.xml file
    ServletContext context = getServletContext();
    host = context.getInitParameter("host");
    port = context.getInitParameter("port");
    user = context.getInitParameter("user");
    pass = context.getInitParameter("pass");
}

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // reading form fields
    String recipient = request.getParameter("recipient");
    String subject = request.getParameter("subject");
    String content = request.getParameter("content");
    System.out.println(recipient+" sub "+subject+" content "+content);

    String resultMessage = "";

    try {
        EmailUtility.sendEmail(host, port, user, pass, recipient, subject,
                content);
        resultMessage = "The e-mail was sent successfully";
    } catch (Exception ex) {
        ex.printStackTrace();
        resultMessage = "There were an error: " + ex.getMessage();
    }

I am doing the right thing but don't know what is the issue

If I enable Less Secure App setting in gmail then its working fine , i don't think it is the right approach to solve the issue as not every user is going to do that

So guys help me out, i don't know what bizarre i am making, thankyou

Edit is there any other resource by which i can send mails on cross platforms like from gmail to yahoo like that, i am open to use any other resource which can do the task i am trying to

Different servers have different security requirements. If you want to handle all of them, you're going to need some special cases in your application.

Sometimes you'll need the user to create an app-specific password for the email server.

Sometimes you'll need to support OAuth if the server supports it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM