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

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

问题描述

我在我的gmail账户上收到电子邮件,但收件人始终是我自己,而不是个人的电子邮件地址,即使我将它作为 message.setFrom(somebody @ hotmail .com); 仍然不起作用。任何想法?

  @Service(mailService)
公共类MailService {

@Autowired
私人MailSender mailSender;
@Autowired
私人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);

$ b






 < bean id =mailSenderclass =org.springframework.mail.javamail.JavaMailSenderImpl> 
< property name =hostvalue =smtp.gmail.com/>
< property name =portvalue =25/>
< property name =usernamevalue =xxx@gmail.com/>
< property name =passwordvalue =xxxx/>
< property name =javaMailProperties>
<道具>
<! - 使用SMTP传输协议 - >
< prop key =mail.transport.protocol> smtp< / prop>
<! - 使用SMTP-AUTH认证SMTP服务器 - >
< prop key =mail.smtp.auth> true< / prop>
<! - 使用TLS加密与SMTP服务器的通讯 - >
< prop key =mail.smtp.starttls.enable> true< / prop>
< prop key =mail.debug> true< / prop>
< /道具>
< / property>
< / bean>


解决方案

我不认为这是Spring问题,而是一项Gmail安全功能。
您基本上是想通过Gmail使用许多发件人使用Spring发送电子邮件?



Gmail支持页面说:


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

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



自定义的发件人:功能仅在您已拥有链接到备用地址的帐户
时才有效。要使用不同的Gmail
用户名发送邮件,您必须先注册该地址。


为了达到这个目的,您必须先在您的Gmail帐户中配置这些电子邮件地址。
配置一个,发送的电子邮件应该如下所示:


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


在电子邮件客户端/网络邮件用户界面中,收件人会看到如下内容:


from somebody@hotmail.com via xxx@gmail.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>

解决方案

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.

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

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