📄 smtpmessagesink.java
字号:
/*
* SSL-Explorer
*
* Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.sslexplorer.notification.smtp;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.smtp.SMTPClient;
import org.apache.commons.net.smtp.SMTPReply;
import org.apache.commons.net.smtp.SimpleSMTPHeader;
import com.sslexplorer.core.CoreServlet;
import com.sslexplorer.notification.Message;
import com.sslexplorer.notification.MessageSink;
import com.sslexplorer.notification.Notifier;
import com.sslexplorer.notification.Recipient;
import com.sslexplorer.security.User;
public class SMTPMessageSink implements MessageSink {
final static Log log = LogFactory.getLog(SMTPMessageSink.class);
private SMTPClient client;
private Notifier notifier;
public SMTPMessageSink() {
super();
client = new SMTPClient();
}
public void start(Notifier notifier) throws Exception {
this.notifier = notifier;
}
public void stop() throws Exception {
}
public boolean send(Message message) throws Exception {
try {
//
List l = notifier.getFullRecipientListAsUsers(message.getRecipients());
int reply;
if (log.isDebugEnabled()) {
log.debug("Setting timeout");
log.debug("Connecting to SMTP server");
}
client.connect(CoreServlet.getServlet().getPropertyDatabase().getProperty(0, null, "smtp.hostname"),
CoreServlet.getServlet().getPropertyDatabase().getPropertyInt(0, null, "smtp.port"));
if (log.isDebugEnabled())
log.debug("Getting reply");
reply = client.getReplyCode();
if (!SMTPReply.isPositiveCompletion(reply)) {
client.disconnect();
throw new Exception("SMTP server refused connection.");
}
if (log.isDebugEnabled())
log.debug("Logging in");
String helo = CoreServlet.getServlet().getPropertyDatabase().getProperty(0, null, "smtp.login");
if(helo.equals("")) {
client.login();
}
else {
client.login(helo);
}
String[] address = new String[l.size()];
int idx = 0;
for (Iterator i = l.iterator(); i.hasNext();) {
Recipient r = (Recipient) i.next();
User u = CoreServlet.getServlet().getUserDatabase().getAccount(r.getRecipientAlias());
address[idx++] = u.getEmail();
}
String sender = CoreServlet.getServlet().getPropertyDatabase().getProperty(0, null, "smtp.senderAddress");
client.setSender(sender);
for (int i = 0; i < address.length; i++) {
client.addRecipient(address[i]);
}
Writer writer = client.sendMessageData();
if (writer == null) {
throw new Exception("Failed to send message data.");
}
SimpleSMTPHeader header = new SimpleSMTPHeader(sender, address[0], message.getSubject());
for (int i = 1; i < address.length; i++) {
header.addCC(address[i]);
}
writer.write(header.toString());
writer.write(message.getContent());
writer.close();
if (!client.completePendingCommand()) {
throw new Exception("Failed to complete sending of message data.");
}
return true;
} finally {
if (client.isConnected()) {
try {
client.disconnect();
} catch (IOException f) {
}
}
}
}
public String getName() {
return "SMTP";
}
public String getShortNameKey() {
return "notification.smtp.shortName";
}
public String getDescriptionKey() {
return "notification.smtp.description";
}
public String getBundle() {
return "setup";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -