java邮件发送类

package joyport.hbase;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class Mail {
	private static String encoding = "text/html;charset=UTF8";

	/**
	 * 发送邮件(默认邮件服务器)
	 * 
	 * @param mimeMessage
	 * @return
	 * @throws Exception
	 */
	private static boolean sendMail(MimeMessage mimeMessage) throws Exception {
		Session session = mimeMessage.getSession();
		Transport transport = session.getTransport("smtp");
		int port = Integer.valueOf(Config.get("mail.smtp.port"));
		transport.connect(Config.get("mail.smtp.host"), port,
				Config.get("mail.user"), Config.get("mail.pass"));
		if (mimeMessage.getRecipients(Message.RecipientType.TO) != null)
			transport.sendMessage(mimeMessage,
					mimeMessage.getRecipients(Message.RecipientType.TO));
		if (mimeMessage.getRecipients(Message.RecipientType.CC) != null)
			transport.sendMessage(mimeMessage,
					mimeMessage.getRecipients(Message.RecipientType.CC));
		if (mimeMessage.getRecipients(Message.RecipientType.BCC) != null)
			transport.sendMessage(mimeMessage,
					mimeMessage.getRecipients(Message.RecipientType.BCC));
		transport.close();
		return true;
	}

	/**
	 * 发送邮件
	 * 
	 * @param title
	 *            邮件主题
	 * @param content
	 *            邮件内容
	 * @param attachments
	 *            附件地址,多个附件以逗号分隔,每个逗号之间|分隔附件中显示的文件名。
	 * @param to
	 *            收件人,多个收件人用逗号分隔
	 * @param cc
	 *            抄送,多个抄送人用逗号分隔
	 * @param bcc
	 *            密件抄送,多个抄送人用逗号分隔
	 * @return boolean
	 * @throws Exception
	 */
	public static boolean send(String title, String content,
			String attachments, String to, String cc, String bcc)
			throws Exception {
		Properties props = new Properties();
		// props.setProperty("mail.host", Config.get("mail.host"));
		props.put("mail.smtp.starttls.enable", Config.get("mail.tls"));
		props.setProperty("mail.debug", Config.get("mail.debug"));
		props.setProperty("mail.from", Config.get("mail.from"));
		MimeMessage mimeMsg = new MimeMessage(Session.getDefaultInstance(props));
		MimeMultipart mp = new MimeMultipart();
		BodyPart bp = new MimeBodyPart();
		// 设置主体内容
		bp.setContent(content, encoding);
		mp.addBodyPart(bp);
		// 添加附件
		if (attachments != null) {
			for (String attachment : attachments.split(",")) {
				if ("".equals(attachment))
					continue;
				BodyPart filebp = new MimeBodyPart();
				// 得到文件名同样至入BodyPart
				String attachmentArr[] = attachment.split("\\|", 2);
				// 得到数据源
				FileDataSource fds = new FileDataSource(attachmentArr[0]);
				// 得到附件本身并至入BodyPart
				filebp.setDataHandler(new DataHandler(fds));
				if (null == attachmentArr[1]) {
					attachmentArr[1] = fds.getName();
				}
				filebp.setFileName(MimeUtility.encodeText(attachmentArr[1]));
				mp.addBodyPart(filebp);
			}
		}
		// 设置主题
		mimeMsg.setSubject(title);
		// 设置发信人
		String from = Config.get("mail.from");
		if (from != null && !"".equals(from))
			mimeMsg.setFrom(new InternetAddress(from));
		if (to != null && !"".equals(to))
			mimeMsg.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(to));
		if (cc != null && !"".equals(cc))
			mimeMsg.setRecipients(Message.RecipientType.CC,
					InternetAddress.parse(cc));
		if (bcc != null && !"".equals(bcc))
			mimeMsg.setRecipients(Message.RecipientType.BCC,
					InternetAddress.parse(bcc));
		mimeMsg.setContent(mp);
		mimeMsg.saveChanges();
		return sendMail(mimeMsg);
	}
}

Config.get()需要的配置文件自行替换。

发表评论

电子邮件地址不会被公开。

ERROR: si-captcha.php plugin: GD image support not detected in PHP!

Contact your web host and ask them to enable GD image support for PHP.

ERROR: si-captcha.php plugin: imagepng function not detected in PHP!

Contact your web host and ask them to enable imagepng for PHP.