当前位置: 首页 > 知识库问答 >
问题:

通过SMTP发送电子邮件,包括附件、纯/文本和文本/hml

厍胤运
2023-03-14

我的目标是:通过SMTP发送带有纯文本、文本/html和附件的事务性电子邮件。

我的代码:用JavaMail实现

我的问题是:它在hotmail或outlook上看起来不错。但在gmail上,如果是一封带有电子邮件地址的邮件,它不会正确显示邮件正文。txt附件(如果附件是图像,则可以正常工作)

任何帮助都将不胜感激。

以下是我的原始SMTP输出:

Subject: ALTERNATIVE | TXT | HTML |ATT.ATTACHMENT | Thu Jun 13 17:48:04 EDT
 2013
MIME-Version: 1.0
Content-Type: multipart/alternative; 
    boundary="----=_Part_0_21791733.1371160084561"

------=_Part_0_21791733.1371160084561
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Body message in text format!
------=_Part_0_21791733.1371160084561
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

Body message in <b>html</b> format! Sent on Thu Jun 13 17:48:04 EDT 2013<br> to: me@gmail.com<br> to: me@mijo.com<br> cc: me@hotmail.com<br> cc: rafael.santos.test@hotmail.com
------=_Part_0_21791733.1371160084561
Content-Type: text/plain; charset=us-ascii; name=email_attachment.txt
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=email_attachment.txt

This is a text attachment file!
------=_Part_0_21791733.1371160084561--
.
250 Delivery in progress
QUIT

一些截图

只送了一个。附件。消息正文不显示,附件重复。

==面向JAVA开发人员的解决方案====

总体思路如下所述:http://www.coderanch.com/t/503380/java/java/Java-Mail-text-html-attachment

现在我的代码看起来像:

// contentPart is the content to be sent. It is divided in bodyContent and attachmentContent
            MimeMultipart contentPart = new MimeMultipart("mixed");

            // Message body in txt and html format
            MimeMultipart bodyPart = new MimeMultipart("alternative");
            // Creates plain text message
            BodyPart bodyTxt = new MimeBodyPart();
            bodyTxt.setText(getMessageBodyText());
            // Creates html message
            BodyPart bodyHtml = new MimeBodyPart();
            bodyHtml.setContent(getMessageBodyHtml(), "text/html");
            bodyPart.addBodyPart(bodyTxt);
            bodyPart.addBodyPart(bodyHtml);

            // Wrapper for bodyTxt and bodyHtml
            MimeBodyPart bodyContent = new MimeBodyPart();
            bodyContent.setContent(bodyPart);

            // At this point, contentPart contains bodyTxt and bodyHtml wrapped in a multipart/alternative
            contentPart.addBodyPart(bodyContent);

            // Adds attachments to contentPart
            if (getAttachments() != null) {
                for(File f : getAttachments()) {
                    try {
                        MimeBodyPart attachmentPart = new MimeBodyPart();
                        attachmentPart.attachFile(f);
                        contentPart.addBodyPart(attachmentPart);
                    } catch (IOException e) {
                        logger.severe("Could not attach file to email!" +
                                " TO: "+ getTo().toString() +
                                "; CC: "+ getCc().toString() +
                                "; ExceptionMessage: " + e.getMessage());
                        throw new SmtpRequestException(e.getMessage());
                    }
                }
            }

共有1个答案

葛季萌
2023-03-14

你的信息结构是错误的。您需要嵌套的多部分来获得正确的结构,如下所示:

  multipart/mixed
    multipart/alternative (holding the two forms of the body part)
      text/plain
      text/html
    text/plain or image/gif (the attachment)
 类似资料:
  • 我已经编写了一个代码通过java mail API发送,所有的工作都很好,但是我发送的纯文本以可下载附件的形式被收件人接收,而不是纯文本消息。 我不知道为什么会这样。下面给出的是我运行时的代码及其输出。 我使用过Java Mail API、NetBeans IDE、Glassfish服务器、Enterprise Java Bean。 结果如下: 邮件收件人-看,简单的短信是附件的形式。

  • 我在网上看到了很多代码,但它们似乎都遇到了问题。 使用以下功能创建并保存文件: 然而,当运行下面的代码来发送文件时,我一直遇到问题,并建议使用此链接的所有答案https://stackoverflow.com/questions/48117511/exposed-beyond-app-through-clipdata-item-geturi,当打开Gmail时,它说无法附加文件 如果有任何方法可以

  • 我使用的PHPMailer表格在这里找到。 从网站下载的示例是“接触-3”,在引导主题中使用PHPMailer通过gmail发送SMTP电子邮件。当我使用“接触-1”时,它完全适用于我的托管域电子邮件地址,但SMTP版本适用于gmail地址,联系人表单不会提交。 在下面的代码中,我更改了以下行以添加我的gmail地址和gmail密码: 任何关于使用给定信息进行此工作的帮助都将不胜感激-提前感谢!

  • 我尝试从这个问题设置服务器PHP Ubuntu使用gmail表单发送电子邮件localhost。所以我设置了(帐户)从: /etc/MSMTPC: 现在,当我尝试发送测试电子邮件(以root用户身份执行)时: 给出: 所以日志上写着<代码>

  • 我在ASP. Net Core中工作,并尝试从gmail使用smtp客户端发送电子邮件。有以下代码,但不起作用也看过以下帖子,但不起作用http://dotnetthoughts.net/how-to-send-emails-from-aspnet-core/ 它会跟踪错误 系统NotSupportedException:SMTP服务器不支持身份验证

  • 我通过SMTP服务发送电子邮件时收到以下错误消息: 我的系统上有以下SMTP设置: 操作系统:Windows 7家庭高级版 IIS:IIS 7 如何解决这个问题? 谢谢。