我正在尝试通过Javamail以编程方式向一组收件人发送电子邮件。我设置了一个MimeMessagePreparator并将其传递给JavaMailSenderImpl,但在发送电子邮件时,我得到以下内容:
org.springframework.mail.MailSendException:失败的消息: com.sun.mail.smtp.SMTPSendFailedException:550 5.1.0 <的 MYCOMPUTER !! >发件人拒绝
因此,发件人看起来像我的电脑。
这是我的代码:
public class EmailNotifier {
private JavaMailSenderImpl mailSender;
@Value("${application.mail.sender.address}")
private String senderEmail;
@Value("${application.mail.subject}")
private String emailSubject;
@Value("${application.mail.sender.address}")
private String senderAddress;
@Value("${application.mail.sender.password}")
private String senderPassword;
@Value("${application.mail.server.outgoing.port}")
private int outgoingPort;
@Value("${application.mail.server.outgoing}")
private String outgoingServer;
@Value("${application.mail.content}")
private String contentTemplate;
private static String BODY_CONTENT_TYPE = "text/html; charset=utf-8";
private static String PDF_CONTENT_TYPE = "application/pdf";
@PostConstruct
public void init() {
mailSender = new JavaMailSenderImpl();
mailSender.setPassword(senderPassword);
mailSender.setUsername(senderAddress);
mailSender.setPort(outgoingPort);
mailSender.setHost(outgoingServer);
}
public void sendEmail(InputStream pdfInputStream, String aRecipient) throws MessagingException {
final String commaSeparatedRecipientsEmail = getRecipients();
String content = contentTemplate;
MimeMessagePreparator msgPreparator = new MimeMessagePreparator() {
public void prepare(MimeMessage msg) throws Exception {
msg.addRecipients(Message.RecipientType.TO, emailsAgenzia);
msg.setSubject(emailSubject.replace(emailSubject);
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(content, BODY_CONTENT_TYPE);
MimeBodyPart attachmentPart = new MimeBodyPart();
ByteArrayDataSource pdf = new ByteArrayDataSource(pdfInputStream, PDF_CONTENT_TYPE);
attachmentPart.setDataHandler(new DataHandler(pdf));
String fileName = getFilename();
attachmentPart.setFileName(fileName);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(bodyPart);
multipart.addBodyPart(attachmentPart);
msg.setContent(multipart);
}
};
this.mailSender.send(msgPreparator);
}
}
出了什么问题?
答案 0 :(得分:0)
这是一个DNS问题。您必须在DNS系统中插入您的发件人地址。
https://digitaltoolfactory.net/blog/2010/03/how-to-fix-the-550-5-1-0-address-rejected-email-problem/
或者mailaddress可能无效。
答案 1 :(得分:0)
我最终不得不放弃JavaMailSenderImpl并使用较低级别的API(Javamail基本类而不是Spring类)。