In my android application i need to send the mail with an image as a attachment.I am done with the sending the mail.but how to send the mail with an image as a attachment to the mail.Here i am posting the code for the sending the mail.Please help me to send the image as an attachment in following code.
Here is the code-
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("abc@gmail.com", "pqr123%");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("abc@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("xyz@gmail.com"));
message.setSubject("Testing Subject");
message.setContent("Hi...", "text/html; charset=utf-8");
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
Try this code:
//This is how you attach image or any file to email
Let me know if it worked for you.
This will surely help you to attach your image without using intent.
//Got this solution form here