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


Reply With Quote
