Javamail How to change the sender’s email address?

Asked

Viewed 288 times

0

I am making use of a very basic code for sending e-mail and I am noticing that the sender’s address differs from the one that was passed, and in the place this the email account address.

I am making use of personal as below no longer works

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);
        }
  }

}

  • You need to post the rest of the code that makes the email sending.

  • Here is an example of sending an email: http://karanalpe.com.br/tecnologia/back-end/enviando-email-com-o-java/

  • Hello Renato good morning, man I did not post the rest of the code because it is basic for a test sending email, and also because the message is being sent or there are "problems in the code" more if you think it necessary I will update the question, thank you.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.