Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9240987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:17:04+00:00 2026-06-18T08:17:04+00:00

I want to send mail using velocity templates. My configuration based on Spring 3.1

  • 0

I want to send mail using velocity templates.

My configuration based on Spring 3.1 documentation.

I have an xml file with configuration:

<?xml version="1.0" encoding="UTF-8"?>

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd&#8221;>

<bean id="sendMail" class="com.myclass.app.SendMail">
    <property name="mailSender" ref="mailSender"/>
    <property name="velocityEngine" ref="velocityEngine"/>
</bean>

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    <property name="velocityProperties">
        <value>
            resource.loader=class
            class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
        </value>
    </property>
</bean>

Then I have class:

@Controller
public class SendMail{


    private static final Logger logger = Logger.getLogger(SendMail.class);

    private JavaMailSender mailSender;
    private VelocityEngine velocityEngine;

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

    public void setVelocityEngine(VelocityEngine velocityEngine) {
        this.velocityEngine = velocityEngine;
    }


    @RequestMapping(value = "/sendEmail", method = RequestMethod.GET)
    public @ResponseBody void send(){

        sentEmail();

    }

    private void sentEmail(){

        try {


//            SimpleMailMessage msg = new SimpleMailMessage(mailMessage);

            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message,
                    true, "UTF-8");
            helper.setFrom("from@from.com");
            helper.setTo("myEmail");

            helper.setSubject("title");

            Map map = new HashMap();
            map.put("user", "Piotr");
            map.put("test", "TEST");

            String text1 = VelocityEngineUtils.mergeTemplateIntoString(
                    velocityEngine, "velocity.vm", map );

            logger.info(text1);

            String text = "test";

                helper.setText(text, true);


            mailSender.send(message);

        } catch (Exception ex) {
            System.out.println(ex.getStackTrace());
            System.out.println(ex.getMessage());
        }


    }

}

my vm file is located in “resources” folder.

And all i receive id “Null point Exception”;

The response doesn’t give me nothing more.

Do you have any ideas, what to do ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:17:05+00:00Added an answer on June 18, 2026 at 8:17 am

    Your templates can’t be in resources, they need to be in the classpath that you can configure in the velocityEngine
    in your applicationContext.xml :

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
      <property name="host" value="ADDRESS_OF_YOUR_SMTP_SERVER"/>
      <property name="defaultEncoding" value="UTF-8"/>
      <property name="port" value="XX_PORT_SERVER"/>
      <property name="javaMailProperties">  
          <props>
            <prop key="mail.smtps.auth">false</prop> <!-- depending on your smtp server -->
            <prop key="mail.smtp.starttls.enable">false</prop>
          </props>
      </property>
    </bean>
    
    <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean" p:resourceLoaderPath="classpath:META-INF/velocity" />
    

    Don’t forget the namespace xmlns:p=”http://www.springframework.org/schema/p” to use the properties of the above example.

    In your Java class SendMail, don’t forget to add the annotation @Autowired :

    @Autowired
    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }
    
    @Autowired
    public void setVelocityEngine(VelocityEngine velocityEngine) {
        this.velocityEngine = velocityEngine;
    }
    

    and finally, to send your email, use the MimeMessagePreparator:

                MimeMessagePreparator preparator = new MimeMessagePreparator() {
                @Override
                public void prepare(MimeMessage mimeMessage) throws Exception {
                   MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                   message.setTo("blabla@test.com");
                   message.setSubject("Email test");
    
                   String text = VelocityEngineUtils.mergeTemplateIntoString(
                       velocityEngine, template, keywords);
                   message.setText(text, true);
    
                 }
              };
    
              //Send email using the autowired mailSender
              this.mailSender.send(preparator);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to send mail using mail templates. To do this I want
I have installed apache-5.4.2, PHP-5.4.11 and Mysql-5.5.29 . I want to send mail using
I am using log4net. When Application gives an error, I want to send e-mail
I am using java to send mail. I want to set the from mail
I want to attach .vcf file with my mail and send through the mail.
I wanted to know how to send e-mail using javascript. I dont want to
I am using asp.net and C#. I want to send mail to my user
I am using asp.net 3.5 and C#. I want to send mail from asp.net,
I want to send HTML-emails, using Django templates like this: <html> <body> hello <strong>{{username}}</strong>
I want to send email to Yahoo mail using Indy. But Yahoo uses 465

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.