从 Java 应用程序发送电子邮件的异常:中继被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21884126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:14:58  来源:igfitidea点击:

Exception in sending emails from java application : Relaying denied

javaspringsmtpjavamailemailrelay

提问by Rahul Agrawal

We are using Spring Mail to send emails from java application org.springframework.mail.javamail.JavaMailSenderImpl

我们正在使用 Spring Mail 从 java 应用程序 org.springframework.mail.javamail.JavaMailSenderImpl 发送电子邮件

Spring Email Configuration is

Spring电子邮件配置是

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl" autowire-candidate="default">
        <property name="host" value="${test.email.host}" />
        <property name="port" value="${test.email.port}" />
        <property name="username" value="${test.email.username}" />
        <property name="password" value="${test.email.password}" />
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>

            </props>
        </property>
    </bean>

Java code snapshot is

Java代码快照是

    @Autowired(required = true)
    @Qualifier("errorMessageMailSender")
    JavaMailSenderImpl mailSender;

    ..............
    ..............

          try {
                MimeMessage mailMessage = buildEmailMimeMessage(properties,mimeMultipart);
                logger.info(String.format("Built MimeMessage object is <%s>",mailMessage));
                if (mailMessage != null) {
                    mailSender.send(mailMessage);
                    logger.info("Mail sent Successfully");
                }else{
                    logger.info("Mail send failed as Mail message object construction failed.");
                }
                result=true;
            } catch (Exception e) {
                logger.error("An exception occurred while sending mail :: " + e.getMessage());
            }

Property files

属性文件

test.email.host=mail.mydomain.net
test.email.port=2525
[email protected]
test.email.password=mypassword

But we are below exception, and email is not being sent

但是我们在例外情况下,并且没有发送电子邮件

An exception occurred while sending mail :: Failed messages: javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 <[email protected]>... Relaying denied

采纳答案by ?zgür Ero?lu

The smtp server that you are trying to send the mail to is rejecting to relay. If it is your own local server that is not a problem, you may change configurations to relay mails from your mydomain.net . But if it is an external server (gmail for example), you have to use a real registered domain.

您尝试将邮件发送到的 smtp 服务器拒绝中继。如果您自己的本地服务器没有问题,您可以更改配置以从 mydomain.net 中继邮件。但如果是外部服务器(例如 gmail),则必须使用真实的注册域。

To test your code against a mail server, I recommend you to setup a dockerized Apache James Server, create some test users on it and send and receive emails.

要针对邮件服务器测试您的代码,我建议您设置一个 dockerized Apache James 服务器,在其上创建一些测试用户并发送和接收电子邮件。

回答by Mounika Nannaka

Working code,

工作代码,

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class JavaEmail {

    Properties emailProperties;
    Session mailSession;
    MimeMessage emailMessage;

    public static void main(String args[]) throws AddressException,
            MessagingException {

        JavaEmail javaEmail = new JavaEmail();

        javaEmail.setMailServerProperties();
        javaEmail.createEmailMessage();
        javaEmail.sendEmail();
    }

    public void setMailServerProperties() {

        String emailPort = "587";//gmail's port

        emailProperties = System.getProperties();
        emailProperties.put("mail.smtp.port", emailPort);
        emailProperties.put("mail.smtp.auth", "true");
        emailProperties.put("mail.smtp.starttls.enable", "true");

    }

    public void createEmailMessage() throws AddressException,
            MessagingException {
        String[] toEmails = { "enter to mail ids" };
        String emailSubject = "Java Email";
        String emailBody = "This is an email sent by JavaMail api.";

        mailSession = Session.getDefaultInstance(emailProperties, null);
        emailMessage = new MimeMessage(mailSession);

        for (int i = 0; i < toEmails.length; i++) {
            emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails[i]));
        }

        emailMessage.setSubject(emailSubject);
        emailMessage.setContent(emailBody, "text/html");//for a html email
        //emailMessage.setText(emailBody);// for a text email

    }

    public void sendEmail() throws AddressException, MessagingException {

        String emailHost = "smtp.gmail.com";
        String fromUser = "enter from Id";//ignore gmail domain
        String fromUserEmailPassword = "enter password";

        Transport transport = mailSession.getTransport("smtp");

        transport.connect(emailHost, fromUser, fromUserEmailPassword);
        transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
        transport.close();

    }

}

回答by Rahil

from what I know, your emailservice has to be inside of your production sphere. you cannot hit prod smtp server from outside of that sphere. at least that was the case with the issue that I encountered.

据我所知,您的电子邮件服务必须在您的生产范围内。您无法从该领域之外访问 prod smtp 服务器。至少我遇到的问题就是这种情况。