采用apache commons email实现邮件的发送

  1. 发送简单的文本邮件。注意setHostName 与setAuthenticator 中的信息必须对应
    public static void main(String[] args) {
    		try {
    			Email email = new SimpleEmail();
    			email.setHostName("smtp.yeah.net");// 发送方的服务器地址
    			email.addTo("ping198909@126.com");// 接受方的邮箱地址
    			// 验证发送人信息
    			email.setAuthenticator(new DefaultAuthenticator(
    					"username", "password"));
    			// 设置发送人
    			email.setFrom("ping198909@yeah.net");
    			// 设置邮件标题
    			email.setSubject("TestMail");
    			// 设置邮件内容
    			email.setMsg("This is a test mail ... :-)");
    
    			email.send();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
     
  2. 发送带附件的邮件
    public static void main(String[] args) {
    		//创建附件信息 
    		EmailAttachment attachment = new EmailAttachment(); 
    		attachment.setPath("f:\\b1737e2dd042fd0d1f3089f4.gif");
    		attachment.setDisposition(EmailAttachment.ATTACHMENT);
    		attachment.setDescription("Picture of John");
    		attachment.setName("b1737e2dd042fd0d1f3089f4.gif");
    		// 创建邮件信息
    		MultiPartEmail email = new MultiPartEmail();
    		try{
    			email.setDebug(true);
    			email.setHostName("smtp.yeah.net");
    			email.setAuthenticator(new DefaultAuthenticator("username", "password"));
    			email.addTo("ping198909@yeah.net");
    			email.setFrom("ping198909@yeah.net");
    			email.setSubject("The picture");
    			email.setMsg("Here is the picture you wanted"); 
    			// 添加附件
    			email.attach(attachment); 
    			// 发送邮件 
    			email.send();
    		}catch(EmailException e){
    			e.printStackTrace();
    		}
    	}
     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值