« Return to Thread: How to send smtp mail

Re: How to send smtp mail

by Johnny Kewl :: Rate this Message:

Reply to Author | View in Thread

Some parts of this message have been removed. Learn more about Nabble's security policy.
 
---------------------------------------------------------------------------
HARBOR: http://coolharbor.100free.com/index.htm
The most powerful application server on earth.
The only real POJO Application Server.
Making the Java dream come true.
---------------------------------------------------------------------------
----- Original Message -----
From: Enang.Augustine@...
To: nbusers@...
Sent: Wednesday, April 09, 2008 10:43 AM
Subject: [nbusers] How to send smtp mail

Hi,

Good day,

Am a netbeans fan and have been using it since 2005

 

Current am working on a project that enable users log issues and to review all logged issues. The application is also meant to send email to the administrator whenever an issue is logged.

I encountered an error trying to send an email to the administrator. My mail server required authentication and itÂ’s on a domain

The error I got is : cannot connect to smtp sever below is the code that should send the email

 

 

============

Hi there Augustine,

 

Code doesnt look to bad, just have a few thoughts...

+ Sending a mail normally doesnt need a password

+ You getting a REFUSED connection which means the server is throwing it out.

SMTP servers because of SPAM are getting pretty smart, so if you testing on your SMTP address and then running say the banks FROM domain they so throw it out. So test it to begin with with the exact same stuff that you would put into outlook express, or the mail client you using.

+ VWP is not my favorite tool, so I'm not too familiar with it, but I think the next problem you going to have is that the WEB page sticks, freezes while the email goes, so you need a thread somewhere... if you look at the http://coolharbor.100free.com/index.htm link, you'l find a mailer, just steal that code and play.

The web page will continue immediately and the mail goes in the background... nice use experience etc.

 

Good luck

 

 

 

CODE

 

String to = "oteniya.olumide@...";     // to address

        String from = "enang.augustine@..."; // from address

        String emailSubject = theSubject;  // the subject line

        String emailMessage = theMessage;                  // the body of the message

        String mailhost = "172.16.10.132"; // SMTP server

        String user = "acb";                 // user ID

        String password = "password";              // password

                                // password

        boolean auth = true;

        boolean ssl = false;

        Properties props = System.getProperties();

       

        if (mailhost != null) {

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

        }

        if (auth) {

            props.put("mail.smtp.auth", "true");

        }

        // Get a Session object

        javax.mail.Session session = javax.mail.Session.getInstance(props, null);

  

        // Construct the message

        javax.mail.Message msg = new MimeMessage(session);

       

        try {

            // Set message details

            msg.setFrom(new InternetAddress(from));

            //msg.setRecipient(javax.mail.Message.RecipientType.TO, arg1)

            msg.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));

            msg.setSubject(emailSubject);

            msg.setSentDate(new Date());

            msg.setText(emailMessage);

           

            // Send the thing off

            SMTPTransport t = (SMTPTransport)session.getTransport(ssl ? "smtps" : "smtp");

            try {

                if (auth) {

                    t.connect(mailhost, user, password);

                } else {

                    t.connect();

                }

                t.sendMessage(msg, msg.getAllRecipients());

            } finally {

                t.close();

            }

            log("Mail was sent successfully.");

        } catch (Exception e) {

            if (e instanceof SendFailedException) {

                MessagingException sfe = (MessagingException)e;

                if (sfe instanceof SMTPSendFailedException) {

                    SMTPSendFailedException ssfe = (SMTPSendFailedException)sfe;

                    log("Smtp_Send_Failed:");

                }

                Exception ne;

                                                                while ((ne = sfe.getNextException()) != null && ne instanceof MessagingException) {

                    sfe = (MessagingException)ne;

                    if (sfe instanceof SMTPAddressFailedException) {

                        SMTPAddressFailedException ssfe = (SMTPAddressFailedException)sfe;

                        log("Address failed:");

                        log(ssfe.toString());

                        log("  Address: " + ssfe.getAddress());

                        log("  Command: " + ssfe.getCommand());

                        log("  Return Code: " + ssfe.getReturnCode());

                        log("  Response: " + ssfe.getMessage());

                    } else if (sfe instanceof SMTPAddressSucceededException) {

                        log("Address succeeded:");

                        SMTPAddressSucceededException ssfe = (SMTPAddressSucceededException)sfe;

                    }

                                                                }

                    } else {

                                                log("Got Exception : " + e);           

        }

 

THIS IS THE ERROR I GOT

 

Initializing Sun's JavaServer Faces implementation (1.2_04-b20-p03) for context '/querylogview'

PWC1412: WebModule[/querylogview] ServletContext.log():Got Exception : javax.mail.MessagingException: Could not connect to SMTP host: 172.16.10.132, port: 25;

  nested exception is:

        java.net.ConnectException: Connection refused: connect

 

I am working with netbeans 6.1  and the framework is

visual web java server faces

 

 

 

cid:image003.jpg@01C87AFA.EFA50080 

Systems Development Unit

Information Technology Group

Intercontinental Bank Plc Nigeria

Plot 999c, Danmole street, Victoria Island, Lagos|

Phone +234 1 2773300|Cell +234 8059342751 

 


 « Return to Thread: How to send smtp mail