• <tfoot id='N78ue'></tfoot>
      <legend id='N78ue'><style id='N78ue'><dir id='N78ue'><q id='N78ue'></q></dir></style></legend>

        <i id='N78ue'><tr id='N78ue'><dt id='N78ue'><q id='N78ue'><span id='N78ue'><b id='N78ue'><form id='N78ue'><ins id='N78ue'></ins><ul id='N78ue'></ul><sub id='N78ue'></sub></form><legend id='N78ue'></legend><bdo id='N78ue'><pre id='N78ue'><center id='N78ue'></center></pre></bdo></b><th id='N78ue'></th></span></q></dt></tr></i><div id='N78ue'><tfoot id='N78ue'></tfoot><dl id='N78ue'><fieldset id='N78ue'></fieldset></dl></div>

        <small id='N78ue'></small><noframes id='N78ue'>

        • <bdo id='N78ue'></bdo><ul id='N78ue'></ul>
      1. 最佳实践 - 发送 javamail mime 多部分电子邮件 - 和 gmail

        Best Practices - Sending javamail mime multipart emails - and gmail(最佳实践 - 发送 javamail mime 多部分电子邮件 - 和 gmail)
        • <bdo id='gsfuO'></bdo><ul id='gsfuO'></ul>
              <tbody id='gsfuO'></tbody>

            1. <i id='gsfuO'><tr id='gsfuO'><dt id='gsfuO'><q id='gsfuO'><span id='gsfuO'><b id='gsfuO'><form id='gsfuO'><ins id='gsfuO'></ins><ul id='gsfuO'></ul><sub id='gsfuO'></sub></form><legend id='gsfuO'></legend><bdo id='gsfuO'><pre id='gsfuO'><center id='gsfuO'></center></pre></bdo></b><th id='gsfuO'></th></span></q></dt></tr></i><div id='gsfuO'><tfoot id='gsfuO'></tfoot><dl id='gsfuO'><fieldset id='gsfuO'></fieldset></dl></div>

              <small id='gsfuO'></small><noframes id='gsfuO'>

            2. <tfoot id='gsfuO'></tfoot>
              <legend id='gsfuO'><style id='gsfuO'><dir id='gsfuO'><q id='gsfuO'></q></dir></style></legend>

                • 本文介绍了最佳实践 - 发送 javamail mime 多部分电子邮件 - 和 gmail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个需要发送确认电子邮件等的 Tomcat 应用程序.我已经使用 Javamail (mail.jar) 对电子邮件程序进行了编码,以发送多部分文本/html 电子邮件.我的代码基于 Java EE 示例.我在本地服务器上使用 SMTP MTA.

                  I have a Tomcat application that needs to send confirmation emails etc. I have coded the emailer with Javamail (mail.jar) to send multipart text/html emails. I based the code on the Java EE examples. I'm using the SMTP MTA on the local server.

                  效果很好.在 Outlook 中,我看到了 HTML 版本.如果我将它拖到 Outlook 垃圾邮件文件夹中,我会看到文本版本.所以我将其解释为有效.

                  It works great. In Outlook, I see the HTML version. If I drag it into the Outlook spam folder, I see the text version. So I interpret that as saying it works.

                  但是,如果我在 Gmail 中查看电子邮件,我只能看到文本版本.我知道 HTML 就在那里(Outlook 就是从那里得到它的).但是 Gmail 没有显示它...我有很多来自其他系统的电子邮件在 Gmail 中显示为 HTML.

                  However, if I view the emails in Gmail, I see only the text version. I know the HTML is there (that's where Outlook got it from). But Gmail is not showing it... I have lots of emails from other systems that show as HTML in Gmail.

                  谁能指出我所缺少的规范?我需要创建特殊的标题吗?

                  Can anyone point me to the spec that shows what I am missing? Are there special headers I need to create?

                  谢谢

                  代码如下:

                  Message message = new MimeMessage(session);
                  Multipart multiPart = new MimeMultipart("alternative");
                  
                  try {
                  
                      MimeBodyPart textPart = new MimeBodyPart();
                      textPart.setText(text, "utf-8");
                  
                      MimeBodyPart htmlPart = new MimeBodyPart();
                      htmlPart.setContent(html, "text/html; charset=utf-8");
                  
                      multiPart.addBodyPart(htmlPart);
                      multiPart.addBodyPart(textPart);
                      message.setContent(multiPart);
                  
                      if(from != null){
                          message.setFrom(new InternetAddress(from));
                      }else
                          message.setFrom();
                  
                      if(replyto != null)
                          message.setReplyTo(new InternetAddress[]{new InternetAddress(replyto)});
                      else
                          message.setReplyTo(new InternetAddress[]{new InternetAddress(from)});
                  
                      InternetAddress[] toAddresses = { new InternetAddress(to) };
                      message.setRecipients(Message.RecipientType.TO, toAddresses);
                      message.setSubject(subject);
                      message.setSentDate(new Date());
                  
                      Transport.send(message);
                  
                  } catch (AddressException e) {
                      e.printStackTrace();
                      System.out.println("Error: "+e.getMessage());
                  
                  } catch (MessagingException e) {
                      e.printStackTrace();
                      System.out.println("Error: "+e.getMessage());
                  
                  } finally {     
                      System.out.println("Email sent!");
                  }
                  

                  推荐答案

                  解决了!似乎根据多部分 MIME 规范,部分的顺序很重要.应按从低保真到高保真的顺序添加它们.因此,GMail 似乎遵循规范并使用最后一部分.就我而言,我有它们 HTML,文本.我只是将订单换成文本、HTML 和 Gmail 正确呈现它...

                  Solved! It seems according to the multipart MIME spec, the order of the parts are important. They should be added in order from low fidelity to high fidelity. So it seems GMail follows the spec and uses the last part. In my case I had them HTML, Text. I just swapped the order to Text, HTML and Gmail renders it correctly...

                  MimeBodyPart textPart = new MimeBodyPart();
                  textPart.setText(text, "utf-8");
                  
                  MimeBodyPart htmlPart = new MimeBodyPart();
                  htmlPart.setContent(html, "text/html; charset=utf-8");
                  
                  multiPart.addBodyPart(textPart); // <-- first
                  multiPart.addBodyPart(htmlPart); // <-- second
                  message.setContent(multiPart);
                  

                  感谢您的帮助!

                  这篇关于最佳实践 - 发送 javamail mime 多部分电子邮件 - 和 gmail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Compiling C++ for the JVM(为 JVM 编译 C++)
                  Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                  How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                  Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                  How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                  Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)

                    <small id='jTEvz'></small><noframes id='jTEvz'>

                      <bdo id='jTEvz'></bdo><ul id='jTEvz'></ul>

                    • <tfoot id='jTEvz'></tfoot>

                    • <legend id='jTEvz'><style id='jTEvz'><dir id='jTEvz'><q id='jTEvz'></q></dir></style></legend>
                    • <i id='jTEvz'><tr id='jTEvz'><dt id='jTEvz'><q id='jTEvz'><span id='jTEvz'><b id='jTEvz'><form id='jTEvz'><ins id='jTEvz'></ins><ul id='jTEvz'></ul><sub id='jTEvz'></sub></form><legend id='jTEvz'></legend><bdo id='jTEvz'><pre id='jTEvz'><center id='jTEvz'></center></pre></bdo></b><th id='jTEvz'></th></span></q></dt></tr></i><div id='jTEvz'><tfoot id='jTEvz'></tfoot><dl id='jTEvz'><fieldset id='jTEvz'></fieldset></dl></div>

                          <tbody id='jTEvz'></tbody>