`

for test

阅读更多
package cn.org.test.j2ee.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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;

/**
 * javaMail发送邮件工具类
 * 
 * @author test
 * @since 0.1
 */
public class MailUtil {
	/**
	 * 发送邮件
	 * 
	 * @param mailServerHost
	 *            邮件服务器地址
	 * @param mailServerPort
	 *            邮件服务器端口
	 * @param validate
	 *            是否要求身份验证
	 * @param fromAddress
	 *            发送邮件地址
	 * @param toAddress
	 *            接收邮件地址
	 * @param subject
	 *            邮件主题
	 * @param content
	 *            邮件内容
	 * @param isHTML
	 *            是否是html格式邮件
	 * @param isSSL
	 *            邮件服务器是否需要安全连接(SSL)
	 * @return true:发送成功;false:发送失败
	 */
	public static boolean sendMail(String mailServerHost,
			String mailServerPort, boolean validate, String fromAddress,
			String userPass, String toAddress, String subject, String content,
			boolean isHTML, boolean isSSL) {
		Properties p = new Properties();
		p.put("mail.smtp.host", mailServerHost);
		p.put("mail.smtp.port", mailServerPort);
		p.put("mail.smtp.auth", validate ? "true" : "false");
		if (isSSL) {
			p.put("mail.smtp.starttls.enable", "true");
			p.put("mail.smtp.socketFactory.fallback", "false");
			p.put("mail.smtp.socketFactory.port", mailServerPort);
		}
		Authenticator auth = null;
		if (validate) {
			auth = new myAuthenticator(fromAddress, userPass);
		}

		try {
			Session session = Session.getDefaultInstance(p, auth);
			Message message = new MimeMessage(session);
			Address from = new InternetAddress(fromAddress);
			Address to = new InternetAddress(toAddress);
			message.setFrom(from);
			message.setRecipient(Message.RecipientType.TO, to);
			message.setSubject(subject);
			message.setSentDate(new Date());
			if (isHTML) {
				Multipart m = new MimeMultipart();
				BodyPart bp = new MimeBodyPart();
				bp.setContent(content, "text/html; charset=utf-8");
				m.addBodyPart(bp);
				message.setContent(m);
			} else
				message.setText(content);
			Transport.send(message);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	// public static void main(String[] args) {
	// System.out
	// .println(MailUtil
	// .sendMail(
	// "smtp.live.com",
	// "25",
	// true,
	// "test111@163.com",
	// "xxxxxxxxxxxxxx",
	// "test111@163.com",
	// "test",
	// "尊敬的用户:test,您于"
	// + new Date()
	// +
	// "使用了找回密码功能,请点击链接<a href='#' target='_blank'>修改密码</a>。请慎重保管注册密码!\n\n\n测试系统\n\n\n\n\n发送时间:"
	// + new Date(), true, true));
	// }
}

class myAuthenticator extends Authenticator {
	String userName;
	String userPass;

	public myAuthenticator() {
	}

	public myAuthenticator(String userName, String userPass) {
		this.userName = userName;
		this.userPass = userPass;
	}

	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(userName, userPass);
	}
}
分享到:
评论

相关推荐

    xuchuantao_Setup_ForTest.rar

    【标题】"xuchuantao_Setup_ForTest.rar" 是一个由用户"xuchuantao"创建的用于测试的安装程序压缩包。这个压缩文件很可能包含了某个软件开发项目的一部分或者全部内容,尤其是考虑到与"Delphi"相关的标签。Delphi是...

    Spring For Test

    Spring For Test

    ForTest.java

    有注释

    bin-for test

    在IT行业中,"bin-for test" 这个标题可能指的是一个用于测试的二进制(binary)文件或者是一个包含测试用二进制文件的目录。"bin" 在计算机术语中通常代表“二进制”或“可执行文件”,通常存放操作系统、应用程序...

    MP4ONLYFORTEST

    目前写MP4播放器所需要用到的视频样本 仅作为开发测试用途

    sunlogin just for test

    sunlogin just for test

    IOS应用源码——pocker_forTest.rar

    "IOS应用源码——pocker_forTest.rar" 提供了一个名为"Pocker_forTest"的iOS应用的完整源代码,这对于我们深入学习iOS开发,特别是游戏开发或者扑克类应用的实现具有极大的价值。 源码分析通常涉及以下几个核心知识...

    ForTest.class

    ForTest.class

    sc main python for test

    sc main python for test............

    Sample_OnlyForTest_wsf_vbs类型

    这个样本是最近需求碰到的,乍一看还是感觉很复杂的,尤其是写在一行,开始先用py分割了下,找到了excuteglobal,解密,出来还需要解密excute,解密,出来仍需要解密.

    ForTest03.java

    ForTest03.java

    MyWork for Test Sreen

    sory this for Test only no use

    Advanced QTP 11.5 for Test Engineers Cookbook.pdf

    This advanced Cookbook is designed for software testers and engineers with previous automation experience and teaches QTP developers advanced programming approaches. Knowledge of software testing and ...

    Design for test

    Design for Test (DFT) Design for Test (DFT) 是一种在数字IC和嵌入式核心系统设计中,旨在提高可测试性的设计方法。该方法通过在设计阶段考虑测试问题,降低了测试的难度和成本,从而提高了产品的可靠性和质量。 ...

    JEDEC JESD82-28A:2008 Fully Buffered DIMM Design for Test,Design

    JEDEC JESD82-28A:2008 Fully Buffered DIMM Design for Test,Design for Validation (DFx) - 完整英文电子版(102页).zip

    app for test

    this is an app for test

    opera11 for test

    opera browser for you to test

    just for test

    just for test,do not download this

    forTest:forTest

    我的项目项目设置npm install编译和热重装以进行开发npm run serve编译并最小化生产npm run build整理和修复文件npm run lint自定义配置请参阅。

Global site tag (gtag.js) - Google Analytics