我正在开发一个将邮件发送到列表等的应用程序。它是使用 javax.mail api 用 Java 编写的。 这里的问题是,我无法使用 CID 嵌入图像。
这是发送邮件的来源:
Delivered-To: -@gmail.com
Received: by 10.43.50.4 with SMTP id vc4csp85536icb;
Tue, 17 Apr 2012 05:08:00 -0700 (PDT)
Received: by 10.204.141.25 with SMTP id k25mr4406030bku.72.1334664479298;
Tue, 17 Apr 2012 05:07:59 -0700 (PDT)
Return-Path: <-@gmail.com>
Received: from mail-bk0-f43.google.com (mail-bk0-f43.google.com [209.85.214.43])
by mx.google.com with ESMTPS id ad16si7984294bkc.150.2012.04.17.05.07.58
(version=TLSv1/SSLv3 cipher=OTHER);
Tue, 17 Apr 2012 05:07:59 -0700 (PDT)
Received-SPF: pass (google.com: domain of -@gmail.com designates 209.85.214.43 as permitted sender) client-ip=209.85.214.43;
Authentication-Results: mx.google.com; spf=pass (google.com: domain of - designates 209.85.214.43 as permitted sender) smtp.mail=-; dkim=pass header.i=@gmail.com
Received: by mail-bk0-f43.google.com with SMTP id j5so5978375bkw.2
for <-@gmail.com>; Tue, 17 Apr 2012 05:07:58 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20120113;
h=date:from:to:message-id:subject:mime-version:content-type;
bh=Xj44O9fBIzirvcquOGxJbYLtbqgBc2Ags7prNUuAtSQ=;
b=WWTCdrFgs3RCT/g2qXHR0fLCTc73TmNMA15sff0oIDksB6Nn3IwTYAqTVmoGiNrkW5
08WDpTRADEKAOvjQ5FC9/uBCh1RXWTjxtawfjHc7vfUpqKbXOCj8Ab6GWXQMmX/+WB6T
KVYLhk3/+GddIJI1XsAX9zprSYVcP6MMJ5/U+idIDlC7xQGGNuvzvpAnlnlGuWzgKc6j
qPoFSAAaio6zICY9uSaI0deBIYTEQ2hIuBDJG8oaRvGVvpjhzJaBK+ab+rPJEboHPg8S
3WSCG/Pp212VOw/YXOLUQV0jmMbuqAbsGdeER+Okbwe11sWi+zvPz+jhplB0NB2wq0Fn
mWzA==
Received: by 10.204.130.13 with SMTP id q13mr4084640bks.128.1334664478358;
Tue, 17 Apr 2012 05:07:58 -0700 (PDT)
Return-Path: <-t@gmail.com>
Received: from Akoya ([xx])
by mx.google.com with ESMTPS id z14sm37467763bky.15.2012.04.17.05.07.51
(version=SSLv3 cipher=OTHER);
Tue, 17 Apr 2012 05:07:54 -0700 (PDT)
Date: Tue, 17 Apr 2012 05:07:54 -0700 (PDT)
From: BP MM <-@gmail.com>
To: xx xx <-@gmail.com>
Message-ID: <404745073.01334664469900.JavaMail.xx@Akoya>
Subject: TestImage
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_2_854532868.1334664469871"
------=_Part_2_854532868.1334664469871
Content-Type: multipart/mixed;
boundary="----=_Part_1_1457048287.1334664469871"
------=_Part_1_1457048287.1334664469871
Content-Type: image/png; name=logo.png
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=logo.png
Content-ID: img_cid
[base64 data]
------=_Part_1_1457048287.1334664469871--
------=_Part_2_854532868.1334664469871
Content-Type: multipart/alternative;
boundary="----=_Part_0_1033690582.1334664469858"
------=_Part_0_1033690582.1334664469858
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Hello
------=_Part_0_1033690582.1334664469858
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<html>
<body>
<H1>Hello</H1>
<img src="cid:img_cid">
</body>
</html>
------=_Part_0_1033690582.1334664469858--
------=_Part_2_854532868.1334664469871--
我尝试了不同的 CID,将 < 和=""> 添加到 CID,将配置设置为内联、验证 HTML 等。 无法使其正常工作,Thunderbird 和 Gmail 查看带有“死”图像的邮件。 我读到客户端在邮件中查找 cid,所以我认为它应该正确获取图像。 发送附件确实有效,但嵌入无效。 有人知道吗?与Multipart结构有关吗?
用于创建消息的 java 代码(我评论了一些试图解决它的行..)
/*
* The method to create a MimeMessage, this message can be sent with the transport method.
*/
private Message createMessage(final Mail mail, final Member member, final MailingList list,
final MailAccount mailAccount, final Session session)
throws MessagingException, UnsupportedEncodingException {
// Get attachments
@SuppressWarnings("unchecked")
List<File> attachments = (List<File>) mail.getProperty(Mail.ATTACHMENTS_KEY);
// Create content parts
String plainContent = templateEngine.replaceTemplateTags(mail, member, list, mailAccount);
String htmlContent = templateEngine.replaceHTMLTemplateTags(mail, member, list, mailAccount);
Multipart multiContentPart = new MimeMultipart("alternative");
// Multipart attachmentsBodyPart = new MimeMultipart();
Multipart rootBodyPart = new MimeMultipart();
// Create plain text part
if (!plainContent.equals("")) {
BodyPart plainMessageBodyPart = new MimeBodyPart();
plainMessageBodyPart.setContent(plainContent, "text/plain");
multiContentPart.addBodyPart(plainMessageBodyPart);
}
// Create html part
if (!htmlContent.equals("")) {
BodyPart htmlMessageBodyPart = new MimeBodyPart();
htmlMessageBodyPart.setContent(htmlContent, "text/html");
multiContentPart.addBodyPart(htmlMessageBodyPart);
}
// Create attachments
if (attachments != null && attachments.size() > 0) {
for (int i = 0; i < attachments.size(); i++) {
BodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachments.get(i));
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(attachments.get(i).getName());
attachmentBodyPart.setHeader("Content-ID", "<img_cid>");
attachmentBodyPart.setDisposition(Part.INLINE);
// attachmentsBodyPart.addBodyPart(attachmentBodyPart);
rootBodyPart.addBodyPart(attachmentBodyPart);
}
// Build attachments
// BodyPart attachmentsWrapper = new MimeBodyPart();
// attachmentsWrapper.setContent(attachmentsBodyPart);
// rootBodyPart.addBodyPart(attachmentsWrapper);
}
// Build content
BodyPart contentWrapper = new MimeBodyPart();
contentWrapper.setContent(multiContentPart);
rootBodyPart.addBodyPart(contentWrapper);
// Create message
Message message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
member.getEmail(), member.getName()));
message.setSubject((String) mail.getProperty(Mail.SUBJECT_KEY));
message.setContent(rootBodyPart);
// Add headers
message.setFrom(new InternetAddress(mailAccount.getFromAddress(),
mailAccount.getFromName()));
if (mailAccount.getReplyTo() != "" && mailAccount.getReplyTo() != null)
message.setReplyTo(InternetAddress.parse(mailAccount.getReplyTo()));
return message;
}
不要介意每个附件的 CID 相同,稍后这将是文件名 - 我现在只用一个附件进行测试。希望这有帮助..
最佳答案
我发现邮件源没有任何问题。这是邮件客户端处理 CID 的方式。 Gmail.com 要求 CID 设置在 < 和=""> 之间。 Thunderbird 还没有显示我的图像.. 我认为它需要在某种程度上是独一无二的。仍然没有想出为什么以及如何解决它。 通过 Embedding images into html email with java mail 找到的
关于java - 在 Java 邮件中嵌入图像 - CID 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10191109/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司
使用Ruby1.9.2运行IDE提示说需要gemruby-debug-base19x并提供安装它。但是,在尝试安装它时会显示消息Failedtoinstallgems.Followinggemswerenotinstalled:C:/ProgramFiles(x86)/JetBrains/RubyMine3.2.4/rb/gems/ruby-debug-base19x-0.11.30.pre2.gem:Errorinstallingruby-debug-base19x-0.11.30.pre2.gem:The'linecache19'nativegemrequiresinstall
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我