Spring 3.0 SimpleMailMessage support

China☆狼群 提交于 2020-01-22 02:10:09

问题


I am receiving emails on my gmail account but the from is always myself and not the person´s email address, even if I hardcode it in the implementation class as message.setFrom("somebody@hotmail.com"); still does not work. Any ideas?

@Service("mailService")
    public class MailService {

        @Autowired
        private MailSender mailSender;
        @Autowired
        private SimpleMailMessage alertMailMessage;

        public void sendMail(String from, String to, String subject, String body) {

            SimpleMailMessage message = new SimpleMailMessage();

            message.setFrom(from);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(body);
            mailSender.send(message);

        }

<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="xxx@gmail.com"/>
            <property name="password" value="xxxx"/>
            <property name="javaMailProperties">
                <props>
                    <!-- Use SMTP transport protocol -->
                    <prop key="mail.transport.protocol">smtp</prop>
                    <!-- Use SMTP-AUTH to authenticate to SMTP server -->
                    <prop key="mail.smtp.auth">true</prop>
                    <!-- Use TLS to encrypt communication with SMTP server -->
                    <prop key="mail.smtp.starttls.enable">true</prop>
                    <prop key="mail.debug">true</prop>
                </props>
            </property>
        </bean>

回答1:


I don't think this is a Spring problem, but rather a Gmail security feature. You're basically trying to send emails through Gmail with many "From" using Spring?

Gmail support pages say:

Note for IMAP/POP users: If you access Gmail through a POP or IMAP email client (e.g. Outlook) and would like to send messages with a custom "from" address, you have two options. We recommend that you configure your email client with two outgoing SMTP servers, one for Gmail and one for your other address.

Or, you can use Gmail's outbound servers with a different "from" address. If you've already configured the alternate address, your message will be sent from:otheraddress@domain.com, sender:username@gmail.com, regardless of which custom from configuration you chose.

The custom 'From:' feature works only if you already own the account linked to the alternate address. To send mail with a different Gmail username, you must first sign up for that address.

To achieve that, you'd have to configure these email addresses first in your Gmail account. One configured, emails sent should look something like:

From: somebody@hotmail.com Sender: xxx@gmail.com

In email clients/webmail UIs, your recipients would read something like:

from somebody@hotmail.com via xxx@gmail.com

So the original sender address always shows up anyway.



来源:https://stackoverflow.com/questions/8429818/spring-3-0-simplemailmessage-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!