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.*;
7 
8 
9 public class Sender
10 {
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 = "";
20 
21 
22 
23 	public Sender( StatusWindow sw, OfficeAttachment attach, String reply,
24 		       String sub, String com, String host, String group )
25 	{
26 		status = sw;
27 		attachments = attach;
28                 replyto = reply;
29                 subject = sub;
30                 comment = com;
31                 hostname = host;
32                 newsgroup = group;
33 	}
34 
35 
36 
37 	public boolean sendMail()
38 	{
39 		int statusPos = 5;
40 		try
41 		{
42 			attachments.createTempDocs();
43 			// Property for any information
44 			Properties props = new Properties();
45 
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 );
50 
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 );
60 
61 			// Buildup bodypart with text and attachments
62 			Multipart multipart = new MimeMultipart();
63 
64 			BodyPart messageBodyPart = new MimeBodyPart();
65 			messageBodyPart.setText( comment );
66 			multipart.addBodyPart( messageBodyPart );
67 
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++ )
73 			{
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 );
80 			}
81 
82 			// Add multipart to mail
83 			message.setContent( multipart );
84 
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 ));
90 
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 );
96 
97 			statusPos++; // 11
98 			statusLine = "Sending message";
99 			status.setStatus( statusPos, statusLine );
100 			transport.sendMessage( message, message.getAllRecipients() );
101 
102 			statusPos++; // 12
103 			statusLine = "Closing transport";
104 			status.setStatus( statusPos, statusLine );
105 			transport.close();
106 
107 			// Clean up when finished
108 			attachments.removeTempDocs();
109 
110 			return true;
111 		}
112 		catch( MessagingException me )
113 		{
114 			if( statusPos == 10 )
115 			{
116 				statusLine = "Error connecting (User authentication?)";
117 			}
118 			status.setStatus( statusPos, statusLine );
119 			System.out.println( "Error sending message: ");
120 			me.printStackTrace();
121 			return false;
122 		}
123 
124 	}
125 
126 }
127