我正在使用下面的代码。我想收到邮件的收据和送货单。仍然无法使用此代码。
我曾尝试添加属性,但没有用。我有一个要求,我需要检查电子邮件是否成功发送。我已经编写了电子邮件递送的代码,但即使对于无效的电子邮件地址也会打印成功。如何使用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.....");
}
}
答案 0 :(得分:3)
无法保证确定邮件是否成功发送。如果这是您的要求,请立即放弃。
JavaMail FAQ解释why you don't get an exception when you try to use an invalid address。
许多邮件服务器只是忽略发送到无效地址的邮件,以防止垃圾邮件发送者"探测"有效地址。
Delivery Status Notifications有互联网标准,但许多服务器不会实施标准或不会收到错误地址的通知。
检测邮件是否成功传递的最可靠方法是在邮件中包含一个链接,并请求收件人单击该链接以验证是否已收到邮件。显然这仍然不完美,因为他们可能永远不会点击链接。