【问题标题】:Sending html with message in email using file path [duplicate]使用文件路径在电子邮件中发送带有消息的 html [重复]
【发布时间】: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


【解决方案1】:

如果您想使用 HTML 文件作为邮件内容,可以使用setDataHandler

String htmlFileName = "/home/vikki/Documents/MailTemplate.html";
message.setDataHandler(
    new DataHandler(new FileDataSource(htmlFileName)));

如果您想让 HTML 文件成为内联附件:

String htmlFileName = "/home/vikki/Documents/MailTemplate.html";

MimeBodyPart part = new MimeBodyPart();
part.attachFile(htmlFileName);
part.setDisposition(Part.INLINE);

message.setContent(new MimeMultipart(part));

这两种方法都只提供平面 HTML 内容。如果 HTML 包含图像,则需要使用外部 URL(可能是 Internet URL)链接到它们。默认情况下,大多数电子邮件客户端会阻止指向外部图像的链接。您可以通过附加每张图片并修改 HTML 以指向每张图片的Content-Id 来避免这种情况,如RFC 2392 中所述,尽管它会使消息本身变大。

【讨论】:

    猜你喜欢
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    相关资源
    最近更新 更多