📄 mailsender.java
字号:
// send final line connection.sendCRLF("\r\n."); capturedMailText = captureStrCRLF(capturedMailText, "\r\n."); // read the reply of the connection (for example smtp server) if (!connection.getLine().startsWith("250")) { // System.out.println("some error while sending"); connection.unGetLine(); reportBox.report("*" + Lang.get(Lang.ALRT_SMTP_SENDING) + message.getSubject() + Lang.get(Lang.FAILED) + ": " + connection.getLine(), SOURCE_FILE); return ""; } // message succesfully sent reportBox.report(Lang.get(Lang.ALRT_SMTP_SENDING) + message.getSubject() + Lang.get(Lang.SUCCESS), SOURCE_FILE); progress.incActual(1); return capturedMailText; } public static String captureStrCRLF(String capturer, String data) { return capturer.concat(data + "\r\n"); } /** * Sends body part representing attachement. * TODO: better handling of return value * @param attachement body part to send * @return String with data that were send into the connection * TODO: always returns "" */ private String sendAttachement(BodyPart attachement, String boundaryInBody, SendingModes sendingMode) throws Throwable { // send header of the attachement sendAttachementHeader(connection, attachement, boundaryInBody); // send data attachement.getStorage().sendContentToConnection(connection, sendingMode, false); return ""; } /** * Sends the header (the beginning) of an attachement. * Does not send the data of an attachement. * Called by metdhod sendAttachement() * * @param attachment the BodyPart representing the attachement which is sent * @param boundaryInBody the delimiter used as boundary between attachements * * @return String with data that were send into the connection * @throws java.lang.Exception */ private String sendAttachementHeader(ConnectionInterface connection, BodyPart attachment, String boundaryInBody) throws Exception { String fileName = attachment.getHeader().getName(); String fileExtension = attachment.getHeader().getExtension(); String capturedAttachmentText = ""; String tmpAttachmentLine = ""; String tmpSendLine; tmpSendLine = boundaryInBody; capturedAttachmentText = captureStrCRLF(capturedAttachmentText, tmpSendLine); connection.sendCRLF(tmpSendLine); // depending on file extension, set mime type fileExtension = fileExtension.toLowerCase(); if (fileExtension.equals("jpg")) { tmpAttachmentLine = "Content-Type: image/jpeg; name=\"" + fileName + "\""; } else if (fileExtension.equals("png")) { tmpAttachmentLine = "Content-Type: image/png; name=\"" + fileName + "\""; } else if (fileExtension.equals("3gp")) { tmpAttachmentLine = "Content-Type: video/3gpp; name=\"" + fileName + "\""; } else if (fileExtension.equals("txt")) { tmpAttachmentLine = "Content-Type: text/plain; name=\"" + fileName + "\""; } else { tmpAttachmentLine = "Content-Type: application/octet-stream; name=\"" + fileName + "\""; } connection.sendCRLF(tmpAttachmentLine); capturedAttachmentText = captureStrCRLF(capturedAttachmentText, tmpAttachmentLine); // encoding tmpAttachmentLine = "Content-Transfer-Encoding: base64"; connection.sendCRLF(tmpAttachmentLine); capturedAttachmentText = captureStrCRLF(capturedAttachmentText, tmpAttachmentLine); // filename tmpAttachmentLine = "Content-Disposition: attachment; filename=\"" + fileName + "\""; connection.sendCRLF(tmpAttachmentLine); capturedAttachmentText = captureStrCRLF(capturedAttachmentText, tmpAttachmentLine); // empty line connection.sendCRLF(""); capturedAttachmentText = captureStrCRLF(capturedAttachmentText, ""); return capturedAttachmentText; } /** * Opens the connection and sets correct state of the connection. * Must be called before the first calling of the method sendMailToConnection. * * @return true if the connection was succesfully opened * @throws mujmail.MyException */ public boolean open() throws MyException { connection.clearInput(); if ( open_() ) { connectionOpened = true; return true; } return false; } /** * Closes the connection and sets correct state of the connection. * Must be called after sending all mails - after last calling of the method * sendMailToConnection() */ public void close() { connectionOpened = false; close_(); } /** * Estabilishes the connection. * For example if the mail is sent via SMTP_MODE, this method must estabilish * the connection with the server. * @return true if the connection was succesfully opened * @throws mujmail.MyException */ protected abstract boolean open_() throws MyException; /** * Closes the connection. * For example if the mail is sent via SMTP_MODE, this method must close * the connection with the server. */ protected abstract void close_(); /** * Enumeration interface representing sending modes used when sending mail. * Sending modes differs by encoding used and whether DATA command is sent * to server after sending information about recipeints in header of the mail. */ public static interface SendingModes { /** * Gets encoded string. * @param input the data to be encoded * @param isFile true if encoded string is from file from filesystem * @return encoded string */ public String toEncoding(String input, boolean isFile); /** * Encodes the field in the header of the mail. * @param input the data from the header of the mail * @return encoded string */ public String encodeHeaderField(String input); /** * Gets the line in mail header with information about encoding. * For example: Content-Transfer-Encoding: base64 * for base64 encoding. * @return the line in mail header with information about encoding */ public String getContentTransferEncodingLine(); /** * Returns true if DATA command should be sent. * The DATA command should be sent when using SMTP connection and should * not be sent when exporting the mail into filesystem. * @return true if DATA command should be sent. */ public boolean sendDataCommand(); /** SMTP_MODE sending mode. Suitable for sending mails via SMTP_MODE. * Uses BASE64 encoding and sends DATA command. */ public static final SendingModes SMTP_MODE = new SMTP_Mode(); /** File system encoding. Suitable when exporting mail to the filesystem. * Uses 7 bit encoding and does not send data command. */ public static final SendingModes FILE_SYSTEM_MODE = new FileSystemMode(); } private static class SMTP_Mode implements SendingModes { public String toEncoding(String input, boolean isFile) { return Decode.toBase64(input, isFile); } public String encodeHeaderField(String input) { return Decode.encodeHeaderField(input); } public String getContentTransferEncodingLine() { return "Content-Transfer-Encoding: base64"; } public boolean sendDataCommand() { return true; } } private static class FileSystemMode implements SendingModes { public String toEncoding(String input, boolean isFile) { return input; } public String encodeHeaderField(String input) { return input; } public String getContentTransferEncodingLine() { return "Content-Transfer-Encoding: 7bit"; } public boolean sendDataCommand() { return false; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -