Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java sendmail() 2

Status
Not open for further replies.

jangochi

Programmer
Nov 20, 2003
4
US
HI,

I use java sendmail() method for sending mails inside a process. But recently I find that the method is not giving any return code for the main program. What's happening is, if the number of emails going out is fewer lesser than 100, there is no issue. But if the number is more, after sending about 100 mails, it's getting stuck and stays there, does not come out. There is a catch section in the code. But control is not going there. If I kill the process & restart it, then again another 100 mail are send and get stuck again(it stays in the sendmail() loop).

Any clues? What should I mainly check? I am basically not a java guy. Thanks in advance.

Manu
 
Do you mean you are using Java to shell out to the OS and run Sendmail (as in the UNIX/Linux native program) using the code :

Code:
 Process p = Runtime.getRuntime().exec("sendmail bla bla");

Or do you mean you are using the JavaMail API to send mail ?
 
Hi,
I am NOT using java to shell out to the OS and run Sendmail. I am using JavaMail API.

syntax: sendmail(emailid).

Thanks
Manu
 
I had once had a problem similar to this our mail server was MS Exchange Server and we were using javamail API too.

We diagonized the problem we found out that the mail server couldn't reply to all of the transactions so fast.That means the API sends the mails so fast.As you may have guesses we were sending the mails in a loop.

Then I tried to make process slower

...sendMail();
Thread.sleep(500);

that worked very well.No error and nearly %100 success in sending >1000 mails at once.

Hope it helps.

Salih Sipahi
Software Engineer.
City of Istanbul Turkey
s.sipahi@sahinlerholding.com.tr
turkey_clr.gif
 
Another appoach would be to have a Mass Mailer class to manage the mailing queue by implementing javax.mail.event.TransportListener. It listen TransportEvent, when messageDelivered event is fired, you know the mail server has consumed a Email and ready for next one. This way you can guarantee not to overloading/cashing the mail server.
 
Where in the JavaMail API is there a method called "sendMail"? One uses a "Transaction" to send mail in the JavaMail API and this is done via Transaction.send(Message,...) methods.

-mdiggory
 
The guy meant he was using some custom code built on the JavaMail API that he called with the signature "sendmail()".
 
Hi Guys, thanks for all the suggestions. I added a sleep time(various values) & that doesn't seem to have made much of a difference. I am not sure as to what exactly is causing the slowness. I am sure that it's slowing once inside the sendmail() because I have added a print statement just before that I could see in the log file that it's getting slower and slower at this point after each set of mailing.

Byam, I am not sure as to how to implement the approach suggested by you. Can you elaborate a bit?
 
Here is the code I quickly come up with. I couldn't test it as I don't have a mail server running. If the code does work out, hope it still gives you some idea.


Code:
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.event.TransportEvent;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MassMailer implements javax.mail.event.TransportListener {

   private Transport transport; 
   private mailHost = "localhost";  // change it to your mail host
   Session mailSession;
  
   public MassMailer() throws NoSuchProviderException {

	  Properties mailProps = new Properties();
	  mailProps.setProperty( "mail.transport.protocol", "smtp" );
	  mailProps.setProperty("mail.host", mailHost );
	  mailProps.setProperty("mail.smtp.host", mailHost);

      Session session = Session.getDefaultInstance(mailProps, null);
      transport = session.getTransport("smtp");
      transport.addTransportListener(this);
   }

   public synchronized void send(InternetAddress sender, 
                                 InternetAddress[] recipents, 
                                 String subject,
                                 String HTMLBody, 
                                 String PlainTextBody)
            throws NoSuchProviderException, InterruptedException,
                   MessagingException, UnsupportedEncodingException {

      transport.connect();
 
	  Message msg = new MimeMessage(mailSession);
	  msg.setFrom(sender);
	  msg.setSubject( subject );
	  msg.setSentDate(new Date());

	  MimeMultipart content = new MimeMultipart("alternative");
	  MimeBodyPart plainText = new MimeBodyPart();
	  plainText.setText( PlainTextBody );
	  content.addBodyPart( plainText );
	  MimeBodyPart html = new MimeBodyPart();
	  html.setContent( HTMLBody, "text/html; charset=iso-8859-1");
	  content.addBodyPart( html );
	  msg.saveChanges();
	
      for (int i=0; i<recipents.length; i++) {
         try {
			transport.sendMessage(msg, new InternetAddress[] {recipents[i]});
         }  catch (javax.mail.internet.AddressException ex) {
         	ex.printStackTrace();
         } catch (javax.mail.SendFailedException ex) {
			ex.printStackTrace();
         }
		this.wait();
      }
   }

   public synchronized void messageDelivered(TransportEvent e) {
   	  notify();
   }

   public synchronized void messageNotDelivered(TransportEvent e) {
      notify();
   }

   public  synchronized  void messagePartiallyDelivered(TransportEvent e) {
      notify();
   }

}

Usuage example:
Code:
InternetAddress sender = new InternetAddress(&quot;email@yourhost.com&quot;);
InternetAddress[] recpients = {
                               new InternetAddress(&quot;a@yourhost.com&quot;),
                               new InternetAddress(&quot;b@yourhost.com&quot;),
                               new InternetAddress(&quot;c@yourhost.com&quot;)
                              };
String planText = &quot;plan text&quot;;
String html = &quot;<html><body>plan text</body></html>&quot;;
String subject = &quot;test&quot;;
MassMailer mailer = new MassMailer();
mailer.send(sender, recipents, subject,html, plainText);

PS: the code only support one instance of MassMailer running at any one time, otherwise the MassMailers will listen tranport event from other MassMailer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top