Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set sender name in JavaMailSender

Tags:

java

email

spring

I'm using JavaMailSender to send an email to my client by my gmail account. In my gmail account settings, I can set value to "Send mail as" in order to display my customize name in my client email. For example, if I send email in gmail, my client email will show this:

From: This is my customize name (not my email address)

To: [email protected]

Subject ...

Body ...

How can I set up the configuration of JavaMailSender in Spring ?

This is my configuration file:

<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="[email protected]" />
    <property name="password" value="[email protected]" />

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

Thank in advance

like image 769
Leo Le Avatar asked Sep 08 '25 14:09

Leo Le


2 Answers

You need to use the MimeMessageHelper as suggested by mumbasa. You can configure it like

MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
message.setTo(...);
message.setFrom("Your Site <[email protected]>");
message.setSubject(...);

Note how message.setFrom(String from) is actually being set. Instead of setting the from address to something like:

[email protected]

Set it to

Your Site <[email protected]>

like image 104
mad_fox Avatar answered Sep 10 '25 03:09

mad_fox


To show email with a customized name showing in the inbox. I use MimeMessageHelper class from method. The one requiring two String parameters. It takes the email address and the customized name in this order

like image 28
mumbasa Avatar answered Sep 10 '25 04:09

mumbasa