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?
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).
To use gmail account with changed "setFrom" to another email address you have to do:
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;
}
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With