1

I'm trying to use JavaMail, but I have some difficulty to understand how it works.

I use JavaMail for a section of my website, the "contact me" section.

<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="sender@gmail.com" /> /*Here there is my real gmail*/
    <property name="password" value="example" /> /*My real pwd*/

    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
        </props>
    </property>
</bean>

<bean id="mailMail" class="it.davidefruci.controllers.MailMail">
    <property name="mailSender" ref="mailSender" />
</bean>

I get the information from the email module of jsp with:

@RequestMapping("sendEmail")
public void send(@RequestParam("email") String from,
                 @RequestParam("subject") String subject,
                 @RequestParam("message") String message) {

    ApplicationContext context = 
         new ClassPathXmlApplicationContext("Spring-Mail.xml");

    MailMail mm = (MailMail) context.getBean("mailMail");
    mm.sendMail(from, "to@example.it", subject, message);
}

And this is the function that sends the email:

    public void sendMail(String from, String to, String subject, String message) {

    SimpleMailMessage mail = new SimpleMailMessage();

    mail.setFrom(from);
    mail.setTo(to);
    mail.setSubject(subject);
    mail.setText(message);

    mailSender.send(mail);
}

Ok, the email arrives to to@example.it and this is correct. But the sender is always sender@gmail.com (that I have configured in the xml file) despite mail.setFrom(from); method is used.

Why?

Dave
  • 1,428
  • 4
  • 31
  • 49

0 Answers0