0
votes

I'm trying to test two methods to send messages by SMTP:

1st method:

public void sendEmail(String emailRecip, String subject, String texte, List<String> listAttachedFile){


        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            String address = emailRecip;
            InternetAddress[] iAdressArray = InternetAddress.parse(address);
            message.setRecipients(Message.RecipientType.TO, iAdressArray);
            message.setSubject(subject);
            MimeBodyPart mbp1 = new MimeBodyPart();
            mbp1.setText(texte);
            Multipart mp = new MimeMultipart();
            mp.addBodyPart(mbp1);
            for (String attachedFile : listAttachedFile) {
                if (attachedFile != null) {
                    addAttachment(mp, attachedFile);
                }
            }
            message.setContent(mp);
            Transport.send(message);

        } catch (MessagingException e) {
            isMsgSent = "0";
        } catch (IOException ex) {
            Logger.getLogger(SMTPRepositoryImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

}

2nd method: repeatedly sending message:

public void sendMailrepeatdly(String emailRecip, String subject, String texte, List<String> listAttachedFile){
Session session = Session.getInstance(props,null);

        /*Session session = Session.getInstance(props,
         new javax.mail.Authenticator() {
         @Override
         protected PasswordAuthentication getPasswordAuthentication() {
         return new PasswordAuthentication(username, password);
         }
         });*/

        try {
            MimeMessage message = new MimeMessage(session);
            String address = emailRecip;
            InternetAddress[] iAdressArray = InternetAddress.parse(address);

            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO, iAdressArray);
            message.setSubject(subject);
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText(texte);
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            messageBodyPart = new MimeBodyPart();

            for (String attachedFile : listAttachedFile) {
                if (attachedFile != null) {
                    addAttachment(multipart, attachedFile);
                }
            }

            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);
            message.saveChanges();
            try {
                long startTime = System.currentTimeMillis();
                Transport tr = session.getTransport("smtps");
                tr.connect(host, username, password);
               // tr.sendMessage(message,message.getAllRecipients() );
                for (Address recipient : message.getAllRecipients()) {
                    tr.sendMessage(message, new Address[]{recipient});
                }
                tr.close();

            } catch (SendFailedException sfe) {
                System.out.println(sfe);
            } catch (IOException ex) {
                Logger.getLogger(SMTPRepositoryImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (MessagingException e) {

        }

}

The 1st method works very well, but with the second method i always got the error:

com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required.

when debuging, the exception SendFailedException is thrown when invoking tr.sendMessage(message, new Address[]{recipient});

am i missing something in my code in the second method, any suggestions?

the properties of the SMPT server are:

mail.smtp.auth = true

mail.smtp.starttls.enable = true

mail.smtp.host = smtp.gmail.com

mail.smtp.port = 587

1
Why all the comments are deleted!!!!! , any way, the solution is to declare Transport tr = session.getTransport("smtp"); and not smtps and also change the construction of the message like in the first method.Selma

1 Answers

0
votes

You're using the "smtps" protocol, but you set properties for the "smtp" protocol. Change your code to use the "smtp" protocol.