Link to home
Start Free TrialLog in
Avatar of Jagadeesh M
Jagadeesh MFlag for United States of America

asked on

Javamail with threads

Hi Guys,

I need to send more number of mails per minute...i believe Multithreading would help me solve the problem....Does anyone have a previously implemented sample code for sending and receiving mails using multithreading/threading

I need sample code snippets please.

Thank You.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Increasing the number of threads won't in itself help you to increase your mailing rate ;-) It will potentially slow it down, giving you the overhead of thread scheduling
Avatar of Jagadeesh M

ASKER

But at the same time if my system has an overhead of bombarding more number of mails per min then i definetely need to  use threads ...
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm not sure *anyone* should be helping with this question ;-)
Why? Its more about multi-threading than Java mail, actually:

http://java.sun.com/docs/books/tutorial/essential/threads/index.html
Sounds as if it *could* be more about successful spamming than anything else ;-)
Why do you say so, CEHJ :) maybe you're in a different mood today - don't discourage the asker ;-)
I'll let him/her tell me otherwise ;-)
Avatar of javaoptimizer
javaoptimizer

Hi ,

just place the code that sends the mail in a run method, and you can have constants file where you can specify the various parameters
like to , from , subject etc.

Class sendmails implements runnable
{

public void run()
{
      Properties prop = new Properties();
      prop.put(Constantes.MAIL_TRANSPORT_PROTOCOL, Constantes.SMTP);
      prop.put(Constantes.MAIL_SMTP_HOST,host);
      prop.put(Constantes.MAIL_SMTP_PORT,  (PORT);

      Session mailSession = Session.getInstance(prop);

      InternetAddress recipient;

      setMessage( new MimeMessage(mailSession) );

       message.setFrom(new InternetAddress("shiv.thbs@gmail.com"));

     in the same way set the javax.mail.Message.RecipientType.TO,  javax.mail.Message.RecipientType.CC,javax.mail.Message.RecipientType.BCC.

      message.setSubject(subject);

      Multipart multipart = new MimeMultipart();

      BodyPart mbp = new MimeBodyPart();

      mbp.setContent(content, "text/html");

      multipart.addBodyPart(mbp);

      DataSource source =
            new FileDataSource (file location);

      try{
          mbp = new MimeBodyPart();
          mbp.setDataHandler(new DataHandler(source));
          mbp.setFileName(source.getName());

          multipart.addBodyPart(mbp);
        }
        catch(MessagingException me){
          //handle it
        }
      }

      try{
        message.setContent(multipart);
      }
      catch(MessagingException me){
        //handle it
      }

       try{
        Transport.send(message);
      }
}
}

Cheers
>>prop.put(Constantes.MAIL_SMTP_HOST,host);

Why would you do that for every thread?
>> prop.put(Constantes.MAIL_TRANSPORT_PROTOCOL, Constantes.SMTP);
>>      prop.put(Constantes.MAIL_SMTP_HOST,host);
>>      prop.put(Constantes.MAIL_SMTP_PORT,  (PORT);

That can be done globally, just once.
jagadeesh_motamarri, is that the only answer you want? It puts the properties everytime the thread runs which is not required.
.....i just reviewed the code and without working on or having a second thought just gave the points. But later i realized that mayankeagle's solution would be atleast near to what i expected.? Suggest me on this please.

I would have done this before you questioned me...!!
i would prefer giving point to mayankeagle....
Then please close it :)