/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. import javax.mail.*;   // J2EE 

2. import javax.mail.internet.*; 

3. import javax.activation.*; 

4. import java.util.*; 

5.  

6. public class SEMMMail 

7.

8. public static void main ( String[] args ) 

9.

10. try

11. //===== send mail  : ============ 

12.   Properties  props = System.getProperties(); 

13.   props.setProperty("mail.smtp.host", "smtp.?????.nl" ); 

14.   Session ses = Session.getInstance( props, null); 

15.  

16.   MimeMessage  mes = new MimeMessage ( ses ); 

17.   mes.setFrom ( new InternetAddress ("yipp@ee.nl") ); 

18.   mes.setSubject ( "pinkelen" ); 

19.   mes.setText ( "Hi,\n alluschoet ?" ); 

20.  

21.   Address[] adr =  InternetAddress.parse( "ab@surd.nl" ); 

22.   mes.setRecipient ( Message.RecipientType.TO, adr[0] ) ; 

23.  

24.   Transport trap = ses.getTransport("smtp"); 

25.   trap.connect ("smtp.hccnet.nl","yoozer","passss"); 

26.  

27.   trap.sendMessage( mes, mes.getAllRecipients() ); 

28.   trap.close(); 

29.  

30. //=========================================== 

31. // get mail  : 

32.   Store stor = ses.getStore("pop3"); 

33.   stor.connect ( "pop.?????.nl", "yoozer","passss" ); 

34.  

35.   Folder fold = stor.getFolder("INBOX"); 

36.   fold.open( Folder.READ_ONLY ); 

37.  

38.   Message[] mesar = fold.getMessages(); 

39.  

40.   if ( mesar.length != 0

41.   { 

42.     MimeMessage mesrecvd = (MimeMessage)mesar[0]; 

43.     mesrecvd.writeTo(System.out); 

44.   } 

45.   fold.close(false); 

46.   stor.close(); 

47. //........... 

48. //===== other source : ===================== 

49. // mail+attachment : 

50.  MimeMessage msg = new MimeMessage(ses); 

51.  msg.setFrom(new InternetAddress(from)); 

52.  InternetAddress[] address = {new InternetAddress(to)}; 

53.  msg.setRecipients(Message.RecipientType.TO, address); 

54.  msg.setSubject(subject); 

55.  

56.  MimeBodyPart mbp1 = new MimeBodyPart(); 

57.  mbp1.setText( strmes ); 

58.  

59.  MimeBodyPart mbp2 = new MimeBodyPart(); 

60.  

61.  FileDataSource fds = new FileDataSource(somefile); 

62.  mbp2.setDataHandler(new DataHandler(fds)); 

63.  mbp2.setFileName(fds.getName()); 

64.  

65.  Multipart mp = new MimeMultipart(); 

66.  mp.addBodyPart(mbp1); 

67.  mp.addBodyPart(mbp2); 

68.  

69.  msg.setContent(mp); 

70.  

71.  msg.setSentDate(new Date()); 

72.  

73. //...send... 

74. //================================ 

75. // delete message : 

76. folder.open(Folder.READ_WRITE); 

77. ... 

78. mes.setFlag(Flags.Flag.DELETED, true); 

79. folder.close(true); 

80.  

81. folder.expunge(); 

82. //======================= 

83. session.setDebug(true); 

84. //==== policy file : =================== 

85. grant { 

86.   permission java.io.FilePermission 

87.     "/path/to/j2ee.jar", "read"

88.   permission java.net.SocketPermission 

89.     "smtp.?????.nl:25", "connect,resolve"

90.   permission java.net.SocketPermission 

91.     "pop.????.??:110", "connect,resolve"

92.   permission java.net.SocketPermission 

93.     "imap.?????.??:143", "connect,resolve"

94.   permission java.util.PropertyPermission 

95.     "*", "read,write"

96. }; 

97.