发送 Html 电子邮件

问题描述 投票:0回答:1

我希望发送一封包含图像的 HTML 电子邮件 我通过谷歌 smtp 服务器发送,

我用内联CSS生成一些html,它看起来像这样

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html charset=UTF-8" />
    </head>
    <body>
        <style>
            body{
                padding: 24px;

                display: flex;
                justify-content: center;
                font-family: Arial
            }

            .content{
                padding: 24px;
                background-color: #2b2b2b;
                color: #cfcfd0 ;
            }

            .row{
                display: flex;
                flex-direction: row;
            }

            .center{
                justify-content: center;
            }

            .code{
                padding: 8px;
                background-color: #1c1c1c;
                border: solid 1px #bababa;
                border-radius: 2px;
                font-size: 42px;
                color: #b81414;
                flex-grow: 1;
                text-align: center;
                font-weight: bold;
                font-family: helvetica
            }

            .link {
                color: #a10000;
                &:hover{
                    color: #ef0000;
                    text-decoration: none;
                }
            }
        </style>
        <div class="content">
            <p>Hello <b>example user</b
<p>your new account is almost ready<br>
to validate your email please use the code</p>
<div class="row center">
<span class="code">123123</span>
</div><p>Or simply click <a class="link" href="http://localhost:80/verify?id=45&code=123123"><b>Here</b></a></p>
<p>If this was not you, you can safely ignore this email</p>
        </div>
    </body>
</html>

在浏览器中,html 看起来像这样:

这是我发送电子邮件的代码

进口

import java.util.Properties
import javax.mail._
import javax.mail.internet._
import org.slf4j.{Logger, LoggerFactory}
import play.api.Configuration
import play.api.libs.json.{JsValue, Json}
import scalaj.http.{Http, HttpOptions, HttpResponse}

方法

  def sendMail(recipient: String, subject: String, content: String): Option[Int] = {
    refreshAccessToken()
    try {
      val message = new MimeMessage(session)
      message.setFrom(new InternetAddress(mimeSender))
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient))
      message.setSubject(subject)
      message.setHeader("Content-Type", "text/html")
      message.setContent(content, "text/html; charset=UTF-8")
      val transport = session.getTransport("smtp")
      transport.connect(hostName, senderEmail, accessToken)
      transport.sendMessage(message, message.getAllRecipients)
      logger.info("Email Sent!!")
      Some(recipient.length)
    }
    catch {
      case exception: Exception =>
        logger.error("Mail delivery failed. " + exception)
        None
    }
  }

电子邮件已发送并已收到,但看起来像这样(gmail)

如果我选择右侧的垂直省略号并选择查看原始内容,我会得到:

Return-Path: <**REDACTED**>
Received: from Magnus (**REDACTED**.dyn.plus.net. [**REDACTED**])
        by smtp.gmail.com with ESMTPSA id p9-20020adff209000000b00324853fc8adsm241084wro.104.2023.10.26.14.06.24
        for <**REDACTED**>
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Thu, 26 Oct 2023 14:06:25 -0700 (PDT)
Sender: **REDACTED**
Date: Thu, 26 Oct 2023 22:06:25 +0100 (BST)
From: **REDACTED**
To: **REDACTED**
Message-ID: <1348099677.0.1698354385683@Magnus>
Subject: Get started
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit





<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html charset=UTF-8" />
    </head>
    <body>
        <style>
            body{
                padding: 24px;

                display: flex;
                justify-content: center;
                font-family: Arial
            }

            .content{
                padding: 24px;
                background-color: #2b2b2b;
                color: #cfcfd0 ;
            }

            .row{
                display: flex;
                flex-direction: row;
            }

            .center{
                justify-content: center;
            }

            .code{
                padding: 8px;
                background-color: #1c1c1c;
                border: solid 1px #bababa;
                border-radius: 2px;
                font-size: 42px;
                color: #b81414;
                flex-grow: 1;
                text-align: center;
                font-weight: bold;
                font-family: helvetica
            }

            .link {
                color: #a10000;
                &:hover{
                    color: #ef0000;
                    text-decoration: none;
                }
            }




        </style>
        <div class="content">
            <p>Hello <b>453452345</b>
            

<p>your new account is almost ready<br>
to validate your email please use the code</p>

<div class="row center">
<span class="code">sIarYu44</span>
</div>

    <p>Or simply click <a class="link" href="http://localhost:80/verify?id=18&code=sIarYu44"><b>Here</b></a></p>
<p>If this was not you, you can safely ignore this email</p>

        </div>
    </body>
</html>

我可以看到内容类型设置为text/html,正文确实包含html,但它看起来不一样,请帮忙,如果我希望有一些图像,我还需要发送多部分电子邮件嵌入到电子邮件中

html scala email mime
1个回答
0
投票

如果将所有样式都内联,看起来会更好吗?

<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
  <div style="padding: 24px; display: flex; justify-content: center; font-family: Arial;">
    <div style="padding: 24px; background-color: #2b2b2b; color: #cfcfd0;">
      <p>Hello <b>example user</b></p>
      <p>Your new account is almost ready.</p>
      <p>To validate your email, please use the code:</p>
      <div style="display: flex; flex-direction: row; justify-content: center;">
        <span style="padding: 8px; background-color: #1c1c1c; border: solid 1px #bababa; border-radius: 2px; font-size: 42px; color: #b81414; flex-grow: 1; text-align: center; font-weight: bold; font-family: helvetica;">123123</span>
      </div>
      <p>Or simply click <a style="color: #a10000;" href="http://localhost:80/verify?id=45&code=123123"><b>Here</b></a></p>
      <p>If this was not you, you can safely ignore this email.</p>
    </div>
  </div>
</body>

</html>

是的,你需要多部分之类的东西

<img src="cid:image1" alt="Embedded Image 1">

每个附件都有一个唯一的 cid 标识符。

© www.soinside.com 2019 - 2024. All rights reserved.