我正在尝试编写一个电子邮件客户端,以便向我的Gmail发送电子邮件。我可以成功验证我的电子邮件帐户,因此也可以发送邮件。在我的电子邮件的另一端,我收到了这封电子邮件,但是当我附上一封邮件时发送电子邮件时图像,接收方表示图像无效,任何帮助都会赞赏。
这是我的电子邮件功能
public synchronized void sendMail(String subject, String body, String sender, String recipients, String image) throws Exception {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(sender));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(sender));
// Set Subject: header field
message.setSubject("Testing Subject");
// This mail has 2 part, the BODY and the embedded image
MimeMultipart multipart = new MimeMultipart("related");
// first part (the html)
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<H1>"+body+"</H1><img src=\"cid:image\">";
messageBodyPart.setContent(htmlText, "text/html");
// add it
multipart.addBodyPart(messageBodyPart);
// second part (the image)
messageBodyPart = new MimeBodyPart();
System.out.println( Environment.getExternalStorageDirectory()+"/"+"1409752026872.jpg");
DataSource fds = new FileDataSource(Environment.getExternalStorageDirectory()+"/"+"1409752026872.jpg");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "image");
// add image to the multipart
multipart.addBodyPart(messageBodyPart);
// put everything together
message.setContent(multipart);
// Send message
Transport.send(message);
System.out.println("Sent message successfully...."+image); }
答案 0 :(得分:1)
您使用什么邮件阅读器来显示邮件?
尝试进行此更改:
messageBodyPart.setHeader("Content-ID", "<image>");