Email Files Using Java

This is a sample method to email (using gmail) with attachment using Java:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
private static void emailWithAttachment(String from, String pass, String to, String subject, String body, String filename) {

 //Create object of Property file
 Properties props = System.getProperties();

 String host = "smtp.gmail.com";
 //Set the host of server
 props.put("mail.smtp.host", host);

 //Set the port of SMTP server
 props.put("mail.smtp.port", "587");

 //Set the authentication to true
 props.put("mail.smtp.auth", "true");
 props.put("mail.smtp.starttls.enable", "true");

 // This will handle the complete authentication
 Session session = Session.getDefaultInstance(props,
  new javax.mail.Authenticator() {
  protected PasswordAuthentication getPasswordAuthentication() {
  return new PasswordAuthentication("add your email", "add your password");
  }
 });

 //Passing user info
 props.put("mail.smtp.user", from);
 props.put("mail.smtp.password", pass);

 try {

  //Create object of MimeMessage class
  MimeMessage message = new MimeMessage(session);

  //Set from address
  message.setFrom(new InternetAddress(from));
  message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

  //Set subject
  message.setSubject(subject);

  //Create object to add multimedia type content
  BodyPart objMessageBodyPart1 = new MimeBodyPart();

  //Set the body of email
  objMessageBodyPart1.setText(body);

  //Create another object to add another content
  MimeBodyPart objMessageBodyPart2 = new MimeBodyPart();

  //Create data source to attach the file in mail
  DataSource source = new FileDataSource(filename);

  //Set the handler
  objMessageBodyPart2.setDataHandler(new DataHandler(source));

  //Set the file
  objMessageBodyPart2.setFileName(filename);

  // Create object of MimeMultipart class
  Multipart multipart = new MimeMultipart();

  // add body part 1
  multipart.addBodyPart(objMessageBodyPart1);

  // add body part 2
  multipart.addBodyPart(objMessageBodyPart2);

  // set the content
  message.setContent(multipart);

  // finally send the email
  Transport transport = session.getTransport("smtp");
  transport.connect(host, from, pass);
  transport.sendMessage(message, message.getAllRecipients());
  transport.close();

  System.out.println("=====Email Sent=====");
 }
 catch (Exception e) {
  e.printStackTrace();
 }
}


And this is how I call the above method when needed:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import java.util.Properties;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;

public class gmailEmail_02 {

static String from, pass, to, subject, body, filename;

public static void main (String[] args){
 from = "Your Gmail account";
 pass = "Your password";
 to = "Receiver's email";
 subject = "Subject line";
 body = "Body message";
 filename = "Path to file";
 emailWithAttachment(from, pass, to, subject, body, filename);
     }
}


 When I am using this email method to send the custom test run reports, I want to programatically find out the latest created file and for that I use this method to find the latest file.


No comments:

Post a Comment