2014-04-01 83 views
0

這是我的code.Can任何人都可以告訴我爲什麼它發送一個電子郵件兩次給單身人士。
我必須只發送一個郵件。我將更改以獲取正確的結果?電子郵件收到兩次,但我只想要一次

public void dbbackup_notify(String email, String data, String subject) { 
    String toEmails = email; 
    Session session = Session.getInstance(props, newjavax.mail.Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(user, pass); 
     } 
    }); 
    Message message = new MimeMessage(session); 
    try { 
     message.setFrom(new InternetAddress(userid)); 
     message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails)); 
     message.setSubject(subject); 
     message.setText(data); 
     message.setContent(data, "text/html"); 

     transport = session.getTransport("smtp"); 
     transport.connect(host, user, pass); 
     transport.sendMessage(message, message.getAllRecipients()); 
     transport.close(); 
     System.out.println("Sent Successfully: " + new Date()); 
    } catch (AddressException e) { 
     e.printStackTrace(); 
    } catch (MessagingException e) { 
     e.printStackTrace(); 
     System.out.println("Don't Send Successfully" + new Date()); 
    } 
} 

回答

1

顯而易見的答案是因爲你調用它兩次。你怎麼知道你不是?

當您從程序中收到兩條消息時,它們是否都具有相同的消息ID?

如果您調用session.setDebug(true),您是否看到消息被髮送兩次?

順便說一下,請注意,setContent只是覆蓋setText的功能;你不需要兩個。將消息聲明爲MimeMessage類型,然後用message.setText(data,「utf-8」,「html」)替換這兩個set調用;

還請注意,您don't need the Authenticator因爲您在connect調用中傳遞用戶名和密碼。

相關問題