【问题标题】:how to format tables in Mails / outlook如何在邮件/outlook 中格式化表格
【发布时间】:2020-04-08 18:23:21
【问题描述】:

我正在使用 Spring MVC 发送电子邮件,但在浏览器和 Outlook 电子邮件中显示不同

    @Service("emailService")
    public class EmailService {  
    @Autowired
     private JavaMailSender mailSender;
     StringBuilder ResponseTable=new StringBuilder();
       public void sendMail(String emailid,String subject,int 
         tracking_id,String classification_type){


      MimeMessage message=mailSender.createMimeMessage();
      MimeMessageHelper helper=new MimeMessageHelper(message,true,"UTF- 8");    

     helper.setTo(emailid);
     helper.setText(ResponseTable.toString(),true);

     helper.setSubject(subject);

     mailSender.send(message);


   }
  }

【问题讨论】:

  • 请重新标记您的问题。这个问题几乎与这些标签中的任何一个都没有关系。基本上和“如何在Mails/outlook中格式化表格”有关。
  • 只有两张截图,这个问题是无法回答的。添加准备和发送电子邮件的代码、您的配置等。请阅读stackoverflow.com/help/how-to-ask 了解如何提出一个好问题。

标签: spring hibernate jsp model-view-controller


【解决方案1】:

您可以使用速度引擎模板来创建自定义模板:

  1. 创建您的模板 mailTemplate.vm

  2. 创建一个可以在模型中插入数据的方法

  3. 创建一个方法来将此模型插入到您的模板中

    1.mailTemplate.vm

    <html> 
    <head></head>
    <body>
        <p>hello,</p>
        <p>${variable}</p>
    </body>
    </html>
    
    1. 创建模型以发送通知邮件

       public Boolean NotificationMail(String sender, String receiver, String url,
                String subject,String footer) {
      
              LOG.info(" ... Sending new Subscription Notification Mail ");
              try {
                JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
                mailSender.setHost(mailHost);
                mailSender.setPort(port);
                mailSender.setJavaMailProperties(setMailProperties());
      
                Map<String, Object> model = new HashMap<String, Object>();
                model.put("variable", "hello this is the text will be display in the mail");
      
      
                String body = getVelocityTemplate(model);
                LOG.info("Mail host {} , Port {} , url {} , sender {}  , receiver {} , subject {} , body {} ",
                    mailHost, port, url, sender, receiver, subject, body);
                Boolean sent = mailService.sendMailWithImg(sender, receiver, subject, body);
                return true;
              } catch (IllegalArgumentException e) {
                LOG.error("IllegalArgumentException , Missing configuration", e);
              } catch (Exception e) {
                LOG.error("General Exception :{}", e);
              }
              return false;
            }
      
    2. 在模板中插入模型

       protected String getVelocityTemplateContent(Map<String, Object> model) {
      StringWriter stringWriter = new StringWriter();
      StringBuilder builder = new StringBuilder();
      builder.append("/template/mailTemplate.vm");
      try {
        VelocityEngineUtils.mergeTemplate(velocityEngine, builder.toString(), "UTF8", model,
            stringWriter);
        return stringWriter.toString();
      } catch (Exception e) {
        LOG.error("Error",e);
      }
      return null;
          }
          }
      

【讨论】:

    猜你喜欢
    • 2021-07-16
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    相关资源
    最近更新 更多