I've got an function to send a mail, but now i need to attach a document but i don't know how to do it or if i can do it, here is the code
public void enviar(){
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", true); // added this line
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.user", "[email protected]");
props.put("mail.smtp.password", "xxxx");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", true);
Session session = Session.getInstance(props,null);
MimeMessage message = new MimeMessage(session);
// Create the email addresses involved
try {
InternetAddress from = new InternetAddress("[email protected]");
message.setSubject("Informe fichadas");
message.setFrom(from);
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
// Create a multi-part to combine the parts
Multipart multipart = new MimeMultipart("alternative");
// Create your text message part
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("some text to send");
// Add the text part to the multipart
multipart.addBodyPart(messageBodyPart);
// Create the html part
messageBodyPart = new MimeBodyPart();
String htmlMessage = "Informe fichadas";
messageBodyPart.setContent(htmlMessage, "text/html");
multipart.addBodyPart(messageBodyPart);
// Associate multi-part with message
message.setContent(multipart);
// Send message
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com", "user", "pass");
System.out.println("Transport: "+transport.toString());
transport.sendMessage(message, message.getAllRecipients());
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
How i can attach a file? the file is in this path (C:\hola.txt)
Add
Right above
source: http://www.tutorialspoint.com/javamail_api/javamail_api_send_email_with_attachment.htm