Cyrus
Cyrus

Reputation: 31

Not getting any response while using JavaMail API

I am trying to send an email from a webpage to my email id using Gmail smtp server. I tried various answers on stackoverflow. Unfortunately, none of them worked.

   `public void sendMail(String msg){

    Properties props= System.getProperties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtps.debug", "true");
    props.put("mail.smtp.socketFactory.fallback", "false");
    //props.put("mail.smtp.ssl.enable", "true");
    //props.put("mail.transport.protocol", "smtp");

    Authenticator authenticate= new Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication(){
            return new PasswordAuthentication(username, password);
            }
            };
    @SuppressWarnings("deprecation")
    Session mailSession= Session.getInstance(props, authenticate);
    mailSession.setDebug(true);

    Message mailMessage= new MimeMessage(mailSession);

    try {
        mailMessage.setFrom(new InternetAddress(mailFrom));
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
        mailMessage.setSubject(subject);
        mailMessage.setText(msg);

        Transport trans= mailSession.getTransport("smtp");
        trans.connect("smtp.gmail.com", username, password);
        trans.sendMessage(mailMessage, mailMessage.getAllRecipients());
        trans.close();

    } catch (Exception ex) {
        Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex);
    }

}` 

I am trying above written code

DEBUG: setDebug: JavaMail version 1.5.4 Info: DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] Info: DEBUG SMTP: useEhlo true, useAuth true Info: DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false

And, this is I am getting as an output with no exceptions, no messages and not even an email to my email address. I am using JavaMail API 1.5.4 Java 8 and GlassFish Server as a localhost server with reference to an online tutorial.

Please help!!

Upvotes: 0

Views: 1524

Answers (2)

Cyrus
Cyrus

Reputation: 31

Finally, I got it right now I am able to send an email. Thank You @BillShanon, @KarlNicholas and Yashashvi Kaushik for your support. Here is the code that worked for me with my mentioned version of JavaMail Api

Properties props= System.getProperties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.auth", "true");

    props.put("mail.smtp.ssl.enable", "true");

    Authenticator authenticate= new Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication(){
            return new PasswordAuthentication(username, password);
            }
            };
    @SuppressWarnings("deprecation")
    Session mailSession= Session.getInstance(props, authenticate);
    mailSession.setDebug(true);


    Message mailMessage= new MimeMessage(mailSession);


    try {
        mailMessage.setFrom(new InternetAddress(mailFrom));
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
        mailMessage.setSubject(subject);
        mailMessage.setText(msg);

        Transport trans= mailSession.getTransport("smtp");
        trans.connect("smtp.gmail.com", username, password);
        Transport.send(mailMessage);

    } catch (Exception ex) {
        Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex);
    }

In addition to this I disabled my Avast Antivirus firewall as this was not allowing my program to connect with my mail id. Hope, this will work for other coders also with same problem.

Upvotes: 1

K.Nicholas
K.Nicholas

Reputation: 11551

Not JavaMail, for which I use to a SendGrid account, but some older apache http code that might help you in your debugging. I don't know if it still works, but should do. Note the port difference.

    SimpleSMTPHeader header;
    AuthenticatingSMTPClient client;
    server = "smtp.gmail.com";

    client = new AuthenticatingSMTPClient();
    client.addProtocolCommandListener(new PrintCommandListener(
                                          new PrintWriter(System.out), true));

    client.connect(server, 587);
    client.ehlo( client.getLocalAddress().getHostName() );
    client.execTLS();

    if (!SMTPReply.isPositiveCompletion(client.getReplyCode()))
    {
        client.disconnect();
        System.err.println("SMTP server refused connection.");
        System.exit(1);
    }

    client.auth(AuthenticatingSMTPClient.AUTH_METHOD.LOGIN,  "[email protected]", "password");


    sender = "[email protected]";
    subject = "A subject";

    BufferedReader reader = new BufferedReader( new FileReader( "html/DocumentReport.html") );
    client.mail("<[email protected]>");
    String receipient = "[email protected];

    client.addRecipient(receipient);

    writer = client.sendMessageData();

    header = new SimpleSMTPHeader(sender, receipient, subject);
    header.addHeaderField("Mime-Version", "1.0");
    header.addHeaderField("Content-Type", "text/html; charset=\"ISO-8859-1\"");
    header.addHeaderField("Content-Transfer-Encoding", "7bit");


    writer.write(header.toString());
    try {
        Util.copyReader(reader, writer);
    } finally {
        reader.close();             
    }

    writer.close();

    boolean completed = client.completePendingCommand();

    client.logout();

    client.disconnect();

Upvotes: 0

Related Questions