I am trying to attach a ".txt" file in a message. When I run the application, and in principle it is when the message should be sent, nothing is sent, I do not know if it is because it does not take the file well or because reason. I want the message to be sent automatically and the user does not have to interact with the application.
The code is as follows:
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pass);
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("correoOrigen"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("correoDestino"));
message.setSubject("asuntoEjemplo");
MimeBodyPart adjunto = new MimeBodyPart();
adjunto.setDataHandler(new DataHandler(new FileDataSource(new File(Environment.getDownloadCacheDirectory().getAbsolutePath() + "/ejemplo.txt"))));
adjunto.setContent(adjunto,"text/plain");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(adjunto);
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException e) {
Log.d("MailJob", e.getMessage());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
The file I want to send is in the device downloads folder, and its name is "example.txt". I've already been looking at several threads on the internet that talk about the same topic but I can not get it to work even if I take them as a reference, I think the problem is when it comes to taking the file, which I will have misplaced the address or something similar.