Tuesday, 29 May 2012

Sending Email Using Java



Current implementation is about sending email using simple java program by Java Mail API.

Generally for sending email we are approaching one of the email servers like Gmail, Yahoo etc... But in this way we need to compose the mail by adding To, CC, BCC, Subject and Mail Body.
Here my requirement is about sending room expenses details to all my roommates without composing the mail using any of the mail servers.

I have prepared one excel sheet which contains the details of room expenses of the current month and placed in my local directory (D:\Room\Room_expenses.xlsx).

Project Structure :


Below is the code snippet for sending mail with an attachment:

Note : Add mail.jar file to your project build path otherwise set as a class path in environment variables.

mail-config.properties :

mail.smtp.host=smtp.gmail.com
mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.port=587
           

SendEmail.java