1)To create a Maven Projet.
2)Add the Dependency
Commons-Email in POM.xml file.
<!--
https://mvnrepository.com/artifact/org.apache.commons/commons-email -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-email</artifactId>
    <version>1.3.1</version>
</dependency>
3.Create a class and
implement the code
package Sampletester;
import
org.apache.commons.mail.DefaultAuthenticator;
import
org.apache.commons.mail.EmailAttachment;
import
org.apache.commons.mail.EmailException;
import
org.apache.commons.mail.MultiPartEmail;
public class NewTestEmailForm {
     public static void main(String[] args) throws EmailException {
           // TODO Auto-generated method stub
            
EmailAttachment attachment = new EmailAttachment();
            
attachment.setPath("D:\\test11.png");
            
attachment.setDisposition(EmailAttachment.ATTACHMENT);
            
attachment.setDescription("Picture of test");
            
attachment.setName("test report");
            
// Create the email message
            
MultiPartEmail email = new MultiPartEmail();
            
email.setHostName("smtp.gmail.com");
            
email.setSmtpPort(465);
            
email.setAuthenticator(new DefaultAuthenticator("yourgmailID@gmail.com", "yourgmailpassword"));
            
email.setSSLOnConnect(true);
            
email.setFrom("yourgmailID@gmail.com");
            
email.setSubject("TestMail Attachment");
            
email.setMsg("This is a test mail ...for attahment:-)");
             email.addTo("reeiveremail address@gmail.com");
            
// add the attachment
            
email.attach(attachment);
            
// send the email
            
email.send(); 
            
System.out.println("report");
     }
}
4.Open the Gmail Account and Check your Attachment.

 
No comments:
Post a Comment