JavaMail How to change the email sender address?

0

I'm using a very basic code for sending e-mail, and I'm noticing that the sender's address differs from the sender's address, and instead the e-mail account address.

I'm making use of the staff as below but it does not work

InternetAddress from = new InternetAddress("[email protected]", "Robson Costa");

Below is the class for sending email

public class SendMailApp {
  public static void main(String[] args) {
        String port = "465";
        String SSL = "2";
        // Usuario e senha para autenticar
        final String user = "prorisc*****@gmail.com";
        final String password = "******";
        // Remetente e destinatario
        String sFrom = "Robson Costa <odinrl****@yahoo.com.br>";
        String sTo   = "robsonlira***@hotmail.com";                   

        Properties props = new Properties();
        /** Parâmetros de conexão com servidor Gmail */
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", port);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", port);

        Session session = Session.getDefaultInstance(props,
                    new javax.mail.Authenticator() {
                         protected PasswordAuthentication getPasswordAuthentication() 
                         {
                               return new PasswordAuthentication(user, password);
                         }
                    });
        /** Ativa Debug para sessão */
        session.setDebug(true);
        try {

              Message message = new MimeMessage(session);

              java.util.regex.Pattern pattern = java.util
                            .regex.Pattern.compile("(.+)?<(.+?)>");
              java.util.regex.Matcher matcher = pattern.matcher(sFrom.trim());
              // Remetente
              InternetAddress from = null;

              if (matcher.find()) {
                 from = new InternetAddress(matcher.group(2) );
                 from.setPersonal(matcher.group(1));
              } else {
                 from = new InternetAddress(sFrom);
              }

              message.setFrom(from); //Remetente
              //Altera a data de envio da mensagem
              message.setSentDate(new Date());
              Address[] toUser = InternetAddress //Destinatário(s)
                         .parse(sTo);  
              message.setRecipients(Message.RecipientType.TO, toUser);
              message.setSubject("Enviando email com JavaMail");//Assunto
              message.setText("Enviei este email utilizando JavaMail com minha conta GMail!");
              /**Método para enviar a mensagem criada*/
              Transport.send(message);
              System.out.println("Feito!!!");
         } catch (MessagingException e) {
              throw new RuntimeException(e);
        }
  }

}

    
asked by anonymous 15.08.2017 / 02:06

0 answers