user1548560
user1548560

Reputation: 101

i want to send mail using java with out delaying

I am using javaMail API for Sending mails my code is working fine but the Problem is that while sending mail it is taking time to send mails(delaying time is nearly 15 to 20 seconds), this is the reason my application is going down.I want to send mails with out taking any time while sending mails .Please give an idea

here is my code:

public class sendMail {

public static void main(String[] args) {
    Properties props = new Properties();
    props=System.getProperties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "587");

    String mail="[email protected]";


    Session session = Session.getDefaultInstance(props,
        new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("[email protected]","********");
            }
        });

    try {
    String emails="[email protected]"+","+"[email protected]";
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(emails));
        message.setSubject("Testing Subject");
        message.setText("Dear Rejender," +
                "\n\n Please find the like!");

    //Transport.send(message);
        Transport tr=session.getTransport("smtp");
        //tr.sendMessage(message, message.getRecipients(message.));
        tr.send(message);
        tr.close();
//Transport         

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

Upvotes: 2

Views: 3048

Answers (2)

pap
pap

Reputation: 27614

Sending email does not offer any guarantee or expectation regarding delivery-time. The JavaMail API does not impose or add any delays to the process, it immediately sends the email to the SMTP server indicated. How long it takes from there to the email having been relayed to the actual recipient's mailbox is out of your control. Depending on circumstances, the message may have to pass several intermediary servers and may or may not be processed at several points during the transit (virus-checking etc).

If you have specific requirements that your message has to be delivered within a certain time, email is a very bad choice of transport. My advice, rethink your architecture to either accept a variable, non-deterministic delivery-time or look into possible using some other messaging method that offers synchronous communication.

Upvotes: 2

assylias
assylias

Reputation: 328598

You could run the method that takes too much time in its own thread, allowing your main program to continue doing other things:

new Thread(new Runnable() {
    public void run() {
        tr.send(message);
        tr.close();
    }
}).start();

ps: you need to make tr and message final and you need to add some error handling in the run method.

Upvotes: 3

Related Questions