Send login and password in JavaMail email
I am developing a web application using JSP and servlet and would like that when sending the email passes an image and the login and password but the email only sends the image and does not send the login and password.
Image with error
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);
}
}
}
0
1 answers
The problem is in 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\">";
Change to
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\">";
1
Author: Orlando Correa, 2018-01-11 02:46:05