Spring 3.0 SimpleMailMessage 支持 [英] Spring 3.0 SimpleMailMessage support

查看:14
本文介绍了Spring 3.0 SimpleMailMessage 支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过我的 gmail 帐户接收电子邮件,但发件人始终是我自己而不是该人的电子邮件地址,即使我在实现类中将其硬编码为 message.setFrom("somebody@hotmail.com"); 仍然不起作用.有什么想法吗?

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>

推荐答案

我不认为这是 Spring 的问题,而是 Gmail 的安全功能.您基本上是在尝试使用 Spring 通过带有许多发件人"的 Gmail 发送电子邮件?

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 支持页面说:

IMAP/POP 用户注意事项:如果您通过 POP 或 IMAP 访问 Gmail电子邮件客户端(例如 Outlook)并希望发送带有自定义发件人"地址,您有两种选择.我们建议您为您的电子邮件客户端配置两台传出 SMTP 服务器,一台用于Gmail 和一个用于您的其他地址.

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.

或者,您可以使用 Gmail 的出站具有不同发件人"地址的服务器.如果您已经配置备用地址,您的消息将被发送from:otheraddress@domain.com, sender:username@gmail.com, 不管您从配置中选择了哪个自定义.

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.

自定义发件人:"功能仅在您已经拥有该帐户时才有效链接到备用地址.使用其他 Gmail 发送邮件用户名,您必须先注册该地址.

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.

为此,您必须首先在您的 Gmail 帐户中配置这些电子邮件地址.一个已配置,发送的电子邮件应如下所示:

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

发件人:someone@hotmail.com发件人:xxx@gmail.com

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

在电子邮件客户端/网络邮件 UI 中,您的收件人会读到如下内容:

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

来自某人@hotmail.com 通过 xxx@gmail.com

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

所以原始发件人地址总是会显示出来.

So the original sender address always shows up anyway.

这篇关于Spring 3.0 SimpleMailMessage 支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆