0

This is my class that holds methods for sending email. I am using JavaMail API to send email. I checked multiple times if I pass right arguments from EmailAccount (<- it is a class that I created) and it seems correct. The problem is that I do not get any error or warning, all that is happening is that when program reaches Transport.send(msg); nothing is happening. It does not freeze or crash it is just running (it is like Transport.send(msg) is never completed). To close the program I have to break it manually from IDE. I will glad for any solutions to my problem.

public class EmailTools
    {
        public void sendEmail(EmailAccount emailAccount, String addressOfReceiver, String title, String text)
        {

            Properties props = new Properties();
            props.put("mail.smtp.host", emailAccount.getSmtpHost());
            props.put("mail.smtp.socketFactory.port", Integer.toString(emailAccount.getSslPort()));
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSlSocketFactory");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", Integer.toString(emailAccount.getSmtpPort()));
            props.put("mail.host", emailAccount.getSmtpHost());

            //Creating Authenticator
            javax.mail.Authenticator auth = new javax.mail.Authenticator()
            {
                @Override
                protected javax.mail.PasswordAuthentication getPasswordAuthentication()
                {
                    return new javax.mail.PasswordAuthentication(emailAccount.getEmailAddress(), emailAccount.getPassword());
                }
            };
            //Starting session
            Session session = Session.getDefaultInstance(props, auth);
            createAndSendEmail(session, emailAccount, addressOfReceiver, title, text);
        }

        public void createAndSendEmail(Session session, EmailAccount emailAccount, String receiver, String title, String message)
        {
            try
            {
                MimeMessage msg = new MimeMessage(session);
                //Header stuff
                msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
                msg.addHeader("format", "flowed");
                msg.addHeader("Content-Transfer-Encoding", "8bit");

                msg.setFrom(new InternetAddress(emailAccount.getEmailAddress(), "NoReply-JD"));
                msg.setSubject(title, "UTF-8");
                msg.setText(message, "UTF-8");
                msg.setSentDate(new Date());
                msg.setReplyTo(InternetAddress.parse(emailAccount.getEmailAddress(), false));
                msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiver, false));

                Transport transport = session.getTransport("smtps");
  //EDIT1      transport.connect(emailAccount.getSmtpHost(),emailAccount.getSmtpPort(),emailAccount.getEmailAddress(),emailAccount.getPassword());
        transport.sendMessage(msg,msg.getAllRecipients());
        transport.close();
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }

        public static void main(String args[])
        {
            EmailAccount emailAccount = new EmailAccount("example@gmail.com","smtp.gmail.com","password","",465,465);
            EmailTools emailTools = new EmailTools();
            emailTools.sendEmail(emailAccount,"example@gmail.com","Title","Text");
        }

}

EDIT1: I managed to solve the problem and got connected. But now i got the exception

javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbuuA
534-5.7.14 2SN9pvdVb-g3yxJj_u5P7eeAJOoLRRJMVevSThdvunt1c2qCcjkt9FRerrmg9YkB3UDbkc
534-5.7.14 wQDcDG8k4c8GcLrteODlY_danNGhcrg_bxE2_SgYioZK4nH0SzNW1K6-ZRCSlm-mTb6Auj
534-5.7.14 UOr5UqyodVDPHCi8fAmsRF-s30sF29nAdzNMjchSccoo3gwHYixRKSTb69XYWpat1SgRHK
534-5.7.14 FIJ5GjQ> Please log in via your web browser and then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 ll20sm5684460wic.14 - gsmtp

EDIT2: Problem solved. It helped me a lot with gmail problem: Unable to send email via google smtp on centos VPS

Community
  • 1
  • 1
Marcin Majewski
  • 1,007
  • 1
  • 15
  • 30
  • 1
    The Transport class has a property debug. Can You set it to true and look what`s happend? – Jens Oct 04 '14 at 17:27
  • @Jens I got this error: "DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false". I assume there is a problem with SSL, how to fix this? – Marcin Majewski Oct 04 '14 at 18:13

0 Answers0