📄 smtp.java
字号:
* @param mail the mail which recipients should be added */ private void addRecipients(MessageHeader mail) { Vector rcps = mail.getRcp(); for (short i = (short) (rcps.size() - 1); i >= 0; --i) { String tmpRcp = (String) rcps.elementAt(i); try { mujMail.getAddressBook().saveContact(new AddressBook.Contact("", tmpRcp, "")); } catch (MyException ex) { } } } public String captureStrCRLF(String capturer, String data) { return capturer.concat(data + "\r\n"); } /** * Sends mails and displays the progress. * */ private void sendMails(StoppableProgress progress) { try { if (DEBUG) System.out.println("DEBUG SMTP.sendMails(StoppableProgress) - send mails"); // determine the number of messages being sent and set progress bar short max = (singleMail == null) ? (short) box.getMessageCount() : 1; updateProgress(max, 0); setTitle(Lang.get(Lang.ALRT_SMTP_SENDING) + Lang.get(Lang.ALRT_PL_CONNECTING)); // Todo account name mailSender.open(); // for all messages being sent MessageHeader message; // used for sending the mail again to the sent box of IMAP server // not nice (Buffer overflow) // TODO: refactor String capturedMailText; for (short j = max; j > 0; j--) { // choose message to sent if (singleMail != null) { message = singleMail; } else { message = box.getMessageHeaderAt(j - 1); } // add recipients to the addressbook if (Settings.addToAddressbook) { addRecipients(message); } // send message //box.report(Lang.get(Lang.ALRT_SMTP_SENDING) + " " + message.getSubject(), SOURCE_FILE); setTitle(Lang.get(Lang.ALRT_SMTP_SENDING) + " " + message.getSubject()); capturedMailText = mailSender.sendMailToConnection(message, MailSender.SendingModes.SMTP_MODE, progress, box); // if the message was not sent if ( "".equals( capturedMailText ) ) { message.setFailedToSend(); try { message.saveHeader(); } catch (MyException ex) { ex.printStackTrace(); } continue; } if (DEBUG) System.out.println("Account: " + getAccount()); // send message to sent folder in the IMAP server if (getAccount().isCopyToSrvSent()) { if (DEBUG) System.out.println("Copy to sent"); getAccount().getProtocol().saveMailToSent(capturedMailText, mujmail.MujMail.mujmail.getInBox()); // Alf: Use outcomming folder of message instead of inbox } // set the status of the message, mark it as deleted message.setSent(); if (!message.wasDeleted()) { // TODO: this two lines cause that sending mails does not work // the problem is while deleting mails from the box: box.deleteMarkedFromBoxAndDB(); message.markAsDeleted(); box.incDeleted(); } // save the message to the sent folder in the phone if (!Settings.safeMode) { mujMail.getSentBox().storeMail(message); } updateProgress(max, max - j + 1); if (stopped()) { break; } } Functions.sleep(500); } catch (MyException ex) { ex.printStackTrace(); //_close() prevents a deadlock in the next session, //as now when we are in the middle of the transaction, the server is waiting for data from us //but the next session we are waiting for a response from the server mailSender.close(); box.report(ex.getDetails(), SOURCE_FILE, ex); //setTitle(ex.getDetails() + " " + SOURCE_FILE + " " + ex); if (ex.getErrorCode() == MyException.COM_HALTED) { connection.unQuit(); } } catch (Throwable ex) { ex.printStackTrace(); mailSender.close(); box.report("100: " + ex, SOURCE_FILE); } // markAsDeleted all messages marked as deleted box.deleteMarkedFromBoxAndDB(); } /** * Gets account for sent copy. * @return the account */ private MailAccount getAccount() { if (account == null) { initAccount(); } return account; } /** * Mail sender which sends mails to smtp connection. */ public class SMTPMailSender extends MailSender { public SMTPMailSender(ConnectionInterface connection) { super(connection); } protected synchronized void close_() { if (!this.connection.isConnected()) { return; } setTitle(Lang.get(Lang.ALRT_PL_CLOSING) + Settings.smtpServer); forcedDisc = false; try { this.connection.sendCRLF("QUIT"); } catch (Exception e) { setTitle(Lang.get(Lang.ALRT_PL_CLOSING) + Settings.smtpServer + Lang.get(Lang.FAILED) + " : " + e); } try { this.connection.close(); } catch (Exception e) { setTitle(Lang.get(Lang.ALRT_PL_CLOSING) + Settings.smtpServer + Lang.get(Lang.FAILED) + " : " + e); } setTitle(Lang.get(Lang.ALRT_SMTP_SENDING) + Lang.get(Lang.ALRT_PL_CLOSING) + Settings.smtpServer + Lang.get(Lang.SUCCESS)); } protected boolean open_() throws MyException { if (isConnected()) { return true; } try { close_(); setTitle(Lang.get(Lang.ALRT_SMTP_SENDING) + Lang.get(Lang.ALRT_PL_CONNECTING) + Settings.smtpServer); if (DEBUG) System.out.println("DEBUG SMTP.open_() - before connecting"); this.connection.open(Settings.smtpServer + ":" + Settings.smtpPort, Settings.smtpSSL, Settings.smtpSSLType); if (DEBUG) System.out.println("DEBUG SMTP.open_() - after connecting"); String serverID = this.connection.getLine(); // Helo phase // note - this check is not taken from ESMTP (RFC2821) specification ... only my decision ... can be removed with else part if (serverID.indexOf("ESMTP") > 0) { // Try to use extended capabilities - EHLO // It's necessary for pochta.ru - if not used we aren't able to login if (DEBUG) { setTitle("Saying EHLO"); } this.connection.sendCRLF("EHLO mujmail.org"); boolean flagError = false; // EHLO command not successful flag // Check server response String smtpEHLOReply = this.connection.getLine(); if (smtpEHLOReply.startsWith("250") == false) { flagError = true; } this.connection.clearInput(); if (flagError == true) { // Falling back to HELO command //box.report("Saying HELO", SOURCE_FILE); if (DEBUG) { setTitle("Saying HELO"); } this.connection.sendCRLF("HELO mujmail.org"); //box.report("Server: " + this.connection.getLine(), SOURCE_FILE); String tmp = this.connection.getLine(); if (DEBUG) { setTitle("Server: " + tmp); } } } else { //box.report("Saying HELO", SOURCE_FILE); if (DEBUG) { setTitle("Saying HELO"); } this.connection.sendCRLF("HELO mujmail.xf.cz"); //box.report("Server: " + this.connection.getLine(), SOURCE_FILE); String tmp = this.connection.getLine(); if (DEBUG) { setTitle("Server: " + tmp); } } if (Settings.smtpAuthName.length() != 0) { //TODO: test this and return false/true //box.report("Authorizing...", SOURCE_FILE); setTitle(Lang.get(Lang.ALRT_SMTP_SENDING) + " authorizing..."); this.connection.sendCRLF("AUTH PLAIN " + Decode.toBase64("\000" + Settings.smtpAuthName + "\000" + Settings.smtpAuthPass, false)); this.connection.getLine(); } setTitle(Lang.get(Lang.ALRT_SMTP_SENDING) + Lang.get(Lang.ALRT_PL_CONNECTING) + Settings.smtpServer + Lang.get(Lang.SUCCESS)); } catch (MyException e) { e.printStackTrace(); setTitle(Lang.get(Lang.ALRT_PL_CONNECTING) + Settings.smtpServer + Lang.get(Lang.FAILED)); throw e; } catch (Exception e) { e.printStackTrace(); setTitle(Lang.get(Lang.ALRT_PL_CONNECTING) + Settings.smtpServer + Lang.get(Lang.FAILED)); throw new MyException(MyException.COM_UNKNOWN, "100: " + e.toString()); } return true; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -