I'm using JavaMail for Java to send out emails to multiple people and so I need to use variables to make each email personal. Now my question is how do I use normal java variables like strings and ints inside the HTML code of an email.
Here is my code
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
MimeMultipart multipart = new MimeMultipart("related");
BodyPart messageBodyPart = new MimeBodyPart();
message.setSubject(title + " has reached your desired price!");
String htmlText = "<H1>Hello</H1> <H2> hi </H2> <img src=\"cid:image\">" + price_desired;
messageBodyPart.setContent(htmlText, "text/html");
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource(folderPath + image_title);
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<image>");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
As you can see I can add variables at the end of the email however how do I do it inside the HTML and how do I make the font size etc same for the variables as the normal code. I'm sorry if this was asked before but I was unable to find anything and I searched here on Stack Overflow and Google.
EDIT - For anyone wondering the answer is
String htmlText = "<H1>Hello. The price desired is " + price_desired + "</H1>
when I initially tried something similar it didn't work but when I copied this code I could then easily change it around.
Thanks everyone :)