gpt4 book ai didi

spring-boot - 使用java电子邮件获取交货并发送收据

转载 作者:行者123 更新时间:2023-12-05 03:08:56 25 4
gpt4 key购买 nike

我正在使用以下代码。我正在尝试获取邮件的发送回执和送达回执。仍然不适用于此代码。

我尝试添加属性,但没有用。我有一个要求,我需要检查电子邮件是否成功发送。我已经编写了电子邮件发送的代码,但即使对于无效的电子邮件地址,它也会打印成功。如何使用 javamail API 跟踪电子邮件是否已发送

import java.io.ByteArrayOutputStream;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import javax.activation.DataSource;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.util.ByteArrayDataSource;

import org.apache.log4j.Logger;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.springframework.util.StringUtils;

@Service
public class VelocityMailServiceImpl implements MailService {

private static final Logger logger = Logger.getLogger(MailService.class);

@Autowired
private JavaMailSender mailSender;

@Autowired
private VelocityEngine velocityEngine;

@Autowired
private MessageSource messageSource;

@Value("$env{email.smtp.host}")
private String emailHost;

/**
* This method will send compose and send the message
* */
public void sendMail(String to, String from, String subject, String body) {
SimpleMailMessage message = new SimpleMailMessage();
String[] emailTos = null;

try{

if (to.contains(";")) {
emailTos = to.split(";");
} else if (to.contains(",")) {
emailTos = to.split(",");
} else {
emailTos = new String[] { to };
}
message.setTo(emailTos);
message.setFrom(from);
//message.addFrom(InternetAddress.parse(from));
message.setSubject(subject);
message.setText(body);
Properties props = new Properties();
// props.put("Return-Receipt-To", "admin@xyz.com");
// mailSender.setJavaMailProperties(props);
mailSender.send(message);
}catch(Exception e){
System.out.println("Error sending amil.....");
}
}

最佳答案

没有保证的方法可以确定消息是否已成功传递。如果这是您的要求,请立即放弃。

JavaMail FAQ 解释了 why you don't get an exception when you try to use an invalid address .

许多邮件服务器只是忽略发送到无效地址的邮件,以防止垃圾邮件发送者“探测”有效地址。

Delivery Status Notifications 有互联网标准,但同样许多服务器未实现标准或不返回错误地址的通知。

检测消息是否成功传递的最可靠方法是在消息中包含一个链接,并要求收件人单击该链接以验证消息是否已收到。显然这仍然不完美,因为他们可能永远不会点击链接。

关于spring-boot - 使用java电子邮件获取交货并发送收据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44369826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com