2017-06-05 99 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有互聯網標準,但許多服務器也不執行該標準或不返回錯誤地址的通知。

檢測郵件是否成功傳遞的最可靠方法是在郵件中包含一個鏈接,並請求收件人單擊鏈接以驗證郵件是否已收到。顯然這還不是完美的,因爲他們可能永遠不會點擊鏈接。

相關問題