Announcement Announcement Module
Collapse
No announcement yet.
Spring Sending Email With Attachment Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Sending Email With Attachment

    Hi Everyone,

    I'm trying to get an example of using Spring Mail to run.

    The example is composed of an one interface and two classes in the src directory as well as a configuration xml file:

    IMailer.java


    Code:
    package com.test.mail;
    
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    
    public interface IMailer {
    
    	public void SetSimpleMailMessage (SimpleMailMessage simpleMailMessage);
    
    	public void setMailSender(JavaMailSender mailSender);
    
    	public void sendMail(String greeting, String content);
    
    }

    Mailer.java

    Code:
    package com.test.mail;
    
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    
    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.MimeMessageHelper;
    
    public class Mailer implements IMailer {
    
    	private JavaMailSender 		mailSender;
    	private SimpleMailMessage 	simpleMailMessage;
    
    	@Override
    	public void SetSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
    		this.simpleMailMessage = simpleMailMessage;
    	}
    
    	@Override
    	public void setMailSender(JavaMailSender mailSender) {
    		this.mailSender = mailSender;
    	}
    
    	@Override
    	public void sendMail(String greeting, String content) {
    
    		MimeMessage message = mailSender.createMimeMessage();
    
    		try {
    			MimeMessageHelper helper = new MimeMessageHelper(message, true);
    
    			helper.setFrom(simpleMailMessage.getFrom());
    			helper.setTo(simpleMailMessage.getTo());
    			helper.setSubject(simpleMailMessage.getSubject());
    			helper.setText(String.format(
    					simpleMailMessage.getText(), greeting, content));
    
    			FileSystemResource file = new FileSystemResource("C:\\log.txt");
    
    			helper.addAttachment(file.getFilename(), file);
    		}
    
    		catch (MessagingException e) {
    			throw new MailParseException(e);
    		}
    
    		mailSender.send(message);
    		
    	}
    
    }

    App.java - To run the example

    Code:
    package com.test.mail;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class App {
    
        public static void main( String[] args )
        {
        	ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Mail.xml");
        	 
        	IMailer mailer = (IMailer) context.getBean("mailer");
            mailer.sendMail("Mr. SIRS", "This is some text content to test this application.");
        }
    
    }

    Spring-Mail.xml

    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
    	<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    		<property name="host"	  value="smtp.gmail.com" />
    		<property name="port"	  value="587" />
    		<property name="username" value="texasbase22" />
    		<property name="password" value="texasbase" />
    		
    		<property name="javaMailProperties">
    			<props>
                	<prop key="mail.smtp.auth">true</prop>
                	<prop key="mail.smtp.starttls.enable">true</prop>
            	</props>
    		</property>
    		
    	</bean>
    	
    	<bean id="mailer" class="com.orcww.mail.Mailer">
    		<property name="mailSender" 		ref="mailSender" />
    		<property name="simpleMailMessage" 	ref="customeMailMessage" />
    	</bean>
    
    	<bean id="customeMailMessage" class="org.springframework.mail.SimpleMailMessage">
    		<property name="from" 	 value="[email protected]" />
    		<property name="to"	  	 value="[email protected]" />
    		<property name="subject" value="Testing Subject" />
    		<property name="text">
    		<value>
    			<![CDATA[
    				Dear %s,
    				Mail Content : %s
    			]]>
    		</value>
    	</property>
    	</bean>
    
    </beans>

    I added the following jars to the Java Application: activation.jar, common-beanutils, commons-chain, commons-collections, commons-digester, commons-logging, commons-validator, log4j, mail.jar, servlet-api.jar, and spring.jar. Granted, some jars might not be needed.

    When I try to run the example I get:

    org.springframework.beans.factory.BeanCreationExce ption: error creating bean with name mailer defined in class pathSpring -Mail.xml. Initialization of bean failed; nested exception is org.springframework.beans.InvalidPropertyException : Invalid property ’simpleMailMessage’ of class com.test.mail.Mailer. No property simpleMailMessage found.

    Any help is appreciated. Thank you.

    Best regards,

    Rudi

  • #2
    Check your source.

    Code:
    @Override
    	public void SetSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
    		this.simpleMailMessage = simpleMailMessage;
    	}
    Although this is valid in .Net it isn't in java... get/set should start with lowercase.

    Comment


    • #3
      Hi Marten,

      Thanks for the comment. I appreciate your help.

      Good catch. Definitely that was wrong. I changed the name
      of the SimpleMailMessage setter to start with lower case:

      Code:
      	@Override
      	public void setSimpleMailMessage(SimpleMailMessage SimpleMailMessage) {
      		this.simpleMailMessage = simpleMailMessage;
      	}
      But the program still doesn't work. Now I get a NullPointerException.

      Exception in thread "main" java.lang.NullPointerException
      at com.test.mail.Mailer.sendMail(Mailer.java:32)
      at com.test.mail.App.main(App.java:14)

      The App.java:14 is the call to sendMail. The last line.

      The Mailer.java:32 is the first line in the sendMail method. The line:
      MimeMessage message = mailSender.createMimeMessage();

      Have a nice day. Thanks in advance again for your help.

      Best regards,

      Rudi

      Comment


      • #4
        Then there must be something wrong in your setup. Post the full stacktrace.

        Comment


        • #5
          Hi Marten,

          I got the example to work. I created a text file that the program attaches to the email and added some jars that the project needed.

          The jars that I added were the servlet-api, and a few commons. I already
          had the activation, mail, and spring jars.

          Have a great day. Thank you very much for your help.

          Best regards,

          Rudi

          Comment


          • #6
            subject is not working pls help me out

            Hi frnd,

            I m try to sent an email by using Spring .
            everything working fine but while i send email subject is not working even i hard coded that pls help me and also there is no error just subject is not working

            i m using this code

            @SpringBean
            JavaMailSender mailSender;

            @SpringBean
            SimpleMailMessage mailMessage;

            public void sendMail(String rfdAch,String subject){

            try {

            SimpleMailMessage message = new SimpleMailMessage(mailMessage);


            message.setTo("[email protected]");

            message.setSubject("Subject");

            message.setText("Test Mail");


            mailSender.send(message);



            }
            catch (MailException ex) {
            System.err.println(ex.getMessage());
            }
            catch (Exception ex) {
            System.err.println(ex.getMessage());
            }
            }

            public JavaMailSender getMailSender() {
            return mailSender;
            }

            public void setMailSender(JavaMailSender mailSender) {
            this.mailSender = mailSender;
            }

            public SimpleMailMessage getMailMessage() {
            return mailMessage;
            }

            public void setMailMessage(SimpleMailMessage mailMessage) {
            this.mailMessage = mailMessage;
            }

            Comment


            • #7
              cud u please send me the code for attachment and jars

              Comment

              Working...
              X