Home arrow static arrow Java Programming [Archive] - MultiThreaded Mail Program
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - MultiThreaded Mail Program
10 Duke Stars available
This topic has 1 reply on 1 page.

Posts:18
Registered: 3/20/03
MultiThreaded Mail Program  
Jul 8, 2004 11:12 PM



 
I have attached a snippet of my program where i create two thread to send mails. Please suggest to imporve this code.
In future the mail IDS will be fetched from database.

import java.util.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

class ThreadTest implements Runnable{

HashSet hs;
Iterator it;
boolean bool= true;

ThreadTest(){
hs = new HashSet();

hs.add("email ID");
hs.add("email ID");
hs.add("email ID");
hs.add("email ID");
hs.add("email ID");
hs.add("email ID");

it = hs.iterator();

for(int i=0;i<2;i++){
Thread t = new Thread(this);
t.start();
}
}
public void run(){
while(bool){
sendMail();
}
}

public void sendMail() {

String host = "Ip Address";
String from = "test@test.com";
String subject = "Test mail";
String bodytext = "Test message";

boolean sent=false;
boolean sessionDebug = false;

try {

sessionDebug = false;
Properties props = System.getProperties();
props.put("mail.host",host);
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.host",host);
props.put("mail.smtp.dsn.notify","SUCCESS,FAILURE ORCPT=rfc822;Email ID");
props.put("mail.smtp.dsn.ret", "FULL");

Session mailSession = Session.getDefaultInstance(props,null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);

msg.setFrom(new InternetAddress(from));

msg.setSubject(subject);
msg.setSentDate(new Date());

String to = getData();
InternetAddress toaddress = new InternetAddress(to);
msg.setRecipient(Message.RecipientType.TO,toaddress);
msg.setContent(bodytext,"text/html");
Transport.send(msg);

System.out.println(" Message were sent by thread "+ Thread.currentThread());

}catch(Exception e){}
}

public String getData(){

String val = "";
synchronized(it){
if(it.hasNext()){
val = (String)it.next();
}else{
bool = false;
}
}
return val;
}

public static void main(String args[]){
new ThreadTest();
}
}

Cheers
Diva.

 

Posts:11,200
Registered: 7/22/99
Re: MultiThreaded Mail Program  
Jul 9, 2004 12:17 AM (reply 1 of 1)



 
suggestions:

Overall: document and comment your code. Use javadoc comments if possible. Make your class public and move it to a package so that it's possible to use it from real code.

import java.util.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

Minor style improvement: spell out which classes you use, don't use import-on-demand for everything.

HashSet hs;
Iterator it;
boolean bool= true;

You should use variable names that have a meaning. "HashSet hs" says only that hs is a HashSet but not what it's for. Write a comment that says what type of elements will be stored in the set and what type of elements will be available through the iterator. Consider changing the HashSet to Set.

ThreadTest(){

This constructor should be public.

String host = "Ip Address";
String from = "test@test.com";
String subject = "Test mail";
String bodytext = "Test message";
You should look into making these values parameters that are passed to the method rather than hardcoding them...

Properties props = System.getProperties();
props.put("mail.host",host);
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.host",host);
props.put("mail.smtp.dsn.notify","SUCCESS,FAILURE ORCPT=rfc822;Email ID");
props.put("mail.smtp.dsn.ret", "FULL");

Is it really necessary to modify the system properties for this?

...
 
This topic has 1 reply on 1 page.