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

📄 messagecenter.java

📁 一个用于监控WEB服务器和数据库服务器的客户端程序。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * <p>Title: 任务管理系统</p>
 *
 * <p>Description: 提供任务管理、分配、调度、通知等功能。</p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: 卓博信息科技有限公司</p>
 *
 * @author Henry
 * @version 1.0
 */
package com.jobcn.control;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;

import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;

/**  MessageCenter.java
 *   function: 
 *   describe:
 */

public class MessageCenter {
	static {
		BasicConfigurator.configure();
	}

	/**
	 * Email发送方法
	 * @param toemails 收件人地址,如果收件人为多个,则用","隔开
	 * @param content 邮件内容
	 * @param subject 邮件主题
	 * @return 邮件发送成功返回true,否则返回false
	 */
	public static boolean sendSimpleEmail(String[] toAddress, String content,
			String subject) {
		boolean result = false;
		if (toAddress == null || toAddress.length == 0) {
			return result;
		}
		String mails = "";
		for (int i = 0; i < toAddress.length; i++) {
			if (toAddress[i].indexOf("@") != -1) {
				mails += toAddress[i] + ",";
			}
		}
		if (mails.length() > 1) {
			mails = mails.substring(0, mails.lastIndexOf(","));
		}

		Properties props = new Properties();
		props.put("mail.transpost.protocol", "smtp");
		props.put("mail.smtp.host", MAIL_SMTP_HOST);
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "25");
		CheckEmail auth = new CheckEmail(MAIL_USER, MAIL_PASSWORD);
		Session sendMailSession = Session.getDefaultInstance(props, auth);
		sendMailSession.setDebug(false);
		Message newMessage = new MimeMessage(sendMailSession);
		try {
			newMessage.setFrom(new InternetAddress(MAIL_ADDRESS));
			newMessage.setRecipients(Message.RecipientType.TO, InternetAddress
					.parse(mails));
			newMessage.setContent("SendMail", "text/multipart");
			newMessage.setSubject(subject);
			newMessage.setSentDate(new Date());
			//newMessage.setText(content);

			//			给消息对象设置内容
			BodyPart mbp = new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
			mbp.setContent(content, "multipart/mixed");//给BodyPart对象设置内容和格式/编码方式
			Multipart mp = new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对
			//			象(事实上可以存放多个)
			mp.addBodyPart(mbp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)

			//			 Part two is attachment
			//messageBodyPart = new MimeBodyPart();
			String attachPath = "./html/dddd_files/spacer.gif";
			DataSource source = new FileDataSource(attachPath);
			mbp.setDataHandler(new DataHandler(source));
			mbp.setFileName(attachPath);
			mp.addBodyPart(mbp);

			newMessage.setContent(mp);//把mp作为消息对象的内容
			newMessage.saveChanges();
			Transport transport = sendMailSession.getTransport("smtp");
			transport.connect(MAIL_SMTP_HOST, MAIL_USER, MAIL_PASSWORD);
			transport.sendMessage(newMessage, newMessage.getAllRecipients());
			transport.close();

			//Transport.send(newMessage);
			result = true;
			logger.info("邮件已发送!");
		} catch (AddressException e) {
			e.printStackTrace();
			logger.info("无效地址!");
		} catch (MessagingException e) {
			e.printStackTrace();
			logger.info("消息错,邮件发送失败!");
		}

		return result;
	}

	public static void testSendWithAttachment() {

		Properties props = new Properties();
		props.put("mail.transpost.protocol", "smtp");
		props.put("mail.smtp.host", MAIL_SMTP_HOST);
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "25");
		CheckEmail auth = new CheckEmail(MAIL_USER, MAIL_PASSWORD);
		Session sendMailSession = Session.getDefaultInstance(props, auth);
		sendMailSession.setDebug(false);
		Message message = new MimeMessage(sendMailSession);

		try {
			message.setFrom(new InternetAddress(MAIL_ADDRESS));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(
					"daibing@jobcn.com"));
			message.setContent("SendMail", "text/multipart");
			message.setSubject("Hello JavaMail Attachment");

			// create the message part 
			MimeBodyPart messageBodyPart = new MimeBodyPart();

			//fill message
			messageBodyPart.setText("邮件已发送");

			Multipart multipart = new MimeMultipart();
			multipart.addBodyPart(messageBodyPart);

			// Part two is attachment
			messageBodyPart = new MimeBodyPart();
			DataSource source = new FileDataSource(fileAttachment);
			messageBodyPart.setDataHandler(new DataHandler(source));
			messageBodyPart.setFileName(fileAttachment);
			multipart.addBodyPart(messageBodyPart);

			// Put parts in message
			message.setContent(multipart);

			// Send the message
			Transport.send(message);
			logger.info("邮件已发送!");
		} catch (AddressException e) {
			e.printStackTrace();
			logger.info("无效地址!");
		} catch (MessagingException e) {
			e.printStackTrace();
			logger.info("消息错,邮件发送失败!");
		}

	}

	public static void testSendHtmlWithImg() {
		Properties props = new Properties();
		props.put("mail.transpost.protocol", "smtp");
		props.put("mail.smtp.host", MAIL_SMTP_HOST);
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "25");
		CheckEmail auth = new CheckEmail(MAIL_USER, MAIL_PASSWORD);
		Session session = Session.getDefaultInstance(props, auth);
		session.setDebug(false);
		Message message = new MimeMessage(session);
		
		try {
			message.setSubject("测试HTML方式发送邮件s");
			message.setFrom(new InternetAddress(MAIL_ADDRESS));
			message.addRecipient(Message.RecipientType.TO,   
					new InternetAddress("daibing@jobcn.com"));
			
			String htmlText = "<H1>Hello</H1>";
			String path = "./img/";
			String[] files = new File(path).list();
			for(int i=0;i<files.length;i++) {
				htmlText += "<img src=\"cid:"+files[i]+"\"><br>";
				System.out.println("files[i]="+files[i]);
			}
			
			BodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setContent(htmlText, "text/html");
			MimeMultipart multipart = new MimeMultipart("related");
			multipart.addBodyPart(messageBodyPart);
			
			
			for(int i=0;i<files.length;i++) {
				DataSource fds = new FileDataSource(path+files[i]);
				messageBodyPart = new MimeBodyPart();
				messageBodyPart.setDataHandler(new DataHandler(fds));
				messageBodyPart.setHeader("Content-ID","<"+files[i]+">");
				multipart.addBodyPart(messageBodyPart);			
			}
			
			
			message.setContent(multipart);
			Transport.send(message);
			logger.info("邮件已发送!");
		} catch (AddressException e) {
			e.printStackTrace();
			logger.info("无效地址!");
		} catch (MessagingException e) {
			e.printStackTrace();
			logger.info("消息错,邮件发送失败!");
		}
	}

	public static void sendHtmlWithImg(String text, ArrayList imgs) {
		Properties props = new Properties();
		props.put("mail.transpost.protocol", "smtp");
		props.put("mail.smtp.host", MAIL_SMTP_HOST);
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "25");
		CheckEmail auth = new CheckEmail(MAIL_USER, MAIL_PASSWORD);
		Session session = Session.getDefaultInstance(props, auth);
		session.setDebug(false);
		Message message = new MimeMessage(session);
		

⌨️ 快捷键说明

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