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

📄 mailprovider.java

📁 The ability to create groups of reports, and grant users access to reports by group. The ability to
💻 JAVA
字号:
/*
 * Copyright (C) 2003 Erik Swenson - eswenson@opensourcesoft.net
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if
 * not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *  
 */

package org.efs.openreports.providers;

import java.util.Properties;
import java.util.Vector;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;

import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.interceptor.component.ComponentManager;

import org.apache.log4j.Logger;

import org.efs.openreports.objects.MailMessage;

public class MailProvider implements PropertiesProviderAware
{
	protected static Logger log = Logger.getLogger(MailProvider.class.getName());

	private PropertiesProvider propertiesProvider;

	private String mailHost;

	public MailProvider()
	{
		ComponentManager container =
			(ComponentManager) ActionContext.getContext().get(
				"com.opensymphony.xwork.interceptor.component.ComponentManager");

		container.initializeObject(this);

		mailHost = propertiesProvider.getProperty("mail.smtp.host");

		log.info("Created");
	}

	public void sendMail(MailMessage mail) throws ProviderException
	{
		try
		{
			// create a session
			Properties mailProps = new Properties();
			mailProps.put("mail.smtp.host", mailHost);
			
			Session mailSession = Session.getInstance(mailProps, null);

			// construct internet addresses
			InternetAddress from = new InternetAddress(mail.getSender());

			Vector recipients = mail.getRecipients();
			InternetAddress[] to = new InternetAddress[recipients.size()];
			for (int i = 0; i < recipients.size(); i++)
			{
				to[i] = new InternetAddress((String) recipients.get(i));
			}

			// create multipart
			Multipart multipart = new MimeMultipart();

			// add text part
			if (mail.getText() != null)
			{
				MimeBodyPart mbpText = new MimeBodyPart();
				mbpText.setText(mail.getText());
				multipart.addBodyPart(mbpText);
			}

			// add file attachments
			Vector attachments = mail.getAttachments();
			for (int i = 0; i < attachments.size(); i++)
			{
				String fileAttachment = (String) attachments.get(i);
				FileDataSource source = new FileDataSource(fileAttachment);

				MimeBodyPart mbpAttachment = new MimeBodyPart();
				mbpAttachment.setDataHandler(new DataHandler(source));
				mbpAttachment.setFileName(fileAttachment);

				multipart.addBodyPart(mbpAttachment);
			}

			// add byteArrayAttachment
			if (mail.getByteArrayDataSource() != null)
			{
				MimeBodyPart mbpAttachment = new MimeBodyPart();
				mbpAttachment.setDataHandler(new DataHandler(mail.getByteArrayDataSource()));
				mbpAttachment.setFileName(mail.getByteArrayDataSource().getName());

				multipart.addBodyPart(mbpAttachment);
			}

			// create message
			Message msg = new MimeMessage(mailSession);
			msg.setFrom(from);
			msg.setRecipients(Message.RecipientType.TO, to);
			msg.setSubject(mail.getSubject());
			msg.setContent(multipart);

			Transport.send(msg);
		}
		catch (Exception e)
		{
			log.error(e.toString());
			throw new ProviderException(e.getMessage());
		}
	}

	public void setPropertiesProvider(PropertiesProvider propertiesProvider)
	{
		this.propertiesProvider = propertiesProvider;
	}

}

⌨️ 快捷键说明

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