Sample code for 30+ languages & platforms
Java

Send HTML Email with Image Downloaded from URL

Demonstrates how to compose an HTML email with an embedded image where the image data is downloaded from a URL.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.MailMan;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // First download the image we'll be adding to the HTML "img" tag.
        BinData bdJpg = new BinData();
        Http http = new Http();
        try {
            http.quickGetBd("https://www.chilkatsoft.com/images/starfish.jpg", bdJpg);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        MailMan mailman = new MailMan();

        // Use your SMTP server..
        mailman.setSmtpHost("smtp.yourserver.com");
        mailman.setSmtpPort(587);
        mailman.setStartTLS(true);

        // Set the SMTP login/password
        mailman.setSmtpUsername("my_login");
        mailman.setSmtpPassword("my_password");

        // Create an HTML email.
        Email email = new Email();
        email.setSubject("HTML Email with Image");
        email.setFrom("Dave <somebody@mydomain.com>");
        email.addTo("Chilkat", "info@chilkatsoft.com");

        String html = "<html><body><p>This is an HTML email with an embedded image.</p><p><img src=\"starfish.jpg\" /></p></body></html>";
        email.setHtmlBody(html);

        // Note: The "starfish.jpg" here must match the name in the "img" tag's "src" attribute in the HTML above.
        try {
            email.addRelatedBd2(bdJpg, "starfish.jpg");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        email.saveEml("qa_output/out.eml");

        // success = mailman.SendEmail(email);
        // if (success == ckfalse) {
        //     println mailman.LastErrorText;
        //     return;
        // }
        //
        // ignore = mailman.CloseSmtpConnection();

        System.out.println("Success.");
    }
}