Results 1 to 7 of 7

Thread: Spring Sending Email With Attachment

  1. #1
    Join Date
    Apr 2010
    Posts
    3

    Unhappy 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="texasbase22@gmail.com" />
    		<property name="to"	  	 value="texasbase22@gmail.com" />
    		<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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Apr 2010
    Posts
    3

    Default

    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

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Then there must be something wrong in your setup. Post the full stacktrace.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Apr 2010
    Posts
    3

    Thumbs up

    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

  6. #6
    Join Date
    Jan 2011
    Posts
    2

    Default 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("priyankatiwari.mca@gmail.com");

    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;
    }

  7. #7
    Join Date
    Jan 2011
    Posts
    2

    Default

    cud u please send me the code for attachment and jars

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •