All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.commons.mail.ImageHtmlEmail Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.mail;

import javax.activation.DataSource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 

Small wrapper class on top of HtmlEmail which encapsulates the required logic * to retrieve images that are contained in "<img src=../>" elements in the HTML * code. This is done by replacing all img-src-elements with "cid:"-entries and * embedding images in the email. *

*

* For local files the class tries to either load them via an absolute path or - * if available - use a relative path starting from a base directory. For files * that are not found locally, the implementation tries to download * the element and link it in. *

*

* The image loading is done by an instance of DataSourceResolver * which has to be provided by the caller. *

* * @since 1.3 */ public class ImageHtmlEmail extends HtmlEmail { // Regular Expression to find all entries in an HTML // document.It needs to cater for various things, like more whitespaces // including newlines on any place, HTML is not case sensitive and there // can be arbitrary text between "IMG" and "SRC" like IDs and other things. /** Regexp for extracting {@code } tags */ public static final String REGEX_IMG_SRC = "(<[Ii][Mm][Gg]\\s*[^>]*?\\s+[Ss][Rr][Cc]\\s*=\\s*[\"'])([^\"']+?)([\"'])"; /** regexp for extracting {@code