Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Improving performance on transport.send()

843830Feb 23 2006 — edited Feb 26 2006
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..............................");
}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 26 2006
Added on Feb 23 2006
5 comments
1,075 views