1

I have a mail service class which sends email using Gmail SMTP, it has been working successfully till the recent past, the same class is not able to send e-mails and I don't even see any error messages when I debug or run this code. Any idea, what is happening?

public class MailService {

public static void sendEmail(String subject, String msgBody, String[] toEmails, 
        String[] ccEmails, String[] bccEmails,
        String fromEmail, String toName){

     Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", true); // added this line
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.user", "mygmail-id");
        props.put("mail.smtp.password", "mypassword");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", true);



        Session session = Session.getInstance(props,null);
    List<InternetAddress> toAdresses = null;
    List<InternetAddress> ccAdresses = null;
    List<InternetAddress> bccAdresses = null;
    try {
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(fromEmail));
        toAdresses = new ArrayList<InternetAddress>();
        ccAdresses = new ArrayList<InternetAddress>();
        bccAdresses = new ArrayList<InternetAddress>();
        for(String toEmail: toEmails){
            toAdresses.add(new InternetAddress(toEmail));
        }
        if(ccEmails != null && ccEmails.length > 0)
        for(String ccEmail: ccEmails){
            ccAdresses.add(new InternetAddress(ccEmail));
        }
        if(bccEmails != null && bccEmails.length > 0)
        for(String bccEmail: bccEmails){
            bccAdresses.add(new InternetAddress(bccEmail));
        }
        msg.addRecipients(Message.RecipientType.TO,
                toAdresses.toArray(new InternetAddress[toAdresses.size()]));
        msg.addRecipients(Message.RecipientType.CC,
                ccAdresses.toArray(new InternetAddress[ccAdresses.size()]));
        msg.addRecipients(Message.RecipientType.BCC,
                bccAdresses.toArray(new InternetAddress[bccAdresses.size()]));
        msg.setSubject(subject);
        msg.setContent(msgBody, "text/html");
        Transport.send(msg);

    } catch (AddressException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
        // ...
    }
}
Unheilig
  • 16,196
  • 193
  • 68
  • 98
user1614862
  • 3,701
  • 7
  • 29
  • 46
  • 1
    Have you had a look at [this](http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/) or [this](http://stackoverflow.com/questions/15597616/sending-email-via-gmail-smtp-server-in-java)? – MadProgrammer Oct 24 '15 at 01:26
  • That code would never work. There is no mail.smtp.password property. You're not doing anything to pass the username and password to the Transport. Use one of the connect methods that allow you to pass the username and password and don't bother setting them as properties. – Bill Shannon Oct 24 '15 at 05:27
  • @MadProgrammer, I have tried all the ways, the above one is one of the options I tried, but none of these working. – user1614862 Oct 25 '15 at 19:07

1 Answers1

0

Two things:

As @Bill Shannon suggested, you should go with an Authenticator instead of relying on Properties alone:

Session session = Session.getInstance(props, new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("my-gmail-id", "mypassword");
            }

        });

But even then - Gmail will block your email from being sent due to security issues and gives this link for further information.

Jan
  • 13,738
  • 3
  • 30
  • 55