Branch libreoffice-5-0-4
[LibreOffice.git] / scripting / examples / java / Newsgroup / Sender.java
blob00b3db87bacbb04ab3d4a4574a5d50d5ca7313c4
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package org.libreoffice.example.java_scripts;
21 import javax.mail.*;
22 import javax.mail.internet.*;
23 import com.msoft.mail.provider.nntp.NNTPTransport;
24 import java.util.Properties;
25 import java.io.*;
26 import javax.activation.*;
29 public class Sender {
30 // Constructor params:
31 private StatusWindow status = null;
32 private OfficeAttachment attachments = null;
33 private String replyto = "";
34 private String subject = "";
35 private String comment = "";
36 private String hostname = "";
37 private String newsgroup = "";
38 private String statusLine = "";
42 public Sender(StatusWindow sw, OfficeAttachment attach, String reply,
43 String sub, String com, String host, String group) {
44 status = sw;
45 attachments = attach;
46 replyto = reply;
47 subject = sub;
48 comment = com;
49 hostname = host;
50 newsgroup = group;
55 public boolean sendMail() {
56 int statusPos = 5;
58 try {
59 attachments.createTempDocs();
60 // Property for any information
61 Properties props = new Properties();
63 // Create unique session (null is unused authenticator info)
64 statusLine = "Creating unique session";
65 status.setStatus(statusPos, statusLine); // 5
66 Session session = Session.getInstance(props, null);
68 // Create message
69 statusPos++; // 6
70 statusLine = "Creating message";
71 status.setStatus(statusPos, statusLine);
72 MimeMessage message = new MimeMessage(session);
73 message.setFrom(new InternetAddress(replyto));
74 message.setSubject(subject);
75 message.setText(comment);
76 message.addHeader("Newsgroups", newsgroup);
78 // Buildup bodypart with text and attachments
79 Multipart multipart = new MimeMultipart();
81 BodyPart messageBodyPart = new MimeBodyPart();
82 messageBodyPart.setText(comment);
83 multipart.addBodyPart(messageBodyPart);
85 statusPos++; // 7
86 statusLine = "Adding attachment(s)";
87 status.setStatus(statusPos, statusLine);
88 File attachs[] = attachments.getAttachments();
90 for (int i = 0; i < attachs.length; i++) {
91 messageBodyPart = new MimeBodyPart();
92 DataSource filesource = new FileDataSource(attachs[i]);
93 messageBodyPart.setDataHandler(new DataHandler(filesource));
94 messageBodyPart.setFileName(attachs[i].getName());
95 multipart.addBodyPart(messageBodyPart);
98 // Add multipart to mail
99 message.setContent(multipart);
101 // Create and send NNTP transport
102 statusPos += 2; // 9
103 statusLine = "Creating NNTP transport";
104 status.setStatus(statusPos, statusLine);
105 Transport transport = new NNTPTransport(session,
106 new URLName("news:" + newsgroup));
108 // Null parameters are for user name and password
109 statusPos++; // 10
110 statusLine = "Connecting to mail server";
111 status.setStatus(statusPos, statusLine);
112 transport.connect(hostname, null, null);
114 statusPos++; // 11
115 statusLine = "Sending message";
116 status.setStatus(statusPos, statusLine);
117 transport.sendMessage(message, message.getAllRecipients());
119 statusPos++; // 12
120 statusLine = "Closing transport";
121 status.setStatus(statusPos, statusLine);
122 transport.close();
124 // Clean up when finished
125 attachments.removeTempDocs();
127 return true;
128 } catch (MessagingException me) {
129 if (statusPos == 10) {
130 statusLine = "Error connecting (User authentication?)";
133 status.setStatus(statusPos, statusLine);
134 System.out.println("Error sending message: ");
135 me.printStackTrace();
136 return false;