本文介绍了使用Java在电子邮件正文中发送图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经能够使用Java在电子邮件中发送图像作为附件。我现在试图在电子邮件正文中发送相同的图像像这样:
public static void main(String [] args)抛出NoSuchProviderException,MessagingException {
System.out.println(发送邮件...);
属性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);
会话mailSession = Session.getDefaultInstance(props,null);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject(带图像的HTML邮件);
message.setFrom(new InternetAddress([email protected]));
message.setContent
(< h1>这是测试< / h1>
+< img src = \C:/ Users / pc / Desktop / Photos /Shammah.PNG\\">,
text / html);
message.addRecipient(Message.RecipientType.TO,
new InternetAddress([email protected]));
transport.connect(); //这是第46行
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
我得到这个输出:
发送邮件...
DEBUG:setDebug:JavaMail版本1.4ea
DEBUG:getProvider()返回javax.mail.Provider [TRANSPORT,smtp ,com.sun.mail.smtp.SMTPTransport,Sun Microsystems,Inc]
DEBUG SMTP:useEhlo true,useAuth true
线程main中的异常javax.mail.AuthenticationFailedException $ javax中的
。 mail.Service.connect(Service.java:306)
在javax.mail.Service.connect(Service.java:156)
在javax.mail.Service.connect(Service.java:105)
at image.in.body.ImageInBody.main(ImageInBody.java:46)
Java结果:1
为什么当我为我的Gmail帐户使用正确的用户名和密码时,身份验证失败?
解决方案
您需要像这样声明您的图片:
< img src =cid:unique-name-or-id />
将图像加载为MimeBodyPart,并将唯一名称或ID与MimeBodyPart的FileName相匹配。
I have been able to send Image as an Attachment in an Email using Java. I am now trying to send the same image in the Email Body Like this:
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("[email protected]"));
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("[email protected]"));
transport.connect();//This is line 46
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
I am getting this output:
Sending mail...
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
Exception in thread "main" javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:306)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at image.in.body.ImageInBody.main(ImageInBody.java:46)
Java Result: 1
Why is authentication failing while I am using the correct username and Password for My Gmail account?
解决方案
You need to declare your images like this :
<img src="cid:unique-name-or-id" />
Load images as MimeBodyPart and match the unique-name-or-id with the FileName of the MimeBodyPart.
这篇关于使用Java在电子邮件正文中发送图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!