开发者

java mail keeping Transport object connected

How do i keep the java mail transport object alive or connected.

I have written this in my code in a simple class file inside a web application : -

@Resource(name = "myMailServer")
    private Session mailSession;

    Transport transport ;

public boolean sendMail(String recipient, String subject, String text) {
    boolean exe = false;

    Properties p = new Properties();

    String username = "someone@gmail.com";
    String password = "password";

    InitialContext c = null;

    try
    {
         c = new InitialContext();
          mailSession = (javax.mail.Session) c.lookup("java:comp/env/myMailServer");
    }
    catch(NamingException ne)
    {
        ne.printStackTrace();
    }

    try
    {
        Message msg = new MimeMessage(mailSession);
        msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(recipient, false));
        msg.setSubject(subject);
        msg.setText(text);
        msg.setHeader("MIME-Version" , "1.0" );
        msg.setHeader("Content-Type" , "text/html" );
        msg.setHeader("X-Mailer", "Recommend-It Mailer V2.03c02");
        msg.saveChanges();

        //Transport.send(msg);
        if(transport == null) {
            transport = mailSession.getTransport("smtps");
            System.out.println("" + transport.isConnected());
            if(!transport.isConnected()) {
                transport.connect(username, password);
            }
        }

        transport.sendMessage(msg, msg.getAllRecipients());
        exe = true;
    }
    catch (AddressException e)
    {
        e.printStackTrace();
        exe = false;
    }
    catch (MessagingException e)
    {
        e.printStackTrace();
        exe = false;
    }
    finally {
        /*try {
            if(transport != null)
                transport.close();
        }
        catch(MessagingException me) {
            me.printStackTrace();
        }
        catch(Exception e) {
            e.printStackTrace();
        }*/
    }

    return exe;
}

the full code here

Now everytime i run this code it takes some time to connect with the mail server

and the line

System.out.println("" + transport.isConnected());

开发者_如何学Pythonprints a false

How do i retain the object transport as it does gets null and into the block

if(transport == null) {

or the transport object remains connected...

Thanks

Pradyut


the code should be....
with a static initialization of transport object
without any problems
but can be good with a function static Transport getTransport() method

@Resource(name = "myMailServer")
    private Session mailSession;

    static Transport transport ;

    public boolean sendMail(String recipient, String subject, String text) {
    boolean exe = false;

    Properties p = new Properties();

    String username = "someone@gmail.com";
    String password = "password";

    InitialContext c = null;

    try
    {
         c = new InitialContext();
          mailSession = (javax.mail.Session) c.lookup("java:comp/env/myMailServer");
    }
    catch(NamingException ne)
    {
        ne.printStackTrace();
    }

    try
    {
        Message msg = new MimeMessage(mailSession);
        msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(recipient, false));
        msg.setSubject(subject);
        msg.setText(text);
        msg.setHeader("MIME-Version" , "1.0" );
        msg.setHeader("Content-Type" , "text/html" );
        msg.setHeader("X-Mailer", "Recommend-It Mailer V2.03c02");
        msg.saveChanges();

        //Transport.send(msg);
        if(transport == null) {

            transport = mailSession.getTransport("smtps");


        }
        if(!transport.isConnected()) {

                transport.connect(username, password);
            }

        transport.sendMessage(msg, msg.getAllRecipients());
        exe = true;
    }
    catch (AddressException e)
    {
        e.printStackTrace();
        exe = false;
    }
    catch (MessagingException e)
    {
        e.printStackTrace();
        exe = false;
    }
    finally {
        /*try {
            if(transport != null)
                transport.close();
        }
        catch(MessagingException me) {
            me.printStackTrace();
        }
        catch(Exception e) {
            e.printStackTrace();
        }*/
    }

    return exe;
}

Thanks
Regards
Pradyut

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜