Send login and password in Javamail email

Asked

Viewed 442 times

0

I am developing a web application using jsp and Servlet and I would like that when sending the email an image is passed and the login and password but the email only send the image and does not send the login and password.

Image with the error

inserir a descrição da imagem aqui

Servlet

     protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //processRequest(request, response);


    String login = request.getParameter("login");
    String senha = request.getParameter("senha");

    String email = request.getParameter("email");

        String assunto = "Cadastro de dados do EccomeceJSP2";
        EnviarEmailGmail gm = new EnviarEmailGmail();
        gm.enviarGmail(email, assunto, login, senha);

Class responsible for sending

public class EnviarEmailGmail {

    static Session session;

public void enviarGmail(String email, String assunto, String login, String senha){
try {

  final String username = "[email protected]";
    final String password = "minhasenha";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    //props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");
            props.put("mail.smtp.socketFactory.port", "465");  
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");  
props.put("mail.smtp.socketFactory.fallback", "false");  

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

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse(email));
        message.setSubject(assunto);
        //message.setText("Seu cadastro foi realizado com sucesso e seu login e senha será <br> " + "Login:" + login + "Senha:" + senha);


   MimeMultipart multipart2 = new MimeMultipart("related");

    // first part  (the html)
    BodyPart messageBodyPart = new MimeBodyPart();
    String htmlText = "<H3>Seu cadastro foi realizado com sucesso e seu login e senha será:</H3><br><b>Login:+login+</b><br><b>Senha:+senha+</b><br><br><img src=\"cid:image\">";
    messageBodyPart.setContent(htmlText, "text/html");

    // add it
    multipart2.addBodyPart(messageBodyPart);

    // second part (the image)
    messageBodyPart = new MimeBodyPart();
    DataSource fds = new FileDataSource
      ("C:\\imagens\\eccomerce.JPG");
    messageBodyPart.setDataHandler(new DataHandler(fds));
    messageBodyPart.setHeader("Content-ID","<image>");

    // add it
    multipart2.addBodyPart(messageBodyPart);

    // put everything together
    message.setContent(multipart2);


        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
       }
     }

    }
  • Have you not already asked this question and deleted it? If you are recreating the same question, I advise you to stop because it may generate site suspension.

  • It is not the same question the problem I had posed in the other question I managed to solve, this is another question with different problem

  • So the title is similar. As a hint, try to write a title that is more consistent with the problem and not one that is generative and repetitive, this even helps your question to be located more easily, increasing your chances of getting answers ;)

1 answer

1


the problem is on the line:

String htmlText = "<H3>Seu cadastro foi realizado com sucesso e seu login e senha será:</H3><br><b>Login:+login+</b><br><b>Senha:+senha+</b><br><br><img src=\"cid:image\">";

swap for

String htmlText = "<H3>Seu cadastro foi realizado com sucesso e seu login e senha será:</H3><br><b>Login: " + login + "</b><br><b>Senha: " + senha + "</b><br><br><img src=\"cid:image\">";

Browser other questions tagged

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