⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mimemessagehelper.java

📁 spring的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

	public void setCc(String cc) throws MessagingException {
		this.mimeMessage.setRecipient(Message.RecipientType.CC, new InternetAddress(cc));
	}

	public void setCc(String[] cc) throws MessagingException {
		InternetAddress[] addresses = new InternetAddress[cc.length];
		for (int i = 0; i < cc.length; i++) {
			addresses[i] = new InternetAddress(cc[i]);
		}
		this.mimeMessage.setRecipients(Message.RecipientType.CC, addresses);
	}

	public void addCc(InternetAddress cc) throws MessagingException {
		this.mimeMessage.addRecipient(Message.RecipientType.CC, cc);
	}

	public void addCc(String cc) throws MessagingException {
		this.mimeMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
	}

	public void addCc(String cc, String personal) throws MessagingException, UnsupportedEncodingException {
		this.mimeMessage.addRecipient(Message.RecipientType.CC,
		                              this.encoding != null ?
		                              new InternetAddress(cc, personal, this.encoding) :
		                              new InternetAddress(cc, personal));
	}


	public void setBcc(InternetAddress bcc) throws MessagingException {
		this.mimeMessage.setRecipient(Message.RecipientType.BCC, bcc);
	}

	public void setBcc(InternetAddress[] bcc) throws MessagingException {
		this.mimeMessage.setRecipients(Message.RecipientType.BCC, bcc);
	}

	public void setBcc(String bcc) throws MessagingException {
		this.mimeMessage.setRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
	}

	public void setBcc(String[] bcc) throws MessagingException {
		InternetAddress[] addresses = new InternetAddress[bcc.length];
		for (int i = 0; i < bcc.length; i++) {
			addresses[i] = new InternetAddress(bcc[i]);
		}
		this.mimeMessage.setRecipients(Message.RecipientType.BCC, addresses);
	}

	public void addBcc(InternetAddress bcc) throws MessagingException {
		this.mimeMessage.addRecipient(Message.RecipientType.BCC, bcc);
	}

	public void addBcc(String bcc) throws MessagingException {
		this.mimeMessage.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
	}

	public void addBcc(String bcc, String personal) throws MessagingException, UnsupportedEncodingException {
		this.mimeMessage.addRecipient(Message.RecipientType.BCC,
		                              this.encoding != null ?
		                              new InternetAddress(bcc, personal, this.encoding) :
		                              new InternetAddress(bcc, personal));
	}


	public void setSubject(String subject) throws MessagingException {
		if (this.encoding != null) {
			this.mimeMessage.setSubject(subject, this.encoding);
		}
		else {
			this.mimeMessage.setSubject(subject);
		}
	}

	public void setText(String text) throws MessagingException {
		setText(text, false);
	}

	/**
	 * Set the given text directly as content in non-multipart mode
	 * respectively as default body part in multipart mode.
	 * @param text text to set
	 * @param html whether to apply content type "text/html" for an
	 * HTML mail, using default content type ("text/plain") else
	 */
	public void setText(final String text, boolean html) throws MessagingException {
		MimePart partToUse = null;
		if (this.mimeMultipart != null) {
			MimeBodyPart bodyPart = null;
			for (int i = 0; i < this.mimeMultipart.getCount(); i++) {
				BodyPart bp = this.mimeMultipart.getBodyPart(i);
				if (bp.getFileName() == null) {
					bodyPart = (MimeBodyPart) bp;
				}
			}
			if (bodyPart == null) {
				MimeBodyPart mimeBodyPart = new MimeBodyPart();
				this.mimeMultipart.addBodyPart(mimeBodyPart);
				bodyPart = mimeBodyPart;
			}
			partToUse = bodyPart;
		}
		else {
			partToUse = this.mimeMessage;
		}
		
		if (html) {
			// need to use a javax.activation.DataSource (!) to set a text
			// with content type "text/html"
			partToUse.setDataHandler(new DataHandler(
			    new DataSource() {
						public InputStream getInputStream() throws IOException {
							return new ByteArrayInputStream(encoding != null ? text.getBytes(encoding) : text.getBytes());
						}
						public OutputStream getOutputStream() throws IOException {
							throw new UnsupportedOperationException("Read-only javax.activation.DataSource");
						}
						public String getContentType() {
							return "text/html";
						}
						public String getName() {
							return "text";
						}
			    }
			));
		}
		else {
			if (this.encoding != null) {
				partToUse.setText(text, this.encoding);
			}
			else {
				partToUse.setText(text);
			}
		}
	}


	/**
	 * Add an attachment to the given MimeMessage, taking the content
	 * from a java.io.File.
	 * <p>The content type will be determined by the name of the given
	 * content file. Do not use this for temporary files with arbitrary
	 * filenames (possibly ending in ".tmp" or the like)!
	 * @param attachmentFilename the name of the attachment as it will
	 * appear in the mail
	 * @param file the File resource to take the content from
	 * @throws MessagingException
	 * @see #addAttachment(String, org.springframework.core.io.InputStreamSource)
	 * @see #addAttachment(String, javax.activation.DataSource)
	 */
	public void addAttachment(String attachmentFilename, File file) throws MessagingException {
		addAttachment(attachmentFilename, new FileDataSource(file));
	}

	/**
	 * Add an attachment to the given MimeMessage, taking the content
	 * from an org.springframework.core.InputStreamResource.
	 * <p>The content type will be determined by the given filename for
	 * the attachment. Thus, any content source will be fine, including
	 * temporary files with arbitrary filenames.
	 * @param attachmentFilename the name of the attachment as it will
	 * appear in the mail
	 * @param inputStreamSource the resource to take the content from
	 * @see #addAttachment(String, File)
	 * @see #addAttachment(String, javax.activation.DataSource)
	 */
	public void addAttachment(final String attachmentFilename, final InputStreamSource inputStreamSource)
	    throws MessagingException {
		addAttachment(attachmentFilename,
		              new DataSource() {
			              public InputStream getInputStream() throws IOException {
											return inputStreamSource.getInputStream();
										}
										public OutputStream getOutputStream() {
											throw new UnsupportedOperationException("Read-only javax.activation.DataSource");
										}
										public String getContentType() {
											return FileTypeMap.getDefaultFileTypeMap().getContentType(attachmentFilename);
										}
										public String getName() {
											return attachmentFilename;
										}
									});
	}

	/**
	 * Add an attachment to the given MimeMessage,
	 * taking the content from a <code>javax.activation.DataSource</code>.
	 * <p>Note that the InputStream returned by the DataSource implementation
	 * needs to be a <i>fresh one on each call</i>, as JavaMail will invoke
	 * getInputStream() multiple times.
	 * @param attachmentFilename the name of the attachment as it will
	 * appear in the mail (the content type will be determined by this)
	 * @param dataSource the <code>javax.activation.DataSource</code> to take
	 * the content from, determining the InputStream and the content type
	 * @throws MessagingException in case of errors
	 * @see #addAttachment(String, File)
	 * @see #addAttachment(String, org.springframework.core.io.InputStreamSource)
	 */
	public void addAttachment(String attachmentFilename, DataSource dataSource) throws MessagingException {
		if (this.mimeMultipart == null) {
			throw new IllegalStateException("Cannot add attachment - not in multipart mode");
		}
		MimeBodyPart bodyPart = new MimeBodyPart();
		bodyPart.setFileName(attachmentFilename);
		bodyPart.setDataHandler(new DataHandler(dataSource));
		this.mimeMultipart.addBodyPart(bodyPart);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -