| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 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/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 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 . |
| 17 |
*/ |
| 18 |
|
| 19 |
package org.libreoffice.example.java_scripts; |
| 20 |
|
| 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.*; |
| 27 |
|
| 28 |
|
| 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 = ""; |
| 39 |
|
| 40 |
|
| 41 |
|
| 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; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public boolean sendMail() { |
| 56 |
int statusPos = 5; |
| 57 |
|
| 58 |
try { |
| 59 |
attachments.createTempDocs(); |
| 60 |
// Property for any information |
| 61 |
Properties props = new Properties(); |
| 62 |
|
| 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); |
| 67 |
|
| 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); |
| 77 |
|
| 78 |
// Buildup bodypart with text and attachments |
| 79 |
Multipart multipart = new MimeMultipart(); |
| 80 |
|
| 81 |
BodyPart messageBodyPart = new MimeBodyPart(); |
| 82 |
messageBodyPart.setText(comment); |
| 83 |
multipart.addBodyPart(messageBodyPart); |
| 84 |
|
| 85 |
statusPos++; // 7 |
| 86 |
statusLine = "Adding attachment(s)"; |
| 87 |
status.setStatus(statusPos, statusLine); |
| 88 |
File attachs[] = attachments.getAttachments(); |
| 89 |
|
| 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); |
| 96 |
} |
| 97 |
|
| 98 |
// Add multipart to mail |
| 99 |
message.setContent(multipart); |
| 100 |
|
| 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)); |
| 107 |
|
| 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); |
| 113 |
|
| 114 |
statusPos++; // 11 |
| 115 |
statusLine = "Sending message"; |
| 116 |
status.setStatus(statusPos, statusLine); |
| 117 |
transport.sendMessage(message, message.getAllRecipients()); |
| 118 |
|
| 119 |
statusPos++; // 12 |
| 120 |
statusLine = "Closing transport"; |
| 121 |
status.setStatus(statusPos, statusLine); |
| 122 |
transport.close(); |
| 123 |
|
| 124 |
// Clean up when finished |
| 125 |
attachments.removeTempDocs(); |
| 126 |
|
| 127 |
return true; |
| 128 |
} catch (MessagingException me) { |
| 129 |
if (statusPos == 10) { |
| 130 |
statusLine = "Error connecting (User authentication?)"; |
| 131 |
} |
| 132 |
|
| 133 |
status.setStatus(statusPos, statusLine); |
| 134 |
System.out.println("Error sending message: "); |
| 135 |
me.printStackTrace(); |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
} |