使用Java在电子邮件正文中发送图像
我已经能够使用Java在电子邮件中以附件的形式发送图像。我现在尝试在电子邮件正文中发送相同的图像,如下所示:使用Java在电子邮件正文中发送图像,java,gmail,jakarta-mail,Java,Gmail,Jakarta Mail,我已经能够使用Java在电子邮件中以附件的形式发送图像。我现在尝试在电子邮件正文中发送相同的图像,如下所示: public static void main(String[] args) throws NoSuchProviderException, MessagingException { System.out.println("Sending mail..."); Properties props = new Properties(); props.setProper
public static void main(String[] args) throws NoSuchProviderException, MessagingException {
System.out.println("Sending mail...");
Properties props = new Properties();
props.setProperty("mail.smtp.starttls.enable", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.port", "587");
props.setProperty("mail.smtp.user", "mysusername");
props.setProperty("mail.smtp.password", "mypassword");
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("HTML mail with images");
message.setFrom(new InternetAddress("myaddress@gmail.com"));
message.setContent
("<h1>This is a test</h1>"
+ "<img src=\"C:/Users/pc/Desktop/Photos/Shammah.PNG\">",
"text/html");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("receiver@simbatech.biz"));
transport.connect();//This is line 46
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
当我为我的Gmail帐户使用正确的用户名和密码时,为什么身份验证失败?创建一个包含内联内容处置的多部分正文,并在base64中编码您的图像
请选中此项,以便了解一些详细信息(在Python中)您需要像这样声明图像:
<img src="cid:unique-name-or-id" />
将图像加载为MimeBodyPart,并将唯一名称或id与MimeBodyPart的文件名匹配。首先,请参阅的此JavaMail常见问题解答条目 然后,使用查看此JavaMail常见问题解答条目
请注意,没有“mail.smtp.password”属性。由于您没有提供密码,身份验证失败。请参阅下面的代码可能已满
class SimpleMail2 {
public static void main(String[] args) throws Exception{
System.out.println("Sending mail...");
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session mailSession = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("sender@gmail.com","password");
}
});
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress("sender@gmail.com"));
message.setSubject("HTML mail with images");
message.setRecipient(Message.RecipientType.TO, new InternetAddress("receiver@gmail.com"));
message.setText("Dear Mail Crawler," + "\n\n No spam to my email, please!");
MimeMultipart multipart = new MimeMultipart("related");
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<H1>Raghava chary</H1>" + "<img src=\"cid:image\">";
messageBodyPart.setContent(htmlText, "text/html");
multipart.addBodyPart(messageBodyPart);
try {
messageBodyPart = new MimeBodyPart();
InputStream imageStream = SimpleMail2.class.getClass().getResourceAsStream("/ab/log.gif");
DataSource fds = new ByteArrayDataSource(IOUtils.toByteArray(imageStream), "image/gif");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID","<image>");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
System.out.println("Done");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
类SimpleMail2{
公共静态void main(字符串[]args)引发异常{
System.out.println(“发送邮件…”);
Properties props=新属性();
put(“mail.smtp.host”、“smtp.gmail.com”);
props.put(“mail.smtp.socketFactory.port”,“465”);
put(“mail.smtp.socketFactory.class”、“javax.net.ssl.SSLSocketFactory”);
props.put(“mail.smtp.auth”,“true”);
props.put(“mail.smtp.port”,“465”);
Session mailSession=Session.getDefaultInstance(props,
新的javax.mail.Authenticator(){
受保护的密码身份验证getPasswordAuthentication(){
返回新密码身份验证(“sender@gmail.com“,”密码“);
}
});
Message Message=新的mimessage(mailSession);
message.setFrom(新的InternetAddress(“sender@gmail.com"));
message.setSubject(“带有图像的HTML邮件”);
message.setRecipient(message.RecipientType.TO,新的InternetAddress(“receiver@gmail.com"));
message.setText(“亲爱的邮件爬虫,”+“\n\n请不要向我的电子邮件发送垃圾邮件!”);
MimMultipart multipart=新的MimMultipart(“相关”);
BodyPart messageBodyPart=新的MimeBodyPart();
字符串htmlText=“Raghava chary”+”;
setContent(htmlText,“text/html”);
multipart.addBodyPart(messageBodyPart);
试一试{
messageBodyPart=新的MimeBodyPart();
InputStream imageStream=SimpleEmail2.class.getClass().getResourceAsStream(“/ab/log.gif”);
DataSource fds=new ByteArrayDataSource(IOUtils.toByteArray(imageStream),“image/gif”);
setDataHandler(新的DataHandler(fds));
messageBodyPart.setHeader(“内容ID”,即“”);
multipart.addBodyPart(messageBodyPart);
message.setContent(多部分);
传输。发送(消息);
系统输出打印项次(“完成”);
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
添加org.apache.commons.io.jar.zip和axiom-api-1.2.6.jar,并添加mail.jar和activation.jar,这是另一个常见的错误(今天我就知道了):图像的内容ID头必须在。如果不这样做,将破坏一些邮件程序(gmail、OS X 10.10),但不会破坏其他邮件程序(Outlook、iOS)。这些错误与身份验证有关,而不是与您的实际身体内容有关……您的设置有问题。好吧,这结束了一个漫长而混乱的谜团。谢谢!
class SimpleMail2 {
public static void main(String[] args) throws Exception{
System.out.println("Sending mail...");
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session mailSession = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("sender@gmail.com","password");
}
});
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress("sender@gmail.com"));
message.setSubject("HTML mail with images");
message.setRecipient(Message.RecipientType.TO, new InternetAddress("receiver@gmail.com"));
message.setText("Dear Mail Crawler," + "\n\n No spam to my email, please!");
MimeMultipart multipart = new MimeMultipart("related");
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<H1>Raghava chary</H1>" + "<img src=\"cid:image\">";
messageBodyPart.setContent(htmlText, "text/html");
multipart.addBodyPart(messageBodyPart);
try {
messageBodyPart = new MimeBodyPart();
InputStream imageStream = SimpleMail2.class.getClass().getResourceAsStream("/ab/log.gif");
DataSource fds = new ByteArrayDataSource(IOUtils.toByteArray(imageStream), "image/gif");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID","<image>");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
System.out.println("Done");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}