update dev300-m58
[ooovba.git] / scripting / examples / java / Newsgroup / Sender.java
blobb1668cb4f24dcf58ff3fea0bb7b7e614e0754063
1 import javax.mail.*;
2 import javax.mail.internet.*;
3 import com.msoft.mail.provider.nntp.NNTPTransport;
4 import java.util.Properties;
5 import java.io.*;
6 import javax.activation.*;
9 public class Sender
11 // Constructor params:
12 private StatusWindow status = null;
13 private OfficeAttachment attachments = null;
14 private String replyto = "";
15 private String subject = "";
16 private String comment = "";
17 private String hostname = "";
18 private String newsgroup = "";
19 private String statusLine = "";
23 public Sender( StatusWindow sw, OfficeAttachment attach, String reply,
24 String sub, String com, String host, String group )
26 status = sw;
27 attachments = attach;
28 replyto = reply;
29 subject = sub;
30 comment = com;
31 hostname = host;
32 newsgroup = group;
37 public boolean sendMail()
39 int statusPos = 5;
40 try
42 attachments.createTempDocs();
43 // Property for any information
44 Properties props = new Properties();
46 // Create unique session (null is unused authenticator info)
47 statusLine = "Creating unique session";
48 status.setStatus( statusPos, statusLine ); // 5
49 Session session = Session.getInstance( props, null );
51 // Create message
52 statusPos++; // 6
53 statusLine = "Creating message";
54 status.setStatus( statusPos, statusLine );
55 MimeMessage message = new MimeMessage( session );
56 message.setFrom( new InternetAddress( replyto ) );
57 message.setSubject( subject );
58 message.setText( comment );
59 message.addHeader( "Newsgroups", newsgroup );
61 // Buildup bodypart with text and attachments
62 Multipart multipart = new MimeMultipart();
64 BodyPart messageBodyPart = new MimeBodyPart();
65 messageBodyPart.setText( comment );
66 multipart.addBodyPart( messageBodyPart );
68 statusPos++; // 7
69 statusLine = "Adding attachment(s)";
70 status.setStatus( statusPos, statusLine );
71 File attachs[] = attachments.getAttachments();
72 for(int i=0; i < attachs.length; i++ )
74 //System.out.println( "Adding file: " + attachs[i].getName() );
75 messageBodyPart = new MimeBodyPart();
76 DataSource filesource = new FileDataSource( attachs[i] );
77 messageBodyPart.setDataHandler( new DataHandler( filesource ));
78 messageBodyPart.setFileName( attachs[i].getName() );
79 multipart.addBodyPart( messageBodyPart );
82 // Add multipart to mail
83 message.setContent( multipart );
85 // Create and send NNTP transport
86 statusPos += 2; // 9
87 statusLine = "Creating NNTP transport";
88 status.setStatus( statusPos, statusLine );
89 Transport transport = new NNTPTransport( session, new URLName( "news:" + newsgroup ));
91 // Null parameters are for user name and password
92 statusPos++; // 10
93 statusLine = "Connecting to mail server";
94 status.setStatus( statusPos, statusLine );
95 transport.connect( hostname, null, null );
97 statusPos++; // 11
98 statusLine = "Sending message";
99 status.setStatus( statusPos, statusLine );
100 transport.sendMessage( message, message.getAllRecipients() );
102 statusPos++; // 12
103 statusLine = "Closing transport";
104 status.setStatus( statusPos, statusLine );
105 transport.close();
107 // Clean up when finished
108 attachments.removeTempDocs();
110 return true;
112 catch( MessagingException me )
114 if( statusPos == 10 )
116 statusLine = "Error connecting (User authentication?)";
118 status.setStatus( statusPos, statusLine );
119 System.out.println( "Error sending message: ");
120 me.printStackTrace();
121 return false;