Newsletter

Spring Send Email With Attachment Using Gmail SMTP – Example

Spring » on Dec 11, 2012 { 12 Comments } By Sivateja

We know how to send a plane spring E-mail i mean without any attachments, Let us see how to send spring E-mail with attachment which is a similar application just like plane one, but with little modifications in MailLogic.java, will show you how..

Files Required

  • ClientLogic.java
  • MailLogic.java
  • spconfig.xml

Jars Required

  • activation.jar
  • classes12.jar
  • commons-email-1.0.jar
  • mail.jar

ClientLogic.java

package java4s;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class ClientLogic {

	public static void main(String[] args)
	{
		Resource res = new ClassPathResource("spconfig.xml");
		BeanFactory factory = new XmlBeanFactory(res);

		Object o = factory.getBean("id2");
		MailLogic ml = (MailLogic)o;

		ml.sendM("Enter from email id","Enter to email id","Spring Mail Using Google SMTP","This Is Body With Attachment");

	}

}

MailLogic.java

package java4s;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailParseException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailLogic {

       private JavaMailSender mail;

       public void setMail(JavaMailSender mail)
       {
           this.mail = mail;
       }

       public void sendM(String from, String to, String subject, String msg) 
       {
           try{

            MimeMessage Mimemessage = mail.createMimeMessage();

            MimeMessageHelper message = new MimeMessageHelper(Mimemessage, true);

            message.setFrom(from);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(msg);
            //message.setText("My alternative text", true);
            //message.addBcc("BCC email");
            //message.addCc("CC email");

            FileSystemResource file = new FileSystemResource("E:\\java4s.com.png");
            message.addAttachment(file.getFilename(), file);

            mail.send(Mimemessage);    

           }catch (MessagingException e) {
                throw new MailParseException(e);
           }

            System.out.println("Mail Sent Successfully With Attachment.....!");
        }

    }

spconfig.xml

 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

 <bean id="id1" class="org.springframework.mail.javamail.JavaMailSenderImpl">
     <property name="host" value="smtp.gmail.com" />
     <property name="port" value="465" />
     <property name="username" value="From Gmail email id" />
     <property name="password" value="and it password" />

     <property name="javaMailProperties">
       <props>
         <prop key="mail.smtp.auth">true</prop>
         <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
         <prop key="mail.smtp.socketFactory.port">465</prop>
         <prop key="mail.debug">true</prop>
         <prop key="mail.smtp.starttls.enable">true</prop>
       </props>
     </property>
</bean>

<bean id="id2" class="java4s.MailLogic">
<property name="mail" ref="id1" />
</bean>

</beans>

Output

 

Execution Flow

  • ClientLogic.java is our main class, and we are calling id2 [line number 15]
  • So come to spconfig.xml and check line number 23, we are calling java4s.MailLogic.java having property mail of type JavaMailSender Interface (provided by spring), so it will moves to JavaMailSenderImpl (id1, implemented class of JavaMailSender) and there we have given all the Email related credentials
  • JavaMailSender contains mail() method to send a mail
  • JavaMailSender‘s mail() method contains MimeMessage class object as parameter, like mail(MimeMessage Object)
  • Finally we set From,To,Subject,Msg and called send() that’s it

How simple its is ? 🙂

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
- Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

12 Responses to “Spring Send Email With Attachment Using Gmail SMTP – Example”
  1. shankar says:

    gr8 work .
    easy to understand for newcomer

  2. alam says:

    what is use of cloning plz specify any one…

    Regards
    A Alam.

  3. Hi Sivateja Kandula,

    I have gone through the complete tutorial. It is very helpful to me.

    Thanks a lot.

  4. How to do that for an email Id with 2 way verification on?

  5. jagadeesh says:

    Hi Teja,

    Thanks for your Tutorial,keep up the good work.

  6. Shaila Addagatla says:

    Hey Sivateja Kandula,

    Thank you for your tutorial.
    They are amazing 🙂 🙂

  7. please add the spring security concept(total) in spring framework

  8. Rajesh Kota says:

    Hello Java4s,

    Its an awesome web content for beginners….! halts off to you…! 🙂

  9. Ram says:

    Good article…

  10. Indraj says:

    Hello Java4s,

    Its an awesome web content for beginners….! keep it up

  11. rajeshwaru says:

    its really simple and helpful.. thank you so much 🙂

  12. Abhilash says:

    i feel it will be really better, when you create a youtube channel and start posting videos of the concepts

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2018 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.