2017-06-05 98 views
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....."); 
     } 
    } 

回答

1

有没有万无一失的方法,以确定消息是否已成功交付或不。如果这是你的要求,现在放弃。

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

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

Delivery Status Notifications有互联网标准,但许多服务器也不执行该标准或不返回错误地址的通知。

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

相关问题