Вы находитесь на странице: 1из 7
Java Mail using Swing Introduction: In this I will discuss to develop Desktop Application to

Java Mail using Swing

Introduction:

In this I will discuss to develop Desktop Application to send email using Java. This application can be used for send email to any email id for e.g. gmail.com, hotmail.com, yahoo.com and many more.

Jdk1.5.0id for e.g. gmail.com, hotmail.com, yahoo.com and many more. Net Beans 5.0/5.5.1 / Sun Java Studio

Net Beans 5.0/5.5.1 / Sun Java Studio Enterprise 8gmail.com, hotmail.com, yahoo.com and many more. Jdk1.5.0 Javamail-1.3.3 Jaf-1.1 Definition: Java Mail It is a

Javamail-1.3.3Jdk1.5.0 Net Beans 5.0/5.5.1 / Sun Java Studio Enterprise 8 Jaf-1.1 Definition: Java Mail It is

Jaf-1.15.0/5.5.1 / Sun Java Studio Enterprise 8 Javamail-1.3.3 Definition: Java Mail It is a messaging service,

Definition:

Java Mail

It is a messaging service, which used to send and receive messages over the

network and the Internet. It handles several message formats, such as plain text, message with attachments and others. It uses Post Office Protocol (POP) / Internet Message Access Protocol (IMAP) to receive message and Simple Mail Transfer Protocol (SMTP) to send message.

Java Mail API: - It is set of classes and interface, which provides a platform and protocol independent framework to build mail and messaging applications. The JavaMail API is available as an optional package for use with Java SE platform and is also included in the Java EE platform.

Following are the important class and interface which is used to send and receive mail.

javax.mail.internet.MimeMessage

- This class is used to create email message in MIME style

javax.mail.Flags.Flag

- This class represents system flag such as message is deleted, answered, drafted, recent, flagged, and seen.

javax.mail.internet.InternetAddress

- This class represents valid internet email address for e.g. abc@gmail.com

javax.mail.Session

- This is final class and represents a mail session to send/receive email

javax.mail.Store

- An abstract class which represents a message store and its access protocol, for storing and retrieving messages.

javax.mail.Folder

- An abstract class that represents a folder for mail messages for e.g. INBOX, DRAFT.

javax.mail.Transport

- An abstract class that models a message transport, and used to send message.

Java Mail using Swing Article Code: ACE-08025

By Rahul Kumar Page 1 of 7

Developing Java Mail Desktop Application in NetBeans using Swing: File -> New Project -> Categories

Developing Java Mail Desktop Application in NetBeans using Swing:

Java Mail Desktop Application in NetBeans using Swing: File -> New Project -> Categories : General
Java Mail Desktop Application in NetBeans using Swing: File -> New Project -> Categories : General

File -> New Project -> Categories : General -> Projects : Java ApplicationJava Mail Desktop Application in NetBeans using Swing: Click on Next -> Project Name: MyMailApp ->

Click on Next -> Project Name: MyMailApp -> Select project location -> Click on Finish. .

-> Select project location -> Click on Finish . Download the javamail-1.3.3 from

Unzip this file in local drive.Click on Tools Menu -> Click on Library Manager Click on

Click on Tools Menu -> Click on Library ManagerUnzip this file in local drive. Click on New Library -> Library Name: JavaMail ->

Click on New Library -> Library Name: JavaMail -> Click on Ok. .

Library -> Library Name: JavaMail -> Click on Ok . Java Mail using Swing Article Code:

Java Mail using Swing Article Code: ACE-08025

By Rahul Kumar Page 2 of 7

Click on Add JAR/Folder -> Select mail.jar file from javamail-1.3.3 folder Click on Add JAR/Folder

Click on Add JAR/Folder -> Select mail.jar file from javamail-1.3.3 folderClick on Add JAR/Folder Button Same way create one more library named JAF and add

Click on Add JAR/Folder Button-> Select mail.jar file from javamail-1.3.3 folder Same way create one more library named JAF and

Same way create one more library named JAF and add activiation.jar file into it.from javamail-1.3.3 folder Click on Add JAR/Folder Button Right click on Libraries of MyMailApp -> Add

more library named JAF and add activiation.jar file into it. Right click on Libraries of MyMailApp

Right click on Libraries of MyMailApp -> Add Librarymore library named JAF and add activiation.jar file into it. Click on Add Library -> Same

Click on Add Library -> Same way add JAF library to the project.it. Right click on Libraries of MyMailApp -> Add Library -> Select JavaMail Right click on

-> Select JavaMail

way add JAF library to the project. -> Select JavaMail Right click on Source Package of

Right click on Source Package of Project -> New -> Click on Java Classway add JAF library to the project. -> Select JavaMail Class Name: MyAuth -> Package Name:

Class Name: MyAuth -> Package Name: mymailapp -> Click on Finish ButtonPackage of Project -> New -> Click on Java Class Replace MyAuth.java with following code package

Replace MyAuth.java with following code-> Package Name: mymailapp -> Click on Finish Button package mymailapp; import javax.mail.Authenticator; import

package mymailapp;

import javax.mail.Authenticator; import javax.mail.PasswordAuthentication;

public class MyAuth extends Authenticator

{

private String emailId, password;

public MyAuth(String emailId, String password)

{

this.emailId = emailId; this.password = password;

}

public PasswordAuthentication getPasswordAuthentication()

{

return new PasswordAuthentication(emailId,password);

}

}

return new PasswordAuthentication(emailId,password); } } Right click on Project -> New -> Click on JFrame

Right click on Project -> New -> Click on JFrame Form.return new PasswordAuthentication(emailId,password); } } Class Name: JavaMailFrame -> package : mymailapp ->

Class Name: JavaMailFrame -> package : mymailapp -> Click on Finish Buttonclick on Project -> New -> Click on JFrame Form. Drag the following components from Palate

Drag the following components from Palate and change the label and change the variable name (By right click on Component and Click on Change Variable Name).-> package : mymailapp -> Click on Finish Button Java Mail using Swing Article Code: ACE-08025

Java Mail using Swing Article Code: ACE-08025

By Rahul Kumar Page 3 of 7

Drag jOptionPane1 in outside of Frame, it will visible in Inspector Window Components Text Property

Drag jOptionPane1 in outside of Frame, it will visible in Inspector WindowComponents Text Property Variable Name jLabel1 My Mail Program lblMessage jLabel2 Email ID: [From]

Components

Text Property

Variable Name

jLabel1

My Mail Program

lblMessage

jLabel2

Email ID: [From]

lblEmailId

jLabel3

Password

lblPassword

jLabel4

To:

lblTo

jLabel5

Subject

lblSubject

jTextField1

 

txtEmailId

jPasswordField1

 

txtPassword

jTextField2

 

txtTo

jTextField3

 

txtSubject

jTextArea1

 

txaMsg

jButton1

Send Mail

btnSend

jOptionPane1

 

dlgEmail

Now your Frame should look below.Send Mail btnSend jOptionPane1   dlgEmail Import following package in Source Code of Frame import

  dlgEmail Now your Frame should look below. Import following package in Source Code of Frame

Import following package in Source Code of Frame  dlgEmail Now your Frame should look below. import java.util.Date; import javax.mail.Message; import

import java.util.Date; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage;

Type the below code in Action Event of btnSendimport javax.mail.internet.MimeMessage; Java Mail using Swing Article Code: ACE-08025 By Rahul Kumar

Java Mail using Swing Article Code: ACE-08025

By Rahul Kumar Page 4 of 7

String to = txtTo.getText(); String from = txtEmailId.getText(); // Smtp Address of Mail Server here

String to = txtTo.getText(); String from = txtEmailId.getText();

// Smtp Address of Mail Server here I am using Gmail String host = "smtp.gmail.com";

//Properties file

java.util.Properties props = System.getProperties();

contains host details

props.put("mail.smtp.user", from); props.put("mail.smtp.host", host); props.put("mail.smtp.port", "587"); props.put("mail.smtp.starttls.enable","true"); props.put("mail.smtp.auth", "true"); props.put("mail.debug", "true");

String pwd = new String(txtPassword.getPassword()); Session session = Session.getInstance(props, new mymailapp.MyAuth(from,pwd)); try

{

// Instantiatee a message Message msg = new MimeMessage(session);

//Set message attributes msg.setFrom(new InternetAddress(from)); InternetAddress[] address = {new InternetAddress(to)}; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject(txtSubject.getText()); msg.setSentDate(new Date());

// Set message content msg.setText(txaMsg.getText());

//Send the message Transport.send(msg);

dlgEmail.showMessageDialog(this,"Message has been successfully send to " + to,"Mail",javax.swing.JOptionPane.INFORMATION_MESSAGE);

System.out.println("Message has been successfully send to " +

to);

}

catch(Exception ee)

{

System.out.println("Error : " + ee.toString()); dlgEmail.showMessageDialog(this,"Error : " + ee.toString(),"Mail",javax.swing.JOptionPane.ERROR_MESSAGE);

}

Java Mail using Swing Article Code: ACE-08025

By Rahul Kumar Page 5 of 7

Right Click on Project and Click on Build Project Right Click on JavaMailFrame and Click
Right Click on Project and Click on Build Project Right Click on JavaMailFrame and Click

Right Click on Project and Click on Build ProjectRight Click on JavaMailFrame and Click on Run File Enter the Email Id [From]: your

Right Click on JavaMailFrame and Click on Run FileRight Click on Project and Click on Build Project Enter the Email Id [From]: your gmail

Enter the Email Id [From]: your gmail id (Because this application is configured using gmail)Project Right Click on JavaMailFrame and Click on Run File Type the Valid password of your

Type the Valid password of your gmail account -> Type Email id inid (Because this application is configured using gmail) To Textbox, to whom you want to send

To

Textbox, to whom

you want to send email (It can be any email id for eg. abc@hotmail.com, abc@yahoo.com ).

Enter the Subject and Message in text Area-> Click on Send Mail Buttonany email id for eg. abc@hotmail.com , abc@yahoo.com ). Following Message will display if Email id

and Message in text Area-> Click on Send Mail Button Following Message will display if Email

Following Message will display if Email id and Password are valid.and Message in text Area-> Click on Send Mail Button If you entered Invalid email id

Message will display if Email id and Password are valid. If you entered Invalid email id

If you entered Invalid email id or password following error message will display.Message will display if Email id and Password are valid. Java Mail using Swing Article Code:

Java Mail using Swing Article Code: ACE-08025

By Rahul Kumar Page 6 of 7

Now this email is received by user rahulk252. Conclusion: In this I explained to JavaMail
Now this email is received by user rahulk252. Conclusion: In this I explained to JavaMail
Now this email is received by user rahulk252. Conclusion: In this I explained to JavaMail

Now this email is received by user rahulk252.

Now this email is received by user rahulk252. Conclusion: In this I explained to JavaMail API

Conclusion:

In this I explained to JavaMail API for sending email. In this application I have not developed own email server, but I used SMTP server of Gmail (Because it s Free!!!). Using same SMTP we can develop application which will send email with attachment and in different format.

Java Mail using Swing Article Code: ACE-08025

By Rahul Kumar Page 7 of 7