How to ignore or suppress or avoid sending duplicate mails in javamail api
843834Jul 15 2008 — edited Jul 16 2008Hi,
I am setting same email id multiple times. But I want that the user should receive only one mail even though their names appear multiple times. Can anyone tell me how to avoid sending duplicate emails even though their names appear multiple times in to/cc list
Rgds.,
VKSethi
import java.util.ArrayList;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class Test
{
public boolean SendBulkMail( ArrayList toAddr, String fromAddr, String subject, String body )
{
String mail = null;
Properties properties = new CRUProperties();
String mailhost = properties.getProperty("MX");
System.out.println( "Sending mail from " + mailhost );
properties.put( "mail.smtp.host", mailhost );
Session session = Session.getDefaultInstance( properties, null );
Message message = new MimeMessage(session);
javax.mail.Address fromaddresses[] = null;
javax.mail.Address toaddresses[] = null;
try
{
InternetAddress toAddrs[] = new InternetAddress[toAddr.size()];
for( int i=0; i<toAddr.size(); i++ )
{
toAddrs[i] = new InternetAddress( (String)toAddr.get(i) );
System.out.println( "adding address " + " at " + i + " " + (String)toAddr.get(i) );
}
message.setRecipients( javax.mail.Message.RecipientType.TO, toAddrs );
message.setFrom( new InternetAddress(fromAddr) );
System.out.println( "sending mail from " + fromAddr );
message.setSubject(subject);
message.setText(body);
Transport.send(message);
System.out.println("Successfully sent the message");
}
catch( AddressException e )
{
e.printStackTrace();
return false;
}
catch( MessagingException e )
{
e.printStackTrace();
return false;
}
return true;
}
public static void main( String[] args )
{
Test test = new Test();
ArrayList to = new ArrayList();
to.add( "shakeel.kadam@oracle.com" );
to.add( "shakeel.kadam@oracle.com" );
to.add( "myraid_77@yahoo.com" );
to.add( "shakeel.kadam@oracle.com" );
to.add( "myraid_77@yahoo.com" );
String from = "shakeel.kadam@oracle.com" ;
String subject = "test" ;
String body = "test" ;
boolean res = test.SendBulkMail( to, from, subject, body );
if ( res )
{
System.out.println( "Done ..." );
}
else
{
System.out.println( "Always handle the exception outside ..." );
}
}
}
class CRUProperties extends Properties
{
public CRUProperties()
{
this.setProperty( "MX" , "rgmapacsmtp.oraclecorp.com" );
}
}
example taken from : http://forum.java.sun.com/thread.jspa?threadID=571431
No answer. Repeating again