📄 mailsender.java
字号:
/*MujMail - Simple mail client for J2MECopyright (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. */package mujmail.protocols;import java.util.Vector;import mujmail.BodyPart;import mujmail.util.Decode;import mujmail.util.Functions;//#ifdef MUJMAIL_FSimport mujmail.FileSystemMailExporter;//#endifimport mujmail.Lang;import mujmail.MessageHeader;import mujmail.MyException;import mujmail.TheBox;import mujmail.connections.ConnectionInterface;import mujmail.protocols.SMTP.SMTPMailSender;import mujmail.tasks.StoppableProgress;/** * Abstract class providing interface and basic functionality for sending mails * to the object of class BasicConnection. * * This means that it is possible to send mails to the SMTP connection as well * as to the file system connection. * * To use this class, implement methods close_() and open_() that must * correctly establish and close the connection. * * @see MailSender.SMTP_Mode * @see SMTPMailSender * @see FileSystemMailExporter * * @author David Hauzar */public abstract class MailSender { private static final String SOURCE_FILE = "mujmail.MailSender"; /** True if the connection is opened. */ private boolean connectionOpened = false; /** Email will be sent to this connection */ protected final ConnectionInterface connection; /** * Constructor. * @param connection the object to which the mail will be sent */ protected MailSender(ConnectionInterface connection) { this.connection = connection; } /** * Sends given mail to the connection of MailSender. * Deparses the content of the mail from entries of the instance * of MessageHeader. * * TODO: Warning: return value of capturedMailText: * - the return value is there just because we sometimes need (according to settings) * to send the content furthermore to the directory SENT on the imap account * - the problem is that cumulating the content of the sent mail in string * can lead to BufferOverflow * - it would be better to sent email to directory SENT separately * * - to not cause BufferOverflow the content of attachments is not cumulating * now: see methods sendAttachment * * TODO2: we are cumulating the content of sent mail even if the content * is not needed * * @param mail the mail which will be sent to the connection * @return the content of sent mail; * empty string if sending of mail was not succesfull (server replied 250..) */ public String sendMailToConnection(MessageHeader message, SendingModes sendingMode, StoppableProgress progress, TheBox reportBox ) throws MyException, Exception, Throwable { if (!connectionOpened) { throw new MyException(MyException.PROTOCOL_CANNOT_CONNECT, "The connection must be opened before sending the mail"); } String capturedMailText = ""; Vector rcps = null; String tmpRcp; // boundary defined in multipart messages (ie. with attached file) String boundary = "16509XY-120sR7729-tree0"; // -- + boundary used in multipart email body String boundaryInBody = "--" + boundary; String tmpMailLine = ""; capturedMailText = ""; // Clear text before capturing new message tmpMailLine = "MAIL FROM: <" + Functions.emailOnly(message.getFrom()) + ">"; capturedMailText = captureStrCRLF(capturedMailText, tmpMailLine); connection.sendCRLF(tmpMailLine); connection.getLine(); // send recipients rcps = message.getRcp(); for (short i = (short) (rcps.size() - 1); i >= 0; --i) { tmpRcp = (String) rcps.elementAt(i); tmpMailLine = "RCPT TO: <" + tmpRcp + ">"; capturedMailText = captureStrCRLF(capturedMailText, tmpMailLine); connection.sendCRLF(tmpMailLine); connection.getLine(); } if (sendingMode.sendDataCommand()) { tmpMailLine = "DATA"; capturedMailText = captureStrCRLF(capturedMailText, tmpMailLine); connection.sendCRLF(tmpMailLine); connection.getLine(); } tmpMailLine = "Return-Path: <" + Functions.emailOnly(message.getFrom()) + ">"; capturedMailText = captureStrCRLF(capturedMailText, tmpMailLine); connection.sendCRLF(tmpMailLine); tmpMailLine = "Date: " + message.getTimeStr() + " " + Functions.getLocalTimeZone(); capturedMailText = captureStrCRLF(capturedMailText, tmpMailLine); connection.sendCRLF(tmpMailLine); tmpMailLine = "From: " + message.getFrom(); capturedMailText = captureStrCRLF(capturedMailText, tmpMailLine); connection.sendCRLF(tmpMailLine); short i; i = (short) message.getRecipients().indexOf("To:"); if (i != -1) { tmpRcp = Functions.encodeRcpNames(message.getRecipients().substring(i, message.getRecipients().indexOf(" *", i + 3))); capturedMailText = captureStrCRLF(capturedMailText, tmpRcp); connection.sendCRLF(tmpRcp); } i = (short) message.getRecipients().indexOf("Cc:"); if (i != -1) { tmpRcp = Functions.encodeRcpNames(message.getRecipients().substring(i, message.getRecipients().indexOf(" *", i + 3))); capturedMailText = captureStrCRLF(capturedMailText, tmpRcp); connection.sendCRLF(tmpRcp); } i = (short) message.getRecipients().indexOf("Bcc:"); if (i != -1) { tmpRcp = Functions.encodeRcpNames(message.getRecipients().substring(i, message.getRecipients().indexOf(" *", i + 4))); capturedMailText = captureStrCRLF(capturedMailText, tmpRcp); connection.sendCRLF(tmpRcp); } tmpMailLine = "Subject: " + sendingMode.encodeHeaderField(message.getSubject()); capturedMailText = captureStrCRLF(capturedMailText, tmpMailLine); connection.sendCRLF(tmpMailLine); // plain or multipart email "intro" at end of email header if (message.isPlain()) { tmpMailLine = "MIME-Version: 1.0"; capturedMailText = captureStrCRLF(capturedMailText, tmpMailLine); connection.sendCRLF(tmpMailLine); tmpMailLine = "Content-Type: text/plain; charset=UTF-8"; capturedMailText = captureStrCRLF(capturedMailText, tmpMailLine); connection.sendCRLF(tmpMailLine); tmpMailLine = sendingMode.getContentTransferEncodingLine(); capturedMailText = captureStrCRLF(capturedMailText, tmpMailLine); connection.sendCRLF(tmpMailLine); tmpMailLine = "Content-Disposition: inline"; capturedMailText = captureStrCRLF(capturedMailText, tmpMailLine); connection.sendCRLF(tmpMailLine); } else if (!message.isPlain()) { tmpMailLine = "MIME-Version: 1.0"; capturedMailText = captureStrCRLF(capturedMailText, tmpMailLine); connection.sendCRLF(tmpMailLine); tmpMailLine = "Content-Type: multipart/mixed; boundary=\"" + boundary + "\""; capturedMailText = captureStrCRLF(capturedMailText, tmpMailLine); connection.sendCRLF(tmpMailLine); } connection.sendCRLF(""); capturedMailText = captureStrCRLF(capturedMailText, ""); // if multipart email, add below before text body if (!message.isPlain()) { tmpMailLine = boundaryInBody; capturedMailText = captureStrCRLF(capturedMailText, tmpMailLine); connection.sendCRLF(tmpMailLine); tmpMailLine = "Content-Type: text/plain; charset=UTF-8"; capturedMailText = captureStrCRLF(capturedMailText, tmpMailLine); connection.sendCRLF(tmpMailLine); tmpMailLine = "Content-Transfer-Encoding: base64"; capturedMailText = captureStrCRLF(capturedMailText, tmpMailLine); connection.sendCRLF(tmpMailLine); tmpMailLine = "Content-Disposition: inline"; capturedMailText = captureStrCRLF(capturedMailText, tmpMailLine); connection.sendCRLF(tmpMailLine); connection.sendCRLF(""); capturedMailText = captureStrCRLF(capturedMailText, ""); } if (message.getBodyPartCount() >= 1) { capturedMailText = message.getBodyPart((byte)0).getStorage(). sendContentToConnection(connection, sendingMode, true); } // send attachements // if multi message, add below before base64 encoded attached file if (!message.isPlain()) { for (int n = 0; n < message.getAttachementCount(); n++) { tmpMailLine = sendAttachement(message.getAttachement(n), boundaryInBody, sendingMode); capturedMailText = capturedMailText + tmpMailLine; } tmpMailLine = boundaryInBody + "--"; capturedMailText = captureStrCRLF(capturedMailText, tmpMailLine); connection.sendCRLF(tmpMailLine); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -