📄 cmsmail.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/com/opencms/defaults/CmsMail.java,v $
* Date : $Date: 2001/07/31 15:50:13 $
* Version: $Revision: 1.8 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001 The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.opencms.defaults;
import com.opencms.file.*;
import com.opencms.core.*;
import com.opencms.util.*;
import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;
import java.util.*;
/**
* This class is used to send a mail using the JavaMail package.
* Sun's <code>mail.jar</code> and <code>activation.jar</code> are required,
* if this class should be used in any OpenCms class.
* <P>
* Sender and recipients addresses may be given as <code>String</code>
* or as <code>CmsUser</code> objects.
* Different constructors for setting additional CC and BCC addresses are provided.
* <P>
* Attachments can be added using the <code>addAttachment()</code> method.
* If the current HTTP request is a <code>multipart/form-data</code> request
* and contains uploaded files, all these files will be sent as attachments, too.
* <P>
* For performance reasons, this classes uses threads for sending mails.
* Mail threads can be initialized and started by :
* <ul>
* <li>creating a new CmsMail object with the appropriate constructor</li>
* <li>eventually adding one ore more attachments to this objects</li>
* <li>calling the <code>start()</code> method on this object.
* </ul>
* <P>
* Example:<br><code>
* String from = "waruschan.babachan@framfab.de";<br>
* String[] to = {"alexander.lucas@framfab.de"};<br>
* String subject = "Testmail";<br>
* String content = "Hello World!";<br>
* <br>
* CmsMail mail=new CmsMail(cms, from, to, subject, content, "text/plain");<br>
* mail.start();<br>
* </code>
*
* @author Waruschan Babachan <waruschan.babachan@framfab.de>
* @author mla
* @author Alexander Lucas <alexander.lucas@framfab.de>
*
* @version $Name: $ $Revision: 1.8 $ $Date: 2001/07/31 15:50:13 $
* @since OpenCms 4.1.37. Previously, this class was part of the <code>com.opencms.workplace</code> package.
*/
public class CmsMail extends Thread implements I_CmsLogChannels {
// constants
private String c_FROM = "";
private String[] c_TO = null;
private String[] c_BCC = null;
private String[] c_CC = null;
private String c_MAILSERVER = "";
private String c_ALTERNATIVE_MAILSERVER = "";
private String c_SUBJECT = "";
private String c_CONTENT = "";
private String c_TYPE = "";
private CmsObject c_CMS = null;
private String m_defaultSender = null;
private Vector attachContent = new Vector();
private Vector attachType = new Vector();
/**
* The constuctors without CmsObject
*/
public CmsMail(String from, String[] to, String subject, String content, String type) throws CmsException {
this(null, from, to, subject, content, type);
}
public CmsMail(CmsUser from, CmsUser[] to, String subject, String content, String type) throws CmsException {
this(null, from, to, subject, content, type);
}
public CmsMail(CmsUser from, CmsGroup to, String subject, String content, String type) throws CmsException {
this(null, from, to, subject, content, type);
}
public CmsMail(String from, String[] to, String[] cc, String[] bcc, String subject, String content, String type) throws com.opencms.core.CmsException {
this(null, from, to, cc, bcc, subject, content, type);
}
public CmsMail(String from, String[] to, String[] bcc, String subject, String content, String type) throws CmsException {
this(null, from, to, bcc, subject, content, type);
}
/**
* Create a new email object with a <code>CmsUser</code> as sender and an array of
* <code>CmsUser</code>'s as recipient(s). Email addresses will be taken from
* the CmsUser properties.
*
* @param cms Cms object
* @param from User object that contains the address of sender.
* @param to User object that contains the address of recipient.
* @param subject Subject of email.
* @param content Content of email.
* @param type ContentType of email.
*/
public CmsMail(CmsObject cms, CmsUser from, CmsUser[] to, String subject, String content, String type) throws CmsException {
// Get Registry
I_CmsRegistry reg = com.opencms.core.OpenCms.getRegistry();
// check sender email address
String fromAddress = from.getEmail();
if(fromAddress == null || fromAddress.equals("")) {
fromAddress = reg.getSystemValue("defaultmailsender");
}
if(fromAddress == null || fromAddress.equals("")) {
throw new CmsException("[" + this.getClass().getName() + "] " + "Error in sending email,Unknown sender email address.", CmsException.C_BAD_NAME);
}
if(fromAddress.indexOf("@") == -1 || fromAddress.indexOf(".") == -1) {
throw new CmsException("[" + this.getClass().getName() + "] " + "Error in sending email,Unknown sender email address: " + fromAddress, CmsException.C_BAD_NAME);
}
// check recipient email address
Vector v = new Vector(to.length);
for(int i = 0;i < to.length;i++) {
if(to[i].getEmail() == null) {
continue;
}
if(to[i].getEmail().equals("")) {
continue;
}
if(to[i].getEmail().indexOf("@") == -1 || to[i].getEmail().indexOf(".") == -1) {
throw new CmsException("[" + this.getClass().getName() + "] " + "Error in sending email, Invalid recipient email address: " + to[i].getEmail(), CmsException.C_BAD_NAME);
}
v.addElement(to[i].getEmail());
}
String users[] = new String[v.size()];
for(int i = 0;i < v.size();i++) {
users[i] = (String)v.elementAt(i);
}
if(users.length == 0) {
throw new CmsException("[" + this.getClass().getName() + "] " + "Error in sending email,Unknown recipient email address.", CmsException.C_BAD_NAME);
}
c_TO = users;
c_FROM = fromAddress;
c_SUBJECT = (subject == null ? "" : subject);
c_CONTENT = (content == null ? "" : content);
c_MAILSERVER = reg.getSystemValue("smtpserver");
c_ALTERNATIVE_MAILSERVER = reg.getSystemValue("smtpserver2");
c_TYPE = type;
c_CMS = cms;
}
/**
* Create a new email object with a <code>CmsUser</code> as sender and a
* <code>CmsGroup</code> as recipient(s). The sender's address will be taken from
* the CmsUser properties, the recipient's addresses from all CmsUsers
* belonging to the given group.
*
* @param cms Cms object.
* @param from User object that contains the address of sender.
* @param to Group object that contains the address of recipient.
* @param subject Subject of email.
* @param content Content of email.
* @param type ContentType of email.
*/
public CmsMail(CmsObject cms, CmsUser from, CmsGroup to, String subject, String content, String type) throws CmsException {
// Get Registry
I_CmsRegistry reg = com.opencms.core.OpenCms.getRegistry();
// check sender email address
String fromAddress = from.getEmail();
if(fromAddress == null || fromAddress.equals("")) {
fromAddress = reg.getSystemValue("defaultmailsender");
}
if(fromAddress == null || fromAddress.equals("")) {
throw new CmsException("[" + this.getClass().getName() + "] " + "Error in sending email,Unknown sender email address.", CmsException.C_BAD_NAME);
}
if(fromAddress.indexOf("@") == -1 || fromAddress.indexOf(".") == -1) {
throw new CmsException("[" + this.getClass().getName() + "] " + "Error in sending email,Unknown sender email address: " + fromAddress, CmsException.C_BAD_NAME);
}
// check recipient email address
Vector vu = cms.getUsersOfGroup(to.getName());
Vector v = new Vector(vu.size());
for(int i = 0;i < vu.size();i++) {
String address = ((CmsUser)vu.elementAt(i)).getEmail();
if(address == null) {
continue;
}
if(address.equals("")) {
continue;
}
if(address.indexOf("@") == -1 || address.indexOf(".") == -1) {
throw new CmsException("[" + this.getClass().getName() + "] " + "Error in sending email, Invalid recipient email address: " + address, CmsException.C_BAD_NAME);
}
v.addElement(address);
}
String users[] = new String[v.size()];
for(int i = 0;i < v.size();i++) {
users[i] = (String)v.elementAt(i);
}
if(users.length == 0) {
throw new CmsException("[" + this.getClass().getName() + "] " + "Error in sending email,Unknown recipient email address.", CmsException.C_BAD_NAME);
}
c_TO = users;
c_FROM = fromAddress;
c_SUBJECT = (subject == null ? "" : subject);
c_CONTENT = (content == null ? "" : content);
c_MAILSERVER = reg.getSystemValue("smtpserver");
c_ALTERNATIVE_MAILSERVER = reg.getSystemValue("smtpserver2");
c_TYPE = type;
c_CMS = cms;
}
/**
* Create a new email object with given FROM, TO, CC and BCC addresses.
*
* @see #CmsMail(CmsObject,String, String[], String[], String, String, String)
*
* @param cms Cms object.
* @param from Address of sender.
* @param to Address of recipient.
* @param cc Address of copy recipient.
* @param bcc Address of blank copy recipient.
* @param subject Subject of email.
* @param content Content of email.
* @param type ContentType of email.
*/
public CmsMail(CmsObject cms, String from, String[] to, String[] cc, String[] bcc, String subject, String content, String type) throws com.opencms.core.CmsException {
this(cms, from, to, bcc, subject, content, type);
Vector v = new Vector();
for(int i = 0;i < cc.length;i++) {
if(cc[i] == null) {
continue;
}
if(cc[i].equals("")) {
continue;
}
if(cc[i].indexOf("@") == -1 || cc[i].indexOf(".") == -1) {
continue;
}
v.addElement(cc[i]);
}
String users[] = new String[v.size()];
for(int i = 0;i < v.size();i++) {
users[i] = (String)v.elementAt(i);
}
if(users.length == 0) {
throw new CmsException("[" + this.getClass().getName() + "] " + "Error in sending email,Unknown recipient email address.", CmsException.C_BAD_NAME);
}
c_CC = users;
}
/**
* Create a new email object with given FROM, TO and BCC addresses.
*
* @see #CmsMail(CmsObject,String, String[], String, String, String)
*
* @param cms Cms object.
* @param from Address of sender.
* @param to Address of recipient.
* @param bcc Address of blank copy recipient.
* @param subject Subject of email.
* @param content Content of email.
* @param type ContentType of email.
*/
public CmsMail(CmsObject cms, String from, String[] to, String[] bcc, String subject, String content, String type) throws CmsException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -