[ACCEPTED]-TLS issue when sending to gmail through JavaMail-jakarta-mail

Accepted answer
Score: 15

You need to enable STARTTLS. Add one more property 1 to your configuration:

props.put("mail.smtp.starttls.enable", "true");
Score: 5
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class CopyOfSendMail {

private static String SMPT_HOSTNAME = "my smtp port no";
private static String USERNAME = "root";
private static String PASSWORD = "root";

public static void main(String[] args) {
    Properties props = new Properties();
    props.put("mail.smtp.host", SMPT_HOSTNAME);
    props.put("mail.from","aaa@gmail.com");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");

    Session session = Session.getInstance(props, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(USERNAME, PASSWORD);
        }
    });
    try {
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom();
        msg.setRecipients(Message.RecipientType.TO,
                          "bbb@gmail.com");
        msg.setSubject("JavaMail hello world example");
        msg.setSentDate(new Date());
        msg.setText("Hello, world!\n");
        Transport.send(msg);
     } catch (MessagingException mex) {
        System.out.println("send failed, exception: " + mex);
     }
    }
}

0

Score: 1

Here Mail Sender which is using MSN-SMTP 10 service

My host is smtp.live.com and port is 587.

As given 9 in official doc of Java Mail, here you can get more info about best 8 Java Mail mechanism to send and receive 7 mails.

Properties of Mail Client are:

  Properties mailProps = new Properties();
  mailProps.put("mail.smtp.user",mailID);
  mailProps.put("mail.smtp.host",host);
  mailProps.put("mail.smtp.auth", "true");
  mailProps.put("mail.smtp.port",port);
  mailProps.put("mail.smtp.starttls.enable", "true");
  Session mailSession = Session.getInstance(mailProps,null);

Sending 6 mechanism is :

 SMTPTransport t=(SMTPTransport)mailSession.getTransport("smtp");
 System.out.println(" Taking protocol! ");
 t.connect(host, mailID, password);
 System.out.println(" Connection Successfull! ");
 t.sendMessage(mimMessage,mimMessage.getAllRecipients());

Note:
Code is running well 5 on local Indian Server. But this is not responding at 4 Azur Congo: Both are Linux server.

Error is:

 com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first

Even 3 if system property set manually:

 java -Dmail.smtp.starttls.enable=true SendAMai

also @Rob 2 Harrop and @Brian 's points are ensured 1

if you're on Linux ensure that you have libnss3 and openssl installed
Score: 0
package enviando_email.enviando_email;
import static org.junit.Assert.assertTrue;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.junit.Test;

/**
 * Unit test for simple App.
 */

public class AppTest 
{

    @org.junit.Test
    public void testEmail() {
        
          final String username = "yourusername";
          final String password = "yourpass";
        
        try {
            
            Properties properties = new Properties(); 
            properties.put("mail.smtp.auth", "true"); /*autorizacao*/
            properties.put("mail.smtp.starttls.enable", "true"); /* autenticacação*/
            properties.put("mail.smtp.host", "smtp-mail.outlook.com"); /* servidor do gmail */
            properties.put("mail.smtp.port", "587"); /* porta de saida do servidor */
            properties.put("mail.smtp.socketFactory.port", "587"); /* especifica a porta a ser conectada pelo socket */
            properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); /* classe do socket de conexão SMTP */
            
            //cria sessão - recebe objeto autorizado para se manter na sessão 
            Session session = Session.getInstance(properties, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });
            
            Address[] toUser = InternetAddress.parse("recipient1@hotmail.com,recipient2@gmail.com");
            
            Message message = new MimeMessage(session); //pega sessão válida 
            message.setFrom(new InternetAddress(username)); //pega o usuario que iniciou a sessão e envia mensagem por ele
            message.setRecipients(Message.RecipientType.TO, toUser); //para quem enviar email 
            message.setSubject("Chegou assunto do e-mail"); //Assunto do e-mail
            message.setText("Olá esse e-mail está sendo enviado pelo java mail");
            
            Transport.send(message);
            
        }catch (Exception e) {
            e.printStackTrace();
        }
        
    }
}

0

More Related questions