Search code examples
javaemailgmail

Java Mail is sending to Wrong Address


When I am trying to send an email to [email protected] it ends up being sent to [email protected]


Send Email Code:

  String to = "[email protected]";
  String from = "[email protected]";
  String host = "localhost";
  Properties properties = System.getProperties();
  properties.setProperty("mail.smtp.host", host);
  Session session = Session.getDefaultInstance(properties);

  try{
     MimeMessage message = new MimeMessage(session);
     message.setFrom(new InternetAddress(from));
     message.addRecipient(Message.RecipientType.TO,
                              new InternetAddress(to));
     message.setSubject("This is the Subject Line!");
     message.setText("This is actual message");

     Transport.send(message);
     System.out.println("Sent message successfully....");
  }catch (MessagingException mex) {
     mex.printStackTrace();
  }

It works correctly with other emails, why doesn't this one work?


Solution

  • Google Mail API suggests and states that:

    Gmail doesn't recognize dots as characters within usernames, you can add or remove the dots from a Gmail address without changing the actual destination address; they'll all go to your inbox, and only yours.

    For example:

    All these addresses belong to the same person. You can see this if you try to sign in with your username, but adding or removing a dot from it. You'll still go to your account.


    Google Apps does recognize dots. If you'd like to have a dot in your username, please ask your domain administrator to add your preferred username as a nickname.