1 19 20 package org.apache.james.imapserver.util; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.util.Date ; 25 import java.util.Random ; 26 27 import javax.mail.Message ; 28 import javax.mail.MessagingException ; 29 import javax.mail.Session ; 30 import javax.mail.internet.InternetAddress ; 31 import javax.mail.internet.MimeMessage ; 32 33 import com.sun.mail.util.CRLFOutputStream; 34 35 public class MessageGenerator 36 { 37 private static Random random; 38 39 protected static synchronized Random getRandom() { 40 if (random == null) { 41 random = new Random (); 42 } 43 return random; 44 45 } 46 47 public static int calculateSize(MimeMessage m) throws IOException , MessagingException { 48 ByteArrayOutputStream os =new ByteArrayOutputStream (); 49 m.writeTo(os); 50 return os.size(); 51 } 52 53 public static MimeMessage generateSimpleMessage() throws MessagingException { 54 55 MimeMessage mm = new MimeMessage ((Session ) null); 56 int r = getRandom().nextInt() % 100000; 57 int r2 = getRandom().nextInt() % 100000; 58 mm.setSubject("good news" + r); 59 mm.setFrom(new InternetAddress ("user" + r + "@localhost")); 60 mm.setSentDate(new Date ()); 61 mm.setRecipients(Message.RecipientType.TO, 62 new InternetAddress [] { new InternetAddress ("user" + r2 63 + "@localhost") }); 64 String text = "Hello User" + r2 65 + "!\r\n\r\nhave a nice holiday.\r\n\r\ngreetings,\nUser" + r 66 + "\r\n"; 67 mm.setText(text); 68 return mm; 69 } 70 public static String messageContentToString(Message mm) throws IOException , MessagingException { 71 ByteArrayOutputStream os=new ByteArrayOutputStream (); 72 mm.writeTo(new CRLFOutputStream(os)); 73 return os.toString(); 74 } 75 76 public static MimeMessage [] generateSimpleMessages(int c) 77 throws MessagingException { 78 MimeMessage [] msgs=new MimeMessage [c]; 79 for (int i=0; i<c; i++) { 80 msgs[i]=generateSimpleMessage(); 81 } 82 return msgs; 83 } 84 85 public static MimeMessage generateMessage(int size) throws MessagingException { 86 MimeMessage mm = new MimeMessage ((Session ) null); 87 int r = getRandom().nextInt() % 100000; 88 int r2 = getRandom().nextInt() % 100000; 89 mm.setSubject("good news" + r); 90 mm.setFrom(new InternetAddress ("user" + r + "@localhost")); 91 mm.setSentDate(new Date ()); 92 mm.setRecipients(Message.RecipientType.TO, 93 new InternetAddress [] { new InternetAddress ("user" + r2 94 + "@localhost") }); 95 char[] textChars=new char[size]; 96 for (int i = 0; i < textChars.length; i++) { 97 if (i%80 == 0) { 98 textChars[i]='\n'; 99 } else { 100 textChars[i]=(char)(65+getRandom().nextInt(26)); 101 } 102 } 103 mm.setText(new String (textChars)); 104 return mm; 105 } 106 } 107 | Popular Tags |