|
Commons Email example source code file (EmailLiveTest.java)
The Commons Email EmailLiveTest.java source code
/*
* 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 java.io.File;
import java.net.URL;
import java.util.Properties;
import java.util.ArrayList;
import java.util.Collection;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import org.apache.commons.mail.settings.EmailConfiguration;
/**
* This are regression test sending REAL email to REAL mail
* servers. The intention is to field-test certain aspects
* of email using a variety of mail clients.
*/
public class EmailLiveTest extends BaseEmailTestCase
{
/**
* @param name name
*/
public EmailLiveTest(String name)
{
super(name);
}
/**
* @throws Exception */
protected void setUp() throws Exception
{
super.setUp();
}
/**
* This test checks the various options of building a HTML email.
*
* https://issues.apache.org/jira/browse/EMAIL-65
*
* @throws Exception the test failed
*/
public void testHtmlMailMimeLayout() throws Exception
{
String textMsg;
String htmlMsg;
Collection toList;
// prepare recipient list
toList = new ArrayList();
toList.add( new InternetAddress( EmailConfiguration.TEST_TO) );
// prepare attachments
String cid;
URL url = new URL(EmailConfiguration.TEST_URL);
File imageFile = new File("./src/test/images/asf_logo_wide.gif");
EmailAttachment attachment = new EmailAttachment();
File attachmentFile = new File("./src/test/attachments/logo.pdf");
attachment.setName("logo.pdf");
attachment.setDescription("The official Apache logo");
attachment.setPath(attachmentFile.getAbsolutePath());
// prepare a mail session
Properties properties = new Properties();
properties.setProperty(Email.MAIL_DEBUG, "" + EmailConfiguration.MAIL_DEBUG);
properties.setProperty(Email.MAIL_PORT, "" + EmailConfiguration.MAIL_SERVER_PORT);
properties.setProperty(Email.MAIL_HOST, EmailConfiguration.MAIL_SERVER);
properties.setProperty(Email.MAIL_SMTP_AUTH, "true");
DefaultAuthenticator authenticator = new DefaultAuthenticator( EmailConfiguration.TEST_USER, EmailConfiguration.TEST_PASSWD);
Session session = Session.getInstance(properties, authenticator);
// 1) text + html content
HtmlEmail htmlEmail1 = new HtmlEmail();
textMsg = "Your email client does not support HTML messages";
htmlMsg = "<html>This is a HTML message without any image";
htmlEmail1.setSubject( "[email] 1.Test: text + html content");
htmlEmail1.setFrom(EmailConfiguration.TEST_FROM);
htmlEmail1.setTo(toList);
htmlEmail1.setTextMsg(textMsg);
htmlEmail1.setHtmlMsg(htmlMsg);
htmlEmail1.setMailSession( session );
if( EmailConfiguration.MAIL_FORCE_SEND ) {
htmlEmail1.send();
}
else {
htmlEmail1.buildMimeMessage();
}
EmailUtils.writeMimeMessage( new File("./target/test-emails/htmlemail1.eml"), htmlEmail1.getMimeMessage());
// 2) text + html content + image as attachment
HtmlEmail htmlEmail2 = new HtmlEmail();
textMsg = "Your email client does not support HTML messages";
htmlMsg = "<html>This is a HTML message with an image attachment";
htmlEmail2.setSubject( "[email] 2.Test: text + html content + image as attachment");
htmlEmail2.setFrom(EmailConfiguration.TEST_FROM);
htmlEmail2.setTo( toList );
htmlEmail2.setTextMsg(textMsg);
htmlEmail2.setHtmlMsg(htmlMsg);
htmlEmail2.attach(url, "Apache Logo", "The official Apache logo" );
htmlEmail2.setMailSession( session );
if( EmailConfiguration.MAIL_FORCE_SEND ) {
htmlEmail2.send();
}
else {
htmlEmail2.buildMimeMessage();
}
EmailUtils.writeMimeMessage( new File("./target/test-emails/htmlemail2.eml"), htmlEmail2.getMimeMessage());
// 3) text + html content + inline image
HtmlEmail htmlEmail3 = new HtmlEmail();
textMsg = "Your email client does not support HTML messages";
cid = htmlEmail3.embed(imageFile, "Apache Logo");
htmlMsg = "<html>This is a HTML message with an inline image -
Other Commons Email examples (source code examples)Here is a short list of links related to this Commons Email EmailLiveTest.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.