Wednesday 13 February 2013

Send Email from your Java Project



private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                      
        if(sub.getText().isEmpty()){
        error.setText("Select Subject");
        }else if(input.getText().isEmpty()){
        error.setText("Select Link");
        }else if(email.getText().isEmpty()){
        error.setText("Select Email From Contacts");
        }else{
        Properties props=new Properties();
        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", "465");
     
        Session session=Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication(){
                    return new PasswordAuthentication("your_email", "your_password");
                }
                }
                );
        try{
            Message message=new MimeMessage(session);
            message.setFrom(new InternetAddress("sender_email"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email.getText()));//recipient email
            message.setSubject(sub.getText());
            message.setText(input.getText());
            Transport.send(message);
            JOptionPane.showMessageDialog(null,"Sucessfully send" );
        }catch(Exception e){
          JOptionPane.showMessageDialog(null,e.getMessage() );
        }
        }
    }

No comments:

Post a Comment