Sending mail in java is very easy just follow the below stpes-
Step 1- Create a property with smtp host, smtp port. Here I’m going the send the mail using gmail so I’m using smtp host as smtp.gmail.com and port is 465.
Add activation.jar and mail.jar
Note: This smtp host and port is varying with mail server.
Step 2- Create Authenticator object (using your gmail id and password) this is mandatory for gmail authentication.
Step 3- Create a session using above property and authentication objects.
Step 4- Create a message using MimeMessage object and set the required property like subject, from address, to address and message.
Step 5- Create message body in form of HTML. Here I’m giving one text with hyperlink.
Step 6- Attach a file. For this create DataSource object with file name. Here I’m attaching a text file you can attach any of file format.
Step 7- Add message body to Multipart and later add this multi part to Message.
Step 8- Send the message using send() method (static method of Transport class) by passing message object as parameter.
Example :
Attachment2.java
Step 1- Create a property with smtp host, smtp port. Here I’m going the send the mail using gmail so I’m using smtp host as smtp.gmail.com and port is 465.
Add activation.jar and mail.jar
Note: This smtp host and port is varying with mail server.
1 2 3 4 | 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", "465"); |
Step 2- Create Authenticator object (using your gmail id and password) this is mandatory for gmail authentication.
1 | Authenticator auth = new SMTPAuthenticator(); |
1 2 3 4 5 | private class SMTPAuthenticator extends javax.mail.Authenticator { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("ashishkumarsinghmca@gmail.com", "yourpassword"); } } |
Step 3- Create a session using above property and authentication objects.
1 | Session session = Session.getInstance(props, auth); |
Step 4- Create a message using MimeMessage object and set the required property like subject, from address, to address and message.
1 2 3 4 5 | MimeMessage msg = new MimeMessage(session); msg.setSubject("this is test mail subject"); msg.setFrom(new InternetAddress("ashishkumarsinghmca@gmail.com")); msg.addRecipient(Message.RecipientType.TO, new InternetAddress("ashishkumarsinghmca@gmail.com")); msg.setText("this is body of mail"); |
Step 5- Create message body in form of HTML. Here I’m giving one text with hyperlink.
1 2 3 4 | BodyPart messageBodyPart = new MimeBodyPart(); String htmlText = "<H1>Hello</H1><br/><a href=\"http://www.gmail.com\">gmail.com</a> "; messageBodyPart.setContent(htmlText, "text/html"); multipart.addBodyPart(messageBodyPart); |
Step 6- Attach a file. For this create DataSource object with file name. Here I’m attaching a text file you can attach any of file format.
1 2 3 4 | String filename = "H:\\StickNote.txt"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); |
Step 7- Add message body to Multipart and later add this multi part to Message.
1 2 | multipart.addBodyPart(messageBodyPart); message.setContent(multipart); |
Step 8- Send the message using send() method (static method of Transport class) by passing message object as parameter.
1 | Transport.send(msg); |
Example :
Attachment2.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 | package com.mymail; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class Attachment2 { Attachment2() { String to = "ashishkumarsinghmca@gmail.com"; // Sender's email ID needs to be mentioned String from = "ashishkumarsinghmca@gmail.com"; final String username = "ashishkumarsinghmca@gmail.com";//change accordingly final String password = "yourpassword";//change accordingly 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", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); Authenticator auth = new SMTPAuthenticator(); Session session = Session.getInstance(props, auth); try { // Create a default MimeMessage object. Message message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to)); // Set Subject: header field message.setSubject("Testing Subject"); // This mail has 2 part, the BODY and the embedded image MimeMultipart multipart = new MimeMultipart("related"); // first part (the html) BodyPart messageBodyPart = new MimeBodyPart(); String htmlText = "<H1>Hello</H1><br/><a href=\"http://www.gmail.com\">gmail.com</a> "; messageBodyPart.setContent(htmlText, "text/html"); // add it multipart.addBodyPart(messageBodyPart); // second part (the image) messageBodyPart = new MimeBodyPart(); // Part two is attachment String filename = "H:\\StickNote.txt"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); // add image to the multipart multipart.addBodyPart(messageBodyPart); // put everything together message.setContent(multipart); // Send message Transport.send(message); System.out.println("Sent message successfully...."); } catch (MessagingException e) { e.printStackTrace(); } } private class SMTPAuthenticator extends javax.mail.Authenticator { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("ashishkumarsinghmca@gmail.com", "yourpassword"); } } public static void main(String[] args) { Attachment2 at=new Attachment2(); } } |
No comments :
Post a Comment