我正在尝试做一个Android应用程序,它可以自动发送图像,而不需要用户的任何干预。
为此,我使用了javax.mail
jar (以及activation.jar
和additional.jar
)。我正在做以下事情
Properties props = new Properties();
props.put("mail.smtp.auth", true);
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.ssl.enable",true);
props.put("mail.smtp.port", "465");
Session session = Session.getInstance(props,new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USER,PASS);
}
});
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("*******@gmail.com"));
message.setSubject("Email Subject");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("******@gmail.com"));
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent("<img src=\"image.gif\"/>","text/html");
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
FileDataSource source = new FileDataSource(new File(ruta));
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("picture.gif");
messageBodyPart.setDisposition("inline");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
当我执行这段代码时,我得到以下错误
W/System.err(24907):发送消息时的javax.mail.MessagingException: IOException;
W/System.err(24907):嵌套异常为:
W/System.err(24907):MIME类型multipart/mixed的javax.activation.UnsupportedDataTypeException:无对象DCH;
带系统错误(24907):boundary="----=_Part__1095625208.1397499270313“
W/System.err(24907):在com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1182)
W/System.err(24907):在javax.mail.Transport.send0(Transport.java:254)
W/System.err(24907):在javax.mail.Transport.send(Transport.java:124)
你知道我怎么解决这个问题吗?
我使用的是一台Debian 64-bits
电脑和Eclipse Java EE IDE for Web Developers - Kepler
。如果解决方案需要它...
https://stackoverflow.com/questions/23067408
复制相似问题