How send e-mail with java(e-mail sending application)

Today I'm going to send a e-mail using java application. In here I'm using G mail SMTP(Simple Mail Transfer Protocol ) server.

First you have to download JavaMail API. Using following link you can download it.

JavaMail API1.4.5

Download it and extract it .

Open your last NetBeans Java project.

Now you have to add you downloaded JAR file to your project.

To do that  Right click on your Project Libraries and click "Add JAR/Folder..."


Go to your extracted folder.


Select mail.jar and click "Open".

Next again go to "Add JAR/Folder" and go to folder "bil" select all jar file their and "Open".


Now adding libraries part is over .

Go to your form and add a button . I name it as "Send Mail". I change it variable name as "btnMail".

OK.

Now it is cording part..

First you have to import following 

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.JOptionPane;

Double click on your button and type this codes..

        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.SSLSocket");
        props.put("mail.smtl.auth", "true");
        props.put("mail.smtp.port", "465");
        
        Session ssn= Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication(){
                return new PasswordAuthentication("myemail@gmail.com", "password");
                
                
                }
                }
                
                
                
                );
        try{
            Message msg=new MimeMessage(ssn);
            msg.setFrom(new InternetAddress("myemail@gmail.com"));
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("youremail@gmail.com"));
            msg.setSubject("subject");
            msg.setText("Mail Text");
            Transport.send(msg);
            
            JOptionPane.showMessageDialog(null, "sent");
        }
        catch(Exception e){JOptionPane.showMessageDialog(null, e);}



In hear
  • myemail@gmail.com is your e mail adress
  • password is your password
  • youremail@gmail.com is receiver's e mail adress.
  • Subject is email sibject
  • Mail text is message
You change them own ...

here you can use only g mail address only. Because we here we use G Mail SMTP server.

If you have any problem please leave a comment..









1 comment:

  1. thanks for the blog,,but however,i got this error after running the code,,"com.sun.mail.smtp.SMTPSendFailed:530 5.7.0 Must issue a STARTLS command first.f132sm3705556wmf.38 -gsmtp"; kindly help

    ReplyDelete