Simple application to send an email using java application
Example :
- import javax.activation.DataHandler;
- import javax.activation.FileDataSource;
- import javax.mail.*;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- import java.util.Date;
- import java.util.Properties;
- public class EmailAttachmentDemo {
- public static void main(String[] args) {
- EmailAttachmentDemo demo = new EmailAttachmentDemo();
- demo.sendEmail();
- }
- public void sendEmail() {
- String from = "email@example.com";
- String to = "email@example.com";
- String subject = "Important Message";
- String bodyText = "This is a important message with attachment";
- String filename = "jee-sample/src/main/resources/data.txt";
- Properties properties = new Properties();
- properties.put("mail.smtp.host", "smtp.example.com");
- properties.put("mail.smtp.port", "25");
- Session session = Session.getDefaultInstance(properties, null);
- try {
- MimeMessage message = new MimeMessage(session);
- message.setFrom(new InternetAddress(from));
- message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
- message.setSubject(subject);
- message.setSentDate(new Date());
- // Set the email message text.
- MimeBodyPart messagePart = new MimeBodyPart();
- messagePart.setText(bodyText);
- // Set the email attachment file
- MimeBodyPart attachmentPart = new MimeBodyPart();
- FileDataSource fileDataSource = new FileDataSource(filename) {
- @Override
- public String getContentType() {
- return "application/octet-stream";
- }
- };
- attachmentPart.setDataHandler(new DataHandler(fileDataSource));
- attachmentPart.setFileName(fileDataSource.getName());
- Multipart multipart = new MimeMultipart();
- multipart.addBodyPart(messagePart);
- multipart.addBodyPart(attachmentPart);
- message.setContent(multipart);
- Transport.send(message);
- } catch (MessagingException e) {
- e.printStackTrace();
- }
- }
- }
Description:
Step 1 - 9 : All files are simply imported.
Step 11: Class is defined.
Step 12 -15: Here object of class is created and "sendEmail()" is called.
Step 18 - 22: Variables themselves describe their role.
Step 16: Here starts the "sendEmail()"
Step 12 -15: Here object of class is created and "sendEmail()" is called.
Step 18 - 22: Variables themselves describe their role.
Step 16: Here starts the "sendEmail()"