I have used
Standard Java,
Java Mail API and
Java Activation Framework
to solve the problem. Detailed description is commented in the sourcecode below.
Java sourcefile content :
public class SendMail {
public static void main(String[] args) {
/**
* create a screenshot
*/
try {
//capture screen as image
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
//create directory to store the image to
File folderForSaving = new File("C:\\screenshot_folder");
if(!folderForSaving.exists()){
folderForSaving.mkdir();
}
//store screenshot into folder
ImageIO.write(image, "png", new File("C:\\screenshot_folder\\screenshot.png"));
} catch (HeadlessException | AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* we also want to create some text file
*/
String text = "some text inside the file";
try {
File file = new File("C:\\screenshot_folder\\example.txt");
if(!file.exists()){
file.createNewFile();
}
BufferedWriter output = new BufferedWriter(new FileWriter(file));
output.write(text);
output.close();
} catch ( IOException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* here beginns the email part
*/
final String username = "your_username@gmail.com"; //sender's email
final String password = "your_password"; //sender's password
/**
* setup properties for gmail's smtp server
*/
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
/*
* create a session
*/
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("your_username@gmail.com"));//senders email
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("some_other_email@xyz.com")); // receivers email
message.setSubject("Testing Subject");
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("hi, how are you?");
// Create a multipart message, for the body text and the attachment(s)
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// attachment 1
messageBodyPart = new MimeBodyPart();
String filename = "C:\\km_ws14\\some_text_file.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// attachment 2
messageBodyPart = new MimeBodyPart();
filename = "C:\\screenshot_folder\\screenshot.png";
source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts (body text and the attachments)
message.setContent(multipart );
//send
Transport.send(message);
System.out.println("Done");
//delete files after sending
File file1 = new File("C:\\km_ws14\\some_text_file.txt");
File file2 = new File("C:\\km_ws14\\some_video.mp4");
if (file1.exists()) {
file1.delete();
}
if (file2.exists()) {
file2.delete();
}
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}