commons email发送附件DEMO

public class bb {

	/**
	 * @param args
	 * @throws EmailException 
	 */
//	public static void main(String[] args) throws EmailException {
//		SimpleEmail email = new SimpleEmail();
//		email.setHostName("smtp.163.com");// 邮件服务器
//		email.setAuthentication("***@163.com", "****");// smtp认证的用户名和密码
//		email.addTo("*****@163.com", "兔子");// 收信者
//		email.setFrom("*****@163.com", "猫咪");// 发信者
//		email.setSubject("猫咪的测试邮件");// 标题
//		email.setCharset("UTF-8");// 编码格式
//		email.setMsg("这是一封猫咪的测试邮件");// 内容
//		email.send();// 发送
//		System.out.println("OK");
//	}

	public static void main(String[] args) throws EmailException {
		MultiPartEmail email =new MultiPartEmail();
		email.setHostName("smtp.163.com");
		email.setAuthentication("****@163.com", "*****");
		email.setCharset("UTF-8");
		email.addTo("****@163.com", "兔子");
		email.setFrom("****@163.com", "猫咪");
		email.setCharset("UTF-8");
		email.setMsg("msg中文");

		EmailAttachment attachment =new EmailAttachment();
		attachment.setPath("c:/234.jpg");// 本地文件
		// attachment.setURL(new URL("http://xxx/a.gif"));//远程文件
		attachment.setDisposition(EmailAttachment.ATTACHMENT);
		attachment.setDescription("a.jpg");
		attachment.setName("a.jpg");

		email.attach(attachment);
		email.send();
		
		System.out.println("1");
	}
}




public static void main(String[] args) throws Exception {
		cc as = new cc();
		String host = "smtp.163.com";
		String from = "[email protected]";
		String username = "12345678";
		String password = "123456";
		// 接收者邮箱
		String to = "[email protected]";
		String subject = "这是李晗的测试主题!";
		String mailConent = "这是李晗的测试邮件";
		// 调用发送附件邮件方法
		as.sendAttachmentMail(host, from, username, password, to, subject,
				mailConent);
	}

	public boolean sendAttachmentMail(String host, String from,
			String username, String password, String to, String subject,
			String mailConent) throws Exception {
		// 创建附件对象
		EmailAttachment attachment = new EmailAttachment();
		/* 附件的地址 */
		attachment.setPath("c:/234.jpg");
		// 设定为附件
		attachment.setDisposition(EmailAttachment.ATTACHMENT);
		/* 附件的描述 */
		attachment.setDescription("jPortMap项目设计附件文档");
		/* 附件的名称,必须和文件名一致 */
		attachment.setName("234.jpg");
		/* new一个HtmlEmail发送对象 */
		HtmlEmail email = new HtmlEmail();
		email.setAuthentication(username, password);
		email.setHostName(host);
		email.addTo(to, from);
		email.setFrom(from);
		email.setSubject(subject);
		// 注意,发送内容时,后面这段会让中文正常显示,否则乱码
		email.setCharset("GB2312");
		email.setHtmlMsg("<html>这是封测试附件邮件</html>"); /* 邮件内容 */
		// 添加附件对象
		email.attach(attachment);
		// 发送
		email.send();
		System.out.println("带符件的邮件发送成功!");
		return true;
	}


你可能感兴趣的:(exception,String,测试,email,邮件服务器)