【发布时间】:2013-03-21 12:57:43
【问题描述】:
在我的应用程序中,我正在使用 smtp 发送邮件。我想将图像文件附加到邮件中。我该如何附加它?我试过了。但没有在邮件中获取图像。它给出了小图标。 请帮助。在此先感谢。 下面是邮件代码和邮件的外观-
public class MailImageFile extends javax.mail.Authenticator {
public MailImageFile(){
}
public void Mail(String user, String pass) {
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 session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USERNAME, PASSWORD);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(USERNAME));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(TO));
message.setSubject("Testing Subject");
Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/wallpaper.jpg"));
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("image.png");
messageBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
messageBodyPart.setHeader("Content-ID","<vogue>");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
【问题讨论】:
标签: android gmail image-file