Sending mails from Web Application using Spring?

You can use Spring's mail abstraction layer to easily send emails. Define the following beans in your applicationContext.xml.

You can use Spring's mail abstraction layer to easily send emails. Define the following beans in your applicationContext. Xml whateverSenderAddress Then, in mypackage.

MySendMailService: public class SendMailService { private MailSender mailSender; private SimpleMailMessage emailTemplate; public void sendEmail(String to, String from, String subject, String body) throws MailException { SimpleMailMessage message = new SimpleMailMessage(this. EmailTemplate); message. SetTo(to); message.

SetFrom(from); message. SetSubject(subject); message. SetText(body); mailSender.

Send(message); } public void setMailSender(MailSender mailSender) { this. MailSender = mailSender; } public void setEmailTemplate(SimpleMailMessage emailTemplate) { this. EmailTemplate = emailTemplate; } }.

Thanks That's what I have been looking for – Khalif Oct 18 at 14:41.

Spring MVC is a web framework, and it has nothing to do with email. Another part of the Spring framework does support mail sending though. Take a look at chapter 24 of the ref guide.

I'm using Apache Velocity as E-Mail templating system. You can define an instance of "VelocityEngine" as a spring bean and inject it into your controllers. The much cleaner solution however is to put the mail-sending code into a service and inject your service into your controller.

@Autowired private VelocityEngine velocityEngine; @Autowired private JavaMailSender mailSender; MimeMessagePreparator preparator = new MimeMessagePreparator() { @Override public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message. SetTo("customer@mail. Com"); message.

SetFrom("noreply@mail. Com"); message. SetSubject("You got mail!"); Map model = new HashMap(); model.

Put("param1", new Date()); String text = VelocityEngineUtils. MergeTemplateIntoString( velocityEngine, "com/myapp/mailtemplates/email. Vm", model ); mimeMessage.

SetText(text,"utf-8", "html"); mimeMessage. SetHeader("Content-Type", "text/html; charset=utf-8"); } }; mailSender. Send(preparator); The HashMap can be used to pass parameters you can then use inside your velocity template.

Then you can send your email using a "JavaMailSender" which can also be define as a spring bean. You can define the mailSender and the velocityEngine beans similar to this: true resource. Loader=class class.resource.loader.

Class=org.apache.velocity.runtime.resource.loader. ClasspathResourceLoader.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions