2 import javax
.mail
.internet
.*;
3 import com
.msoft
.mail
.provider
.nntp
.NNTPTransport
;
4 import java
.util
.Properties
;
6 import javax
.activation
.*;
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
)
37 public boolean sendMail()
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 );
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
);
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
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
93 statusLine
= "Connecting to mail server";
94 status
.setStatus( statusPos
, statusLine
);
95 transport
.connect( hostname
, null, null );
98 statusLine
= "Sending message";
99 status
.setStatus( statusPos
, statusLine
);
100 transport
.sendMessage( message
, message
.getAllRecipients() );
103 statusLine
= "Closing transport";
104 status
.setStatus( statusPos
, statusLine
);
107 // Clean up when finished
108 attachments
.removeTempDocs();
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();