Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring/Java Mail: The FROM address is being ignored

I am using JavaMail with Spring FW. Everything is working nicely, but I don't know why the FROM address is always wrong; it seems to ignore it and say where it's really from instead. What I want will become clearer in a moment. First, here's my code:

CONFIG

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.gmail.com"/>
    <property name="port" value="25"/>
    <property name="username" value="[my gmail address]"/>
    <property name="password" value="[my password]"/>
    <property name="javaMailProperties">
        <props>
            <prop key="mail.transport.protocol">smtp</prop>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
            <!--<prop key="mail.debug">true</prop>-->
        </props>
    </property>
</bean>

HELPER CLASS

@Service("mailService")
public class MailService {

    @Autowired
    private JavaMailSenderImpl mailSender;

    public void sendMail(String from, String to, String subject, String body) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(body, true);
            mailSender.send(message);
        }
        catch (MessagingException ex) {
            Logger.getLogger(MailService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    //etc...
}

CONTROLLER CODE SNIPPET

mailService.sendMail(
    contactModel.getEmail(), //From
    Constants.DefaultEmailAddress, //To
    "Enquiry from site", "Phone: " + contactModel.getPhone() + "<br />Message: <br />" + contactModel.getMessage());

Basically, contactModel is a normal Java class with a few properties for collecting info from a user on the contact us form. When I send the e-mail, I am currently seeing the FROM address the same as the TO address. What I can't figure out is whether I need to make some change in the config or if maybe Gmail is perhaps not allowing me to do this. Thoughts, anyone?

like image 950
Matt Avatar asked Dec 19 '12 06:12

Matt


2 Answers

Google won't allow you to send mail from your account saying you are somebody you're not (other domain).

So it will overwrite Sender with account you authorized with saving what you've specified in X-google-original-from header. You have to add your external account as specified in https://support.google.com/mail/answer/22370?hl=en (or add your external domain to be managed in Google if you have Google Apps).

like image 136
Kangur Avatar answered Nov 10 '22 15:11

Kangur


To use gmail account with changed "setFrom" to another email address you have to do:

  1. Add new address to your gmail account. Instruction is here: https://support.google.com/mail/answer/22370?hl=en
  2. Set "Always send from a different address". Instruction is under the same link.
  3. In gmail account Settings/Accounts and Import/Send mail as set your new email address as default.
  4. In AppConfig.java type:

    @Bean
    public JavaMailSender javaMailSender() {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setHost("smtp.gmail.com");
    mailSender.setPort(587);
    mailSender.setUsername("[email protected]");
    mailSender.setPassword(your gmail's password);
    mailSender.getJavaMailProperties().setProperty("mail.smtp.auth", "true");
    mailSender.getJavaMailProperties().setProperty("mail.smtp.starttls.enable", "true");
    return mailSender;
    

    }

  5. In EmailServiceImplementation.java type this:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.stereotype.Service;
    
    @Service       
    public class EmailServiceImpl implements EmailService {
    @Autowired
    private JavaMailSender mailSender;
    @Override
    public void sendEmail(String fromAddress, String toAddress, String subject, String body) {
    SimpleMailMessage mailMessage = new SimpleMailMessage();
    
    mailMessage.setTo(toAddress);
    mailMessage.setFrom("[email protected]");
    mailMessage.setSubject(subject);
    mailMessage.setText(body);
    
    mailSender.send(mailMessage);
    }
    }
    
like image 34
sakul Avatar answered Nov 10 '22 13:11

sakul