Reputation: 6963
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?
Upvotes: 7
Views: 15384
Reputation: 124
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);
}
}
Upvotes: 8
Reputation: 8027
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).
Upvotes: 8
Reputation: 434
I know this question is old, but consider following instructions from Google
You can connect to the Google Apps mail servers using SMTP or SSL. If you connect using SMTP, you can only send mail to Gmail or Google Apps users; if you connect using SSL, you can send mail to anyone.
If your device or application supports SSL, connect to smtp.gmail.com on port 465 or 587.
and you are using port 25.
Upvotes: 0
Reputation: 62792
I am not sure why your to address is not being set, I would put a logging statement to see what is being passed to the sendMail method.
In your class you should be using MailSender not MailSenderImpl as the MailSender is the interface you want to be using. In my application I use SimpleMailMessage instead of MimeMessage and that works for me, below is code snippet I have used in the past.
@Autowired
private MailSender mailSender;
// Email is my own Pojo with from, to, subject and body properties.
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void sendEmail(Email email)
{
// create email message
SimpleMailMessage msg = new SimpleMailMessage();
msg.setFrom(email.getFromAddress());
msg.setTo(email.getToAddress());
msg.setSubject(email.getSubject());
msg.setText(email.getBody());
// send the message using spring mail sender
this.mailSender.send(msg);
}
Upvotes: 0