简体   繁体   English

从Centos 7服务器发送邮件不起作用

[英]Sending mail does not work from my Centos 7 server

In my java application I have a module that allows to send mails 在我的java应用程序中,我有一个允许发送邮件的模块

application.yml: application.yml:

...
    mail:
        host: smtp.gmail.com
        port: 587
        username: xxxxxx@gmail.com  #Replace this field with your Gmail username.
        password: xxxxxxx           #Replace this field with your Gmail password.
        protocol: smtp
        tls: true
        properties.mail.smtp:
            auth: true
            starttls.enable: true
            ssl.trust: smtp.gmail.com
...

MailService.java: MailService.java:

...
    @Async
    public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) {
        log.debug("Send email[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}",
            isMultipart, isHtml, to, subject, content);

        // Prepare message using a Spring helper
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, StandardCharsets.UTF_8.name());
            message.setTo(to);
            message.setFrom(Properties.getMail().getFrom());
            message.setSubject(subject);
            message.setText(content, isHtml);
            javaMailSender.send(mimeMessage);
            log.debug("Sent email to User '{}'", to);
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.warn("Email could not be sent to user '{}'", to, e);
            } else {
                log.warn("Email could not be sent to user '{}': {}", to, e.getMessage());
            }
        }
    }
...

When I use my application locally (Windows 10) I receive my emails. 当我在本地使用我的应用程序(Windows 10)时,我会收到我的电子邮件。 But when I deploy my application on my server Centos 7 I do not receive any mail and I have no error in my logs. 但是当我在我的服务器Centos 7上部署我的应用程序时,我没有收到任何邮件,我的日志中没有错误。

So I wonder if it is necessary to install a postfix or others on my server Centos 7 to relay the mail? 所以我想知道是否有必要在我的服务器Centos 7上安装postfix或其他人来传递邮件?

I opened port 587 25 on my Centos 7 firewall 我在Centos 7防火墙上打开了端口587 25

So I wonder if it is necessary to install a postfix or others on my server Centos 7 to relay the mail? 所以我想知道是否有必要在我的服务器Centos 7上安装postfix或其他人来传递邮件?

It shouldn't be necessary. 它没有必要。 The Javamail API should be able to talk directly to an SMTP server without any local intermediary to relay the email. Javamail API应该能够直接与SMTP服务器通信,而无需任何本地中介来中继电子邮件。

I opened port 587 25 on my Centos 7 firewall 我在Centos 7防火墙上打开了端口587 25

Unblocking out-bound connections on port 587 on your server's firewall would be necessary ... if it was blocked. 如果被阻止,则必须解锁服务器防火墙上端口587上的出站连接... But it is not usual to block outgoing ports. 但是阻止传出端口并不常见。

It is more likely that outgoing connections on port 587 are blocked by your ISP or cloud provider. 端口587上的传出连接更可能被您的ISP或云提供商阻止。 Check their policy on clients sending email. 查看客户发送电子邮件的政策。

Another possibility is that the emails are reaching the Google mail servers, but they get classified as spam ... and are silently filtered out. 另一种可能性是电子邮件到达谷歌邮件服务器,但它们被归类为垃圾邮件......并被静默过滤掉。


You can get more clues as to what is actually happening by setting the mail.debug property to true , or by calling setDebug(true) on the mail Session object. 通过将mail.debug属性设置为true ,或者通过在邮件Session对象上调用setDebug(true)setDebug(true)有关实际发生情况的更多线索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM