📄 smtp.java
字号:
package mujmail.protocols;/*MujMail - Simple mail client for J2MECopyright (C) 2006 Nguyen Son Tung <n.sontung@gmail.com>Copyright (C) 2008 David Hauzar <david.hauzar.mujmail@gmail.com> This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe 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 ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */import java.util.Vector;import mujmail.AddressBook;import mujmail.Lang;import mujmail.MessageHeader;import mujmail.MujMail;import mujmail.MyException;import mujmail.PersistentBox;import mujmail.Settings;import mujmail.account.MailAccount;import mujmail.account.MailAccountPrimary;import mujmail.connections.ConnectionCompressed;import mujmail.connections.ConnectionInterface;import mujmail.tasks.StoppableBackgroundTask;import mujmail.tasks.StoppableProgress;import mujmail.util.Decode;import mujmail.util.Functions;/** * Singleton class. It is only one instance of this class in whole program. * TODO: or consider the concept in which each instance of Outbox has own * instance of SMTP * * Can be used for sending messages from given box using the method * sendMessages() for sending arbitrary message using method * sendMessage(MessageHeader). */public class SMTP extends StoppableBackgroundTask { /** The name of this source file */ private static final String SOURCE_FILE = "SMTP"; /** Flag signals if we want to print debug prints */ private static final boolean DEBUG = false; public static final byte CLOSE = 1; public static final byte SEND = 2; byte runMode; private final MujMail mujMail; /** The box the messages of which are actually sent. Updates with each calling of methods sendMail, sendMails etc.*/ private PersistentBox box; //private TheBox reportBox; private boolean busy; private MessageHeader singleMail; //if we just want to send a single mail private ConnectionInterface connection; boolean locked; //for thread synchronizing boolean forcedDisc; //if it should disconnect from the server unconditionally private MailAccount account; /** Used to send mails via smtp */ private SMTPMailSender mailSender; private static SMTP SMTPSingleton = null; /** * Creates new instance of SMTP_MODE class. * * @param box the box which messages will SMTP_MODE send * @param mujMail the main class of the program */ private SMTP(MujMail mujMail) { super("SMTP singleton"); //this.box = box; this.mujMail = mujMail; this.connection = new ConnectionCompressed(); this.mailSender = new SMTPMailSender(connection); //initAccount cannot go here, no hashtable yet; } /** * Gets the singleton instance of SMTP class. * @param mujMail the main object of the application * @return the SMTP singleton instance */ public static SMTP getSMTPSingleton(MujMail mujMail) { if (SMTPSingleton != null) return SMTPSingleton; SMTPSingleton = new SMTP(mujMail); return SMTPSingleton; } /** Search account for sending mails * Currently we're using primary mail account for sending mails */ public void initAccount() { String primaryMailAccountName = Settings.primaryEmail; account = (MailAccount)mujMail.getMailAccounts().get(primaryMailAccountName); if (account == null) { // Error primary account not found // Safe choice ... using first mail account if (mujMail.getMailAccounts().size() > 0) { account = (MailAccount)mujMail.getMailAccounts().elements().nextElement(); } else { account = new MailAccountPrimary(); } } if (DEBUG) System.out.println("DEBUG SMTP.initAccount():\n" + getAccount()); } protected synchronized void lock() { try { while (locked) { wait(100); } } catch (InterruptedException e) { } locked = true; } protected void unlock() { locked = false; } public void stop() { if (isBusy()) { connection.quit(); } } public boolean isConnected() { try { if (connection.isConnected()) { connection.sendCRLF("NOOP"); if (connection.getLine().compareTo("") != 0) { return true; } } } catch (Exception ex) { return false; } return false; } public boolean isBusy() { return busy; } /** * Sends all mails in the box. */ public void sendMails(PersistentBox box) { this.box = box; // send mails in the box, not single mail singleMail = null; sendSMTP(); } /** * Sends single mail. * @param mail the mail to send */ public void sendMail(MessageHeader mail, PersistentBox box) { this.box = box; singleMail = mail; sendSMTP(); } /** * Sends single mail or all mails in the box - according to setting of * the variable singleMail. */ private synchronized void sendSMTP() { runMode = SEND; start(); } public synchronized void close(boolean forcedDisc, PersistentBox box) { this.box = box; if (this.forcedDisc) { return; } this.forcedDisc = forcedDisc; runMode = CLOSE; start(); } public void doWork() { if (runMode == CLOSE && forcedDisc) { busy = true; mailSender.close(); forcedDisc = false; busy = false; box.repaint(); return; } lock(); busy = true; connection.unQuit(); switch (runMode) { case CLOSE: mailSender.close(); break; case SEND: // send mails sendMails(this); break; } busy = false; singleMail = null; unlock(); box.repaint(); } /** * Adds recipients to the addressbook.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -