📄 smtpclient.java
字号:
if (holder[i].hasExtensionFromList(this.inlineExtension)) mbp.setDisposition(MimeBodyPart.INLINE); } // Encoding violates RFC 2231 but is very common to do so for non-ASCII character sets: //mbp.setFileName(MimeUtility.encodeText(holder[i].getFileName())); if (holder[i].getContentType().startsWith("text/")) { //String tmp = MimeUtility.encodeText(new String(content, Constants.UTF8_ENCODING), Constants.UTF8_ENCODING, Constants.ENCODING_QUOTED_PRINTABLE); //mbp.setText(tmp, Constants.UTF8_ENCODING); String contentStr = new String(content, Constants.UTF8_ENCODING); if (this.breakLongMessageIdLine && emailData.isMessageIdAttachment(holder[i])) { // <messageId><sessionId>unknown</sessionId><requestId>1140597982821000000</requestId><methodName>update</methodName><expires>2006-02-23T08:46:22.821Z</expires></messageId> contentStr = ReplaceVariable.replaceAll(contentStr, "<requestId>", "\r\n<requestId>"); contentStr = ReplaceVariable.replaceAll(contentStr, "<methodName>", "\r\n<methodName>"); contentStr = ReplaceVariable.replaceAll(contentStr, "<expires>", "\r\n<expires>"); } mbp.setText(contentStr, Constants.UTF8_ENCODING); } else { // "application/xmlBlaster-xbformat" DataSource ds = new ByteArrayDataSource( content, holder[i].getContentType()); mbp.setDataHandler(new DataHandler(ds)); } multi.addBodyPart(mbp); } // add the Multipart to the message message.setContent(multi); } // else multipart // set the Date: header Date date = new Date(); message.setSentDate(date); // Set the xmlBlaster specific expiry header field // Expires: Thu, 15 Dec 2005 21:45:01 +0100 (CET) // This could be evaluated by MTA plugins if (this.addExpiresHeader && emailData.getExpiryTime() != null) { //message.setHeader(EmailData.EXPIRES_HEADER, emailData.getExpiryTime().toString()); message.setHeader(EmailData.EXPIRES_HEADER_RFC2156, MailUtil.dateTime(emailData.getExpiryTime())); } //log.severe("DEBUG ONLY: Trying to send email" + emailData.toXml(true)); send(message); //log.severe("DEBUG ONLY: Successful send email" + emailData.toXml(true)); if (log.isLoggable(Level.FINE)) log.fine("Successful send email from=" + emailData.getFrom() + " to=" + emailData.getRecipientsList()); if (log.isLoggable(Level.FINER)) log.finer("Successful send email" + emailData.toXml(true)); } catch (Exception e) { log.fine("Can't send mail: " + e.toString() + ": " + emailData.toXml(true)); throw new XmlBlasterException(Global.instance(), ErrorCode.COMMUNICATION_NOCONNECTION, "SmtpClient", "Email sending failed, no mail sent from=" + emailData.getFrom() + " to=" + emailData.getRecipientsList(), e); } } public synchronized void shutdown() { if (this.session != null) { log.info("Shutting down SMTP mail client"); this.glob.unregisterMBean(this.mbeanHandle); this.mbeanHandle = null; this.session = null; } } public String getSmtpUrl() { return this.xbUri.toString(); } public void setSmtpUrl(String uri) { try { this.xbUri = new XbUri(uri); } catch (URISyntaxException e) { throw new IllegalArgumentException( "Your URI '" + uri + "' is illegal: " + e.toString()); } } /** * @return a human readable usage help string */ public java.lang.String usage() { return "Provides access to a remote SMTP mail transfer agent (MTA)" +Global.getJmxUsageLinkInfo(this.getClass().getName(), null); } /** * @return A link for JMX usage */ public java.lang.String getUsageUrl() { return Global.getJavadocUrl(this.getClass().getName(), null); } /* dummy to have a copy/paste functionality in jconsole */ public void setUsageUrl(java.lang.String url) {} /** * @return Returns the contentForceBase64. */ public boolean isContentForceBase64() { return this.contentForceBase64; } /** * @param contentForceBase64 The contentForceBase64 to set. */ public void setContentForceBase64(boolean contentForceBase64) { this.contentForceBase64 = contentForceBase64; } /** * @return Returns the messageIdForceBase64. */ public boolean isMessageIdForceBase64() { return this.messageIdForceBase64; } /** * @param messageIdForceBase64 The messageIdForceBase64 to set. */ public void setMessageIdForceBase64(boolean messageIdForceBase64) { this.messageIdForceBase64 = messageIdForceBase64; } /** * Standalone usage example: * <code> * java -Dmail.debug=true -Dmail.smtp.url=smtp://xmlBlaster:xmlBlaster@localhost:25 org.xmlBlaster.util.protocol.email.SmtpClient -from xmlBlaster@localhost -to xmlBlaster@localhost -expires +5000 * </code> * The output is like<pre>Return-Path: <blue@localhost>Received: from localhost ([127.0.0.1]) by noty (JAMES SMTP Server 2.2.0) with SMTP ID 501 for <blue@localhost>; Tue, 21 Feb 2006 10:54:58 +0100 (CET)Message-ID: <13748088.01140515698827.JavaMail.xmlBlaster@noty>Date: Tue, 21 Feb 2006 10:54:58 +0100 (CET)From: blue@localhostTo: blue@localhostSubject: Hi from javaMIME-Version: 1.0Content-Type: text/plain; charset=UTF-8Content-Transfer-Encoding: 7bitExpires: Tue, 21 Feb 2006 10:55:00 +0100 (CET)Some body text</pre> * @see #setSessionProperties(Properties) for other properties */ public static void main(String[] args) { Global glob = new Global(args); SmtpClient mail = null; try { mail = SmtpClient.getSmtpClient(glob, null); final boolean debug = false; // Here we create the mail Session manually without a JNDI lookup Properties props = System.getProperties(); props.put("mail.debug", "" + debug); mail.setSessionProperties(props, glob, null); String from = glob.getProperty().get("from", "blue@localhost"); String to = glob.getProperty().get("to", "blue@localhost"); String subject = glob.getProperty().get("subject", "Hi from java"); String content = glob.getProperty().get("content", "Some body text"); String expires = glob.getProperty().get("expires", ""); // "+5000" means lives 5 sec from now on Timestamp ts = null; if (expires.length() > 0) { Date now = new Date(); if (expires.indexOf("+") == 0) { ts = new Timestamp(Long.valueOf(expires.substring(1)).longValue() + now.getTime()); } else ts = Timestamp.valueOf(expires); } EmailData msg = new EmailData(to, from, subject, content); if (ts != null) msg.setExpiryTime(ts); System.out.println("Sending message " + msg.toXml(true)); mail.sendEmail(msg); System.out.println("Sent a message from '" + from + "' to '" + to + "'"); } catch (Exception e) { e.printStackTrace(); System.out.println("Mail failed: " + e.toString()); } finally { if (mail != null) mail.shutdown(); } } /* James 2.2.0 extension in * org.apache.james.transport.mailets.RemoteDelivery.java * method * boolean deliver(MailImpl mail, Session session) * to throw away expired mails: try { // Expiry Date Indication // Supported as new RFC 822 header (Expires:). // @see http://www.faqs.org/rfcs/rfc2156.html final String EXPIRES_HEADER_RFC2156 = "Expires"; String[] expires = mail.getMessage().getHeader(EXPIRES_HEADER_RFC2156); if (expires != null && expires.length > 0) { // Date: Thu, 17 Nov 2005 16:45:12 +0100 (CET) String value = expires[0].trim(); java.text.DateFormat df = new javax.mail.internet.MailDateFormat(); java.util.Date expire = df.parse(value); java.util.Date now = new java.util.Date(); if (now.getTime() > expire.getTime()) { StringBuffer logMessageBuffer = new StringBuffer(256) .append("Mail ") .append(mail.getName()) .append(" to host ") .append(outgoingMailServer.getHostName()) .append(" at ") .append(outgoingMailServer.getHost()) .append(" to addresses ") .append(Arrays.asList(addr)) .append(" is expired since ") .append(value) .append(" and silently discarded"); log(logMessageBuffer.toString()); return true; } } } catch (Throwable e) { e.printStackTrace(); // Ignore Expires: problems } */ /** * @return Returns the addExpiresHeader. */ public boolean isAddExpiresHeader() { return this.addExpiresHeader; } /** * @param addExpiresHeader The addExpiresHeader to set. */ public void setAddExpiresHeader(boolean addExpiresHeader) { this.addExpiresHeader = addExpiresHeader; } /** * @return Returns the breakLongMessageIdLine. */ public boolean isBreakLongMessageIdLine() { return this.breakLongMessageIdLine; } /** * @param breakLongMessageIdLine The breakLongMessageIdLine to set. */ public void setBreakLongMessageIdLine(boolean breakLongMessageIdLine) { this.breakLongMessageIdLine = breakLongMessageIdLine; } public int getSmtpIoTimeout() { return smtpIoTimeout; } //I don't think i can change this on an established connection //public void setSmtpIoTimeout(int smtpIoTimeout) { // this.smtpIoTimeout = smtpIoTimeout; //} public int getSmtpConnectionTimeout() { return smtpConnectionTimeout; } //I don't think i can change this on an established connection //public void setSmtpConnectionTimeout(int smtpConnectionTimeout) { // this.smtpConnectionTimeout = smtpConnectionTimeout; //}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -