我希望发送一个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看起来像这样:
x1c 0d1x的数据
这里是我的代码发送电子邮件
进口
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>
型
我可以看到内容类型设置为文本/html和身体确实包含html,但它看起来不一样,请帮助,也将我需要发送一个多部分的电子邮件,如果我希望有一些图像嵌入到电子邮件
2条答案
按热度按时间chhqkbe11#
如果你把所有的样式都内联,会不会看起来更好?
字符串
是的,你需要多部分和类似的东西,
型
每个附件具有唯一的CID标识符。
pexxcrt22#
虽然没有人能回答作者的问题,但电子邮件中的HTML通常限于Microsoft Outlook 2007或更早版本接受的HTML的历史子集,详见https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2007/aa338201(v=office.12)?redirectedfrom=MSDN。该子集派生自https://www.w3.org/TR/1999/REC-html401-19991224/中描述的HTML 4,并在https://www.w3.org/TR/1999/REC-html401-19991224/sgml/dtd.html中使用正式的SGML DTD语法。
值得注意的是,
style
元素应该是head
的子元素,而不是允许出现在body
下面的任何地方。WHATWG HTML 5在某个时候考虑过在body
下面引入<style scoped>
,但在HTML 5之前(WHATWG HTML的W3C版本最初发布为)已经放弃了,如https://sgmljs.net/docs/w3c-html5-dtd.html。此外,HTML中的CSS通常仅限于CSS 2.1中可用的一个子集,例如
float
,而flex
不会被大多数专用的邮件用户代理呈现。