1
我正在使用以下代码。我正准备发送邮件的收据和递送收据。仍然不适用于此代码。使用java电子邮件获取投递并发送收据
我试图添加属性,但没用。我有一个要求,我需要检查电子邮件是否成功传递。我已经写了电子邮件发送的代码,但它打印甚至是无效的电子邮件address.How成功追踪到的电子邮件是否送达或不使用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", "[email protected]");
// mailSender.setJavaMailProperties(props);
mailSender.send(message);
}catch(Exception e){
System.out.println("Error sending amil.....");
}
}