email sendig

View: New views
3 Messages — Rating Filter:   Alert me  

email sendig

by puntapari :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi to everybody!

After reading in forums, i have made this code but it makes me an error: SMTP SEND FAILED



 public String button1_action() {
        // TODO: Process the button click action. Return value is a navigation
        // case name where null will return to the same page.
        // Send Email
// This clip can be used in a button action
// method to send an e-mail via SMTP
// (if no authentication is required from the mailserver)
// TODO: Create a Text Area component on the page and
// a Button, place this code in the Button's action method
// TODO: set the from, to addresses and the mail server
        String to = "xxxxx@gmail.com";
        // from address
        String from = "xxxxx@gmail.com"; // to address
        String subject = "mailcom";   // please note that if your mail server requires authentication,
        String message = textArea1.getText().toString();
        String mailhost = "smtp.gmail.com"; // this code may not work
        String user = "user";                 // TODO: set the subject line
        String password = "password";              // TODO: set the body of the message
// in the case of an exception, print a message to the output log
        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
       
        Session s = Session.getInstance(props,null);
// construct the message
        Message msg = new MimeMessage(s);
       
        try {
//  Set message details
            msg.setFrom(new InternetAddress(from));
            msg.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
            msg.setSubject(subject);
//            msg.setSentDate(new Date());
            msg.setText(message);
           
// send the thing off
            //SMTPTransport t =(SMTPTransport)session.getTransport(ssl ? "smtps" : "smtp");
           
            SMTPTransport t =(SMTPTransport)s.getTransport("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.");
            info("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:");
                    info("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("  RetCode: " + ssfe.getReturnCode());
                        log("  Response: " + ssfe.getMessage());
                    } else if (sfe instanceof SMTPAddressSucceededException) {
                        log("ADDRESS SUCCEEDED:");
                        SMTPAddressSucceededException ssfe =
                                (SMTPAddressSucceededException)sfe;
                    }
                }
            } else {
                log("Got Exception : " + e);
                info("Got Exception : " + e);
            }
        }
        return null;
    }


If anyone could help me.....

Many thanks!!!

Re: email sendig

by Rick Fincher :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Gmail requires SSL, see the following for help:
http://www.velocityreviews.com/forums/t141237-send-smtp-mail-using-javamail-with-gmail-account.html

Rick

puntapari wrote:

> Hi to everybody!
>
> After reading in forums, i have made this code but it makes me an error:
> SMTP SEND FAILED
>
>
>
>  public String button1_action() {
>         // TODO: Process the button click action. Return value is a
> navigation
>         // case name where null will return to the same page.
>         // Send Email
> // This clip can be used in a button action
> // method to send an e-mail via SMTP
> // (if no authentication is required from the mailserver)
> // TODO: Create a Text Area component on the page and
> // a Button, place this code in the Button's action method
> // TODO: set the from, to addresses and the mail server
>         String to = "xxxxx@...";
>         // from address
>         String from = "xxxxx@..."; // to address
>         String subject = "mailcom";   // please note that if your mail
> server requires authentication,
>         String message = textArea1.getText().toString();
>         String mailhost = "smtp.gmail.com"; // this code may not work
>         String user = "user";                 // TODO: set the subject line
>         String password = "password";              // TODO: set the body of
> the message
> // in the case of an exception, print a message to the output log
>         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
>        
>         Session s = Session.getInstance(props,null);
> // construct the message
>         Message msg = new MimeMessage(s);
>        
>         try {
> //  Set message details
>             msg.setFrom(new InternetAddress(from));
>             msg.setRecipient(javax.mail.Message.RecipientType.TO, new
> InternetAddress(to));
>             msg.setSubject(subject);
> //            msg.setSentDate(new Date());
>             msg.setText(message);
>            
> // send the thing off
>             //SMTPTransport t =(SMTPTransport)session.getTransport(ssl ?
> "smtps" : "smtp");
>            
>             SMTPTransport t =(SMTPTransport)s.getTransport("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.");
>             info("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:");
>                     info("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("  RetCode: " + ssfe.getReturnCode());
>                         log("  Response: " + ssfe.getMessage());
>                     } else if (sfe instanceof SMTPAddressSucceededException)
> {
>                         log("ADDRESS SUCCEEDED:");
>                         SMTPAddressSucceededException ssfe =
>                                 (SMTPAddressSucceededException)sfe;
>                     }
>                 }
>             } else {
>                 log("Got Exception : " + e);
>                 info("Got Exception : " + e);
>             }
>         }
>         return null;
>     }
>
> If anyone could help me.....
>
> Many thanks!!!
>  


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: email sendig

by puntapari :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi!

Many thanks. I have tried sending to a gmail account and a hotmail account and it works.
Now, i have another question related. What should i do if i want to send an email in a particular day. I have thought doing with listeners but i don“t know how. If anyone knows...please tell me, many thanks.

This is the code of sending mails which works:

String  d_email = "xxxxx@gmail.com",
            d_password = "xxxxxx",
            d_host = "smtp.gmail.com",
            d_port  = "465",
            m_to = "xxxxx@gmail.com",
            m_subject = "Testing";
           
           
    public String button1_action() {
       
       
         String m_text = textArea1.getValue().toString();
        Properties props = new Properties();
        props.put("mail.smtp.user", d_email);
        props.put("mail.smtp.host", d_host);
        props.put("mail.smtp.port", d_port);
        props.put("mail.smtp.starttls.enable","true");
        props.put("mail.smtp.auth", "true");
        //props.put("mail.smtp.debug", "true");
        props.put("mail.smtp.socketFactory.port", d_port);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
       
        SecurityManager security = System.getSecurityManager();
       
        try {
            Authenticator auth = new SMTPAuthenticator();
            Session session = Session.getInstance(props, auth);
            //session.setDebug(true);
           
            MimeMessage msg = new MimeMessage(session);
            msg.setText(m_text);
            msg.setSubject(m_subject);
            msg.setFrom(new InternetAddress(d_email));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
            Transport.send(msg);
        } catch (Exception mex) {
            mex.printStackTrace();
        }
        return null;
       
       
    }
    private class SMTPAuthenticator extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(d_email, d_password);
        }
    }