« Return to Thread: How to send smtp mail

Re: How to send smtp mail

by david Beer :: Rate this Message:

Reply to Author | View in Thread

Some parts of this message have been removed. Learn more about Nabble's security policy.
Hi

You may find the following link useful. It describes how you can send email using java http://www.builderau.com.au/program/java/soa/Sending-e-mail-in-Java-There-s-more-than-one-way/0,339024620,339222642,00.htm

Most likely your issue is that your smtp will not allow you to send un authorised, the web page does give an example on how to authentication when sending an email.

Thanks

David

----- Original Message ----
From: Augustine Enang <Enang.Augustine@...>
To: nbusers@...
Sent: Wednesday, 9 April, 2008 9:43:33 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

 

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