1

I am trying to configure JavaMailSender in my Spring Web App. On order completion, it sends out the email which is working perfectly fine. However, the from email address it picks is the one in the configuration(a@gmail.com) and not the one I set in MimeMessageHelper(b@gmail.com).Below is my configuration file:

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="${mail.host}" />
    <property name="port" value="${mail.port}" />
    <property name="username" value="${mail.username}" /> // a@gmail.com
    <property name="password" value="${mail.password}" />



    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
            <prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
        </props>
    </property>

</bean>

<bean id="mailSenderUtil" class="com.autoshipcart.payment.util.MailSenderUtil">
    <property name="mailSender" ref="mailSender"></property>
</bean>

This is what goes in my JAVA class:

            MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        helper.setTo(toEmailAddress);
        helper.setFrom(new InternetAddress(fromEmailAddress)); // b@gmail.com
        helper.setSubject(subject);
        helper.setText(text, ishtml);
        this.mailSender.send(mimeMessage);  

I have even tried MimeMessagePreparator. Please help!

user2879206
  • 13
  • 2
  • 8
  • You will find a more dynamic approach to your question [https://stackoverflow.com/questions/2016190/how-to-configure-spring-javamailsenderimpl-for-gmail](https://stackoverflow.com/questions/2016190/how-to-configure-spring-javamailsenderimpl-for-gmail) – sam Nov 05 '17 at 19:15

1 Answers1

4

It isnt a problem with your java code. Its Gmail - because you are sending the mail through your gmail account, google are overwriting the From address to your gmail username.

If you view the sent email in its original format with all its headers - you will seeX-Google-Original-From which will contain the from address you set in the MimeMessage - but the From field will be the gmail username.

Louise Miller
  • 3,069
  • 2
  • 12
  • 13