从java应用程序发送电子邮件时出现异常:中继被拒绝



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

春季电子邮件配置是

<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 代码快照是

    @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());
            }

属性文件

test.email.host=mail.mydomain.net
test.email.port=2525
test.email.username=demo@mydomain.net
test.email.password=mypassword

但我们低于例外情况,并且没有发送电子邮件

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 <demo@mydomain.net>... Relaying denied

您尝试向其发送邮件的 SMTP 服务器拒绝中继。如果不是问题,则可能是您自己的本地服务器,则可以更改配置以中继来自 mydomain.net 的邮件。但是,如果它是外部服务器(例如Gmail),则必须使用真正的注册域。

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

这个JavaMail常见问题条目应该会有所帮助:

  • 当我尝试发送消息时,为什么我会收到javax.mail.SendFailedException:550无法中继我的地址?

工作代码,

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();
    }
}

我所知,您的电子邮件服务必须在您的生产范围内。 您不能从该范围外部访问 PROD SMTP 服务器。 至少我遇到的问题是这种情况。

相关内容

  • 没有找到相关文章

最新更新