Sie sind auf Seite 1von 3

Deleting email using java mail api

As we send, forward and receive the emails, we can delete it too. The setFlag method of
Message class is used to delete a particular message.
For better understanding of this example, learn the steps of sending email using JavaMail
API first.
For receiving or sending the email using JavaMail API, ou need to load the two !ar files"
mail.jar
activation.jar
download these !ar files #or$ go to the %racle site to download the latest version.
Steps for deleting the email using JavaMail API
There are total & steps for deleting the email. The are"
'. Get the session object
(. create the store object and connect to the current host
). create the folder object and open it
*. Get the message to delete
&. delete the message using setFlag method
!ample of deleting email using JavaMail API
'. import com.sun.mail.imap.protocol.F+A,-.
(. import !ava.io./.
). import !ava.util./.
*. import !avax.mail./.
&. import !avax.mail.internet./.
0.
1. public class 2eleteMail 3
4.
5. public static void main#-tring args67$ throws 8xception 3
'9.
''. -tring user: ;sonoo!aiswal<!avatpoint.com;.==change accordingl
'(. -tring password:;xxxxx;.==change accordingl
').
'*. =='$ get the session ob!ect
'&. Properties properties : -stem.getProperties#$.
'0. -ession session : -ession.get2efaultInstance#properties$.
'1.
'4. ==($ create the store ob!ect and connect to the current host
'5. -tore store : session.get-tore#;pop);$.
(9. store.connect#;mail.!avatpoint.com;,user,password$.
('.
((. ==)$ create the folder ob!ect and open it
(). Folder folder : store.getFolder#;inbox;$.
(*.
(&. if #>folder.exists#$$ 3
(0. -stem.out.println#;inbox not found;$.
(1. -stem.exit#9$.
(4. ?
(5.
)9. folder.open#Folder.@8A2AB@IT8$.
)'.
)(. ==*$ ,et the message to delete
)). Message67 msg : folder.getMessages#$.
)*.
)&. ==-stem.out.println##messages.lengthC'$C; message found;$.
)0. for #int i : 9. i D msg.length. iCC$ 3
)1. -stem.out.println#;EEEEEEEEE ; C #i C '$ C ;EEEEEEEEEEEE;$.
)4. -tring from : InternetAddress.to-tring#msg6i7.getFrom#$$.
)5.
*9. if #from >: null$ 3
*'. -stem.out.println#;From" ; C from$.
*(. ?
*).
**. -tring replTo : InternetAddress.to-tring#
*&. msg6i7.get@eplTo#$$.
*0. if #replTo >: null$ 3
*1. -stem.out.println#;@eplEto" ; C replTo$.
*4. ?
*5.
&9. -tring to : InternetAddress.to-tring#
&'. msg6i7.get@ecipients#Message.@ecipientTpe.T%$$.
&(.
&). if #to >: null$ 3
&*. -stem.out.println#;To" ; C to$.
&&. ?
&0.
&1. -tring sub!ect : msg6i7.get-ub!ect#$.
&4. if #sub!ect >: null$ 3
&5. -stem.out.println#;-ub!ect" ; C sub!ect$.
09. ?
0'. 2ate sent : msg6i7.get-ent2ate#$.
0(. if #sent >: null$ 3
0). -stem.out.println#;-ent" ; C sent$.
0*. ?
0&. -stem.out.println#;Message " ;$.
00. -stem.out.println#msg6i7.getFontent#$$.
01.
04. ?==end of for loop
05.
19. == get the message number to delete #optional$
1'. -stem.out.println#;8nter message number to delete ";$.
1(. Guffered@eader br : new Guffered@eader#new Input-tream@eader#-stem.in$$.
1). -tring no : br.read+ine#$.
1*. ==&$ delete the message using setFlag method
1&. msg6Integer.parseInt#no$ E '7.setFlag#F+A,-.Flag.28+8T82, true$.
10.
11. -stem.out.println#;Message 2eleted .....;$.
14.
15. folder.close#true$.
49. store.close#$.
4'. ?
4(. ?
download this example to delete the email
As ou can see in the above example, we are able to delete the email from the user
mailbox. How run this program b "
+oad the !ar file c"IJ set classpath"mail.jar#activation.jar#.#
compile the source file c"IJ javac DeleteMail.java
run b c"IJ java DeleteMail

Das könnte Ihnen auch gefallen