Like this i need to attach html in message partI have attached my example source here. I have a html template and path in system. My objective is need to send the HTML template with message in mail. But i have to append the HTML message using html path. How to do this.
public class SendMail {
public void sendingEmailToClient(String sEmail,String sUserName,String htmlPath) throws Exception{
String fromMerchant=null;
String merchantPassword=null;
String toClientMail=null;
String userName=null;
String smtp=null;
String smtpServer=null;
String smtpSocketFactory=null;
String smtpPort=null;
String socketFactoryClass=null;
String sslSocketFactory=null;
String smtpAuthentication=null;
String smtpAuthenticateState=null;
String mailSmtpPort=null;
if(sEmail!=null && !sEmail.isEmpty() && sUserName !=null && !sUserName.isEmpty()){
toClientMail=sEmail;
userName=sUserName;
System.out.println("The user name is ==>"+userName);
fromMerchant="[email protected]";
merchantPassword="passw0rd";
Properties properties=new Properties();
smtp="mail.smtp.host";
smtpServer="smtp.gmail.com";
smtpSocketFactory="mail.smtp.socketFactory.port";
smtpPort="465";
socketFactoryClass="mail.smtp.socketFactory.class";
sslSocketFactory="javax.net.ssl.SSLSocketFactory";
smtpAuthentication="mail.smtp.auth";
smtpAuthenticateState="true";
mailSmtpPort="mail.smtp.port";
properties.put(smtp,smtpServer);
properties.put(smtpSocketFactory, smtpPort);
properties.put(socketFactoryClass, sslSocketFactory);
properties.put(smtpAuthentication, smtpAuthenticateState);
properties.put(mailSmtpPort,smtpPort);
Session session=Session.getDefaultInstance(properties,new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication ("[email protected]","passw0rd");
}
});
try{
MimeMessage message=new MimeMessage(session);
message.setFrom(new InternetAddress("LoyaltyCart"));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(toClientMail));
message.setText(" Hi "+userName+"\n You have successfully registered on YourShopping !!.. \n We heartly welcomes you and save your money, buy new products...");
**//Need to append the HTML template using file path with message like message.setHTMLTemplate**
Transport.send(message);
System.out.println("message sent successfully");
message.setText("You have successfully registered on YourShopping !!..");
message.setText("We heartly welcomes you and save your money, buy new products...");
}catch(Exception e){
throw new RuntimeException(e);
}
}
}}
-
Are you trying to read a HTML file in the working directory, and append the content in the HTML to the e-mail?user5287318– user52873182016年05月11日 13:10:47 +00:00Commented May 11, 2016 at 13:10
-
Do you want the HTML to be an attachment, or part of the text?VGR– VGR2016年05月11日 13:16:24 +00:00Commented May 11, 2016 at 13:16
-
Yes mob. I need to read html using file path then need to append in the message part. After sending mail it should showing with html in emailuser5122076– user51220762016年05月11日 13:16:52 +00:00Commented May 11, 2016 at 13:16
-
I have update my changes. You can see the image how my mail message part should be. Like this image i have a html template. I need to show it using file pathuser5122076– user51220762016年05月11日 13:21:22 +00:00Commented May 11, 2016 at 13:21
-
@MuthuVikki ...the HTML file is local or web?user5287318– user52873182016年05月11日 13:24:55 +00:00Commented May 11, 2016 at 13:24
1 Answer 1
If you want to use an HTML file as the content of your message, you can ues setDataHandler:
String htmlFileName = "/home/vikki/Documents/MailTemplate.html";
message.setDataHandler(
new DataHandler(new FileDataSource(htmlFileName)));
If you want to make the HTML file an inline attachment:
String htmlFileName = "/home/vikki/Documents/MailTemplate.html";
MimeBodyPart part = new MimeBodyPart();
part.attachFile(htmlFileName);
part.setDisposition(Part.INLINE);
message.setContent(new MimeMultipart(part));
Both approaches merely provide flat HTML content. If the HTML includes images, it will need to link to them using external URLs (probably Internet URLs). Most e-mail clients block links to external images by default. You can avoid this by attaching each image, and modifying the HTML to point to each image's Content-Id as explained in RFC 2392, though it will make the message itself larger.