我正在尝试向用户发送电子邮件。我已经使用了javax.mail。*和javax.mail.internet。*包并成功发送了一封电子邮件,但问题是它正在为一个邮件ID发送多封电子邮件。我怎样才能让它只发送一次?
我的代码:
public void sendEmail(){
System.out.println("called "+j+" time");
emailIDs.add(emailID);
to = emailID;
MimeMessage message = new MimeMessage(session1);
// Set From: header field of the header.
try {
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject(notifyName[j]+" "+"Document Alert Mail!");
// Send the actual HTML message, as big as you like
message.setContent("<h1>document created on "+createdOn+" is coming to expire</h1>",
"text/html" );
// Send message
Transport.send(message);
System.out.println("Message Sent Successfully....");
}
catch(Exception e){
e.printStackTrace();
}
}
答案 0 :(得分:0)
你应该在你的函数中提供参数。您调用emailID
函数时似乎已多次添加sendEmail()
元素。每次调用该函数时,都应该清理emailIDs
或者只需要向最后一个元素发送电子邮件。
这里我只发送一个收件人:
public void sendEmail(String emailID){
System.out.println("called "+j+" time");
emailIDs.add(emailID);
to = emailID;
MimeMessage message = new MimeMessage(session1);
// Set From: header field of the header.
try{
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject(notifyName[j]+" "+"Document Alert Mail!");
// Send the actual HTML message, as big as you like
message.setContent("<h1>document created on "+createdOn+" is coming to expire</h1>",
"text/html" );
// Send message
Transport.send(message);
System.out.println("Message Sent Successfully....");
}
catch(Exception e){
e.printStackTrace();
}
}