【发布时间】:2016-05-11 13:03:10
【问题描述】:
我在这里附上了我的示例源代码。我在系统中有一个 html 模板和路径。我的目标是需要在邮件中发送带有消息的 HTML 模板。但我必须使用 html 路径附加 HTML 消息。如何做到这一点。
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="vikki@gmail.com";
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 ("vikki@gmail.com","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);
}
}
}}
【问题讨论】:
-
您是否尝试读取工作目录中的 HTML 文件,并将 HTML 中的内容附加到电子邮件中?
-
您希望 HTML 成为附件还是文本的一部分?
-
是的暴徒。我需要使用文件路径读取 html,然后需要附加到消息部分。发送邮件后,它应该在电子邮件中显示为 html
-
我已经更新了我的更改。您可以看到我的邮件消息部分应该是怎样的图像。像这张图片一样,我有一个 html 模板。我需要使用文件路径显示它
-
@MuthuVikki ...HTML 文件是本地文件还是网络文件?
标签: java html email message filepath