📄 sendmessage.java
字号:
/* CVS ID: $Id: SendMessage.java,v 1.6 2001/01/11 18:41:06 wastl Exp $ */
import net.wastl.webmail.server.*;
import net.wastl.webmail.server.http.*;
import net.wastl.webmail.ui.html.*;
import net.wastl.webmail.ui.xml.*;
import net.wastl.webmail.misc.*;
import net.wastl.webmail.config.ConfigurationListener;
import net.wastl.webmail.exceptions.*;
import java.io.*;
import java.util.*;
import java.text.*;
import javax.mail.*;
import javax.mail.internet.*;
// Modified by exce, start
import org.bulbul.webmail.util.TranscodeUtil;
// Modified by exce, end
/*
* SendMessage.java
*
* Created: Tue Sep 7 13:59:30 1999
*
* Copyright (C) 1999-2000 Sebastian Schaffert
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/**
* Send a message and show a result page.
*
* provides: message send
* requires: composer
*
* @author Sebastian Schaffert
* @version
*/
public class SendMessage implements Plugin, URLHandler, ConfigurationListener {
public static final String VERSION="1.8";
public static final String URL="/send";
Storage store;
WebMailServer parent;
Session mailsession;
public SendMessage() {
}
public void register(WebMailServer parent) {
parent.getURLHandler().registerHandler(URL,this);
parent.getConfigScheme().configRegisterStringKey(this,"SMTP HOST","localhost","Host used to send messages via SMTP. Should be localhost or your SMTP smarthost");
parent.getConfigScheme().configRegisterYesNoKey(this,"ADVERTISEMENT ATTACH", "Attach advertisement from ADVERTISEMENT SIGNATURE PATH to each outgoing message");
parent.getConfigScheme().setDefaultValue("ADVERTISEMENT ATTACH","NO");
parent.getConfigScheme().configRegisterStringKey(this,"ADVERTISEMENT SIGNATURE PATH","advertisement.sig","Path to advertisement to attach to all outgoing messages (either absolute or relative to data directory)");
this.store=parent.getStorage();
this.parent=parent;
init();
}
protected void init() {
Properties props=new Properties();
props.put("mail.host",store.getConfig("SMTP HOST"));
props.put("mail.smtp.host",store.getConfig("SMTP HOST"));
mailsession=Session.getInstance(props,null);
}
public String getName() {
return "SendMessage";
}
public String getDescription() {
return "This URL-Handler sends a submitted message.";
}
public String getVersion() {
return VERSION;
}
public String getURL() {
return URL;
}
public void notifyConfigurationChange(String key) {
init();
}
public HTMLDocument handleURL(String suburl, HTTPSession sess1, HTTPRequestHeader head) throws WebMailException {
if(sess1 == null) {
throw new WebMailException("No session was given. If you feel this is incorrect, please contact your system administrator");
}
WebMailSession session=(WebMailSession)sess1;
UserData user=session.getUser();
HTMLDocument content;
// Modified by exce, start
Locale locale = user.getPreferredLocale();
// Modified by exce, end
/* Save message in case there is an error */
session.storeMessage(head);
if(head.isContentSet("SEND")) {
/* The form was submitted, now we will send it ... */
try {
MimeMessage msg=new MimeMessage(mailsession);
Address from[]=new Address[1];
try {
// Modified by exce, start
/**
* Why we need
* org.bulbul.util.TranscodeUtil.transcodeThenEncodeByLocale()?
*
* Because we specify client browser's encoding to UTF-8, IE seems
* to send all data encoded in UTF-8. We have to transcode all byte
* sequences we received to UTF-8, and next we encode those strings
* using MimeUtility.encodeText() depending on user's locale. Since
* MimeUtility.encodeText() is used to convert the strings into its
* transmission format, finally we can use the strings in the
* outgoing e-mail which relies on receiver's email agent to decode
* the strings.
*
* As described in JavaMail document, MimeUtility.encodeText() conforms
* to RFC2047 and as a result, we'll get strings like "=?Big5?B......".
*/
/**
* Since data in session.getUser() is read from file, the encoding
* should be default encoding.
*/
// from[0]=new InternetAddress(MimeUtility.encodeText(session.getUser().getEmail()),
// MimeUtility.encodeText(session.getUser().getFullName()));
from[0]=new InternetAddress(
TranscodeUtil.transcodeThenEncodeByLocale(session.getUser().getEmail(), null, locale),
TranscodeUtil.transcodeThenEncodeByLocale(session.getUser().getFullName(), null, locale)
);
// Modified by exce, end
} catch(UnsupportedEncodingException e) {
store.log(Storage.LOG_WARN,"Unsupported Encoding while trying to send message: "+e.getMessage());
from[0]=new InternetAddress(session.getUser().getEmail(),session.getUser().getFullName());
}
StringTokenizer t;
try {
// Modified by exce, start
/**
* Since data in session.getUser() is read from file, the encoding
* should be default encoding.
*/
// t=new StringTokenizer(MimeUtility.encodeText(head.getContent("TO")).trim(),",");
t = new StringTokenizer(TranscodeUtil.transcodeThenEncodeByLocale(head.getContent("TO"), null, locale).trim(), ",");
// Modified by exce, end
} catch(UnsupportedEncodingException e) {
store.log(Storage.LOG_WARN,"Unsupported Encoding while trying to send message: "+e.getMessage());
t=new StringTokenizer(head.getContent("TO").trim(),",;");
}
/* Check To: field, when empty, throw an exception */
if(t.countTokens()<1) {
throw new MessagingException("The recipient field must not be empty!");
}
Address to[]=new Address[t.countTokens()];
int i=0;
while(t.hasMoreTokens()) {
to[i]=new InternetAddress(t.nextToken().trim());
i++;
}
try {
// Modified by exce, start
/**
* Since data in session.getUser() is read from file, the encoding
* should be default encoding.
*/
// t=new StringTokenizer(MimeUtility.encodeText(head.getContent("CC")).trim(),",");
t = new StringTokenizer(TranscodeUtil.transcodeThenEncodeByLocale(head.getContent("CC"), null, locale).trim(), ",");
// Modified by exce, end
} catch(UnsupportedEncodingException e) {
store.log(Storage.LOG_WARN,"Unsupported Encoding while trying to send message: "+e.getMessage());
t=new StringTokenizer(head.getContent("CC").trim(),",;");
}
Address cc[]=new Address[t.countTokens()];
i=0;
while(t.hasMoreTokens()) {
cc[i]=new InternetAddress(t.nextToken().trim());
i++;
}
try {
// Modified by exce, start
/**
* Since data in session.getUser() is read from file, the encoding
* should be default encoding.
*/
// t=new StringTokenizer(MimeUtility.encodeText(head.getContent("BCC")).trim(),",");
t = new StringTokenizer(TranscodeUtil.transcodeThenEncodeByLocale(head.getContent("BCC"), null, locale).trim(), ",");
// Modified by exce, end
} catch(UnsupportedEncodingException e) {
store.log(Storage.LOG_WARN,"Unsupported Encoding while trying to send message: "+e.getMessage());
t=new StringTokenizer(head.getContent("BCC").trim(),",;");
}
Address bcc[]=new Address[t.countTokens()];
i=0;
while(t.hasMoreTokens()) {
bcc[i]=new InternetAddress(t.nextToken().trim());
i++;
}
session.setSent(false);
msg.addFrom(from);
if(to.length > 0) {
msg.addRecipients(Message.RecipientType.TO,to);
}
if(cc.length > 0) {
msg.addRecipients(Message.RecipientType.CC,cc);
}
if(bcc.length > 0) {
msg.addRecipients(Message.RecipientType.BCC,bcc);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -