title: Unable to send email using servlet and JavaMail
body: <p>I am working on sending emails using <code>JavaMail</code>,but getting error as <code>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</code></p>
  
  <ul>
  <li>What i am doing is, i have a class as <code>EmailUtility</code>.</li>
  <li>The above class has one static method, <code>sendEmail()</code> – which takes SMTP server settings and message details as its arguments. </li>
  <li>I am putting SMTP server settings in my web.xml file 
  but don't know what is going wrong</li>
  </ul>
  
  <p>My <code>EmailUtility</code> class</p>
  
  <pre><code>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);
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>here is my web.xml file</p>
  
  <pre><code> &lt;context-param&gt;
      &lt;param-name&gt;host&lt;/param-name&gt;
      &lt;param-value&gt;smtp.gmail.com&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;port&lt;/param-name&gt;
      &lt;param-value&gt;587&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;user&lt;/param-name&gt;
      &lt;param-value&gt;test.123@gmail.com&lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;pass&lt;/param-name&gt;
      &lt;param-value&gt;12345698&lt;/param-value&gt;
  &lt;/context-param&gt;
  </code></pre>
  
  <p>And here is my servlet class</p>
  
  <pre><code>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();
      }
  </code></pre>
  
  <p>I am doing the right thing but don't know what is the issue</p>
  
  <p><strong>If I enable Less Secure  App setting in gmail then its working fine</strong>, i don't think it is the right approach to solve the issue as not every user is going to do that</p>
  
  <p>So guys help me out, i don't know what bizarre i am making, thankyou  </p>
  
  <p><strong>Edit</strong> 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</p>