Improving performance on transport.send()

Hi all,

I'm hoping that someone can help. I've written a little java email program that happily "sends" emails with attachments.

However, i'm concerned about the "performance" of the transport.send(message) command.

If i send the same attachment to a friend via an email client, the email is send within 0.5 seconds. If I use my program the send command seems to take around 4-5 seconds. I'm currently sending my friend around 100 emails a day using this method, so 4-5 seconds is a BIG overhead. I've enclosed the code, and wondered if anyone could advise as to what I could do to improve it? or get rid of this overhead...... (I'm very new to Java though, so please feel free to be as explicit as you wish).

thanks

jake

package racing.reports;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Properties;

import javax.mail.*;

import javax.mail.internet.*;

import javax.activation.*;

public class Email {

public static void email(Connection connection, String subject, String filename, String from, String filetitle) throws MessagingException, SQLException, InterruptedException {

System.out.println("Start of Email program");

ResultSet rs = null;

System.out.println(filename);

Properties props = new Properties();

// fill props with any information

props.put("mail.smtp.host","mailhost.XXXXXXXX.XXXXXXX");

// mailhost XXX'd out

Session session = Session.getDefaultInstance(props, null);

Address toAddress =null;

MimeMessage message = new MimeMessage(session);

message.setSubject(subject);

Address address = new InternetAddress(from);

message.setFrom(address);

try {

Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

rs = stmt.executeQuery("select distinct var_value from parameters " +

"where type = 'email'");

} catch (SQLException e) {e.printStackTrace();}

while (rs.next()) {

String email = rs.getString("var_value");

toAddress = new InternetAddress(email);

message.addRecipient(Message.RecipientType.TO, toAddress);

}

MimeBodyPart messageBodyPart =

new MimeBodyPart();

Multipart multipart = new MimeMultipart();

messageBodyPart.setText("File Attached");

multipart.addBodyPart(messageBodyPart);

if (filename != "") {

// Part two is attachment

messageBodyPart = new MimeBodyPart();

DataSource source =

new FileDataSource(filename);

messageBodyPart.setDataHandler(

new DataHandler(source));

messageBodyPart.setFileName(filetitle);

multipart.addBodyPart(messageBodyPart);

}

// Put parts in message

message.setContent(multipart);

//Transport transport = session.getTransport();

System.out.println("MESSAGE ABOUT TO BE SENT..............................");

try {

Transport.send(message);

}

catch (MessagingException e) {

Thread.sleep(100);

System.out.println("Cannot print email:"+e);

try {

Transport.send(message);}

catch (MessagingException e2) {

e2.printStackTrace();

System.out.println("Cannot print email:"+e2);

}

}

//transport.close();

System.out.println("MESSAGE SENT..............................");

}

}

[3521 byte] By [jakedaya] at [2007-10-2 13:07:46]
# 1
The code you posted there actually generates the attachment as well as sending it. Comparing that to your e-mail client that just sends the attachment isn't a true comparison.
DrClapa at 2007-7-13 10:33:03 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

DrClap,

thanks kindly for the response.

Two points:-

The delay is between the printed line - "Message ready to be sent"

and "Message Sent" ? So it does seem to be the transport.send() command that is taking the time.

But the code shouldn't be generating the attachment either? Apologies for my naivety, but which lines actually "generate" the attachement, as this may be the problem. The attachement should is already created, and should simply be "stuck and sent?". Maybe the transport.send() is doing more work than it should?

Any help would be greatly appreciated.

jakedaya at 2007-7-13 10:33:03 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Sorry, your code doesn't create the attachment. I just saw some SQL stuff going on and assumed it was creating the attachment. The code's pretty unreadable in that format.

I'm going to guess that most of that 4 or 5 seconds is involved in setting up the connection to the server. Your e-mail client might have a permanent connection. I can say this because my code (plain ordinary JavaMail code like yours) sends several messages per second. But it caches and reuses the session, whereas yours generates a new session every time.

DrClapa at 2007-7-13 10:33:03 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

Sorry about the code... all the justification had gone when i pasted the code. (Should have removed the "excess", but was worried that I might remove the lines that actually caused the problem!

Would you mind pasting your code up for me? (or email me a copy? jake.day@zen.co.uk) - i'm still fairly new to java and most of the code i've built has simply been "borrowed" from other sources, so i'm not very adept yet at building my own code.

thanks greatly. i need to award you the duke dollars too...but no idea how to do that yet! will investigate.....

thanks again for your help.

jake

jakedaya at 2007-7-13 10:33:03 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

My code is pretty much like yours (I learned from the same sources you did, I'm sure, there isn't much out there). The only difference is that I don't do thisSession session = Session.getDefaultInstance(props, null);

for every message. It would be best if you refactored your code into some code of object-oriented mailer class, instead of just a method. The presence of both JDBC and e-mail code in that method is a sure sign of that.

DrClapa at 2007-7-13 10:33:03 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...