用commons-email-1.1.jar实现发邮件功能

//声明转载
//http://commons.apache.org/email/

/**简单邮件发送*/
package com.bulktree.mail;

import java.util.Date;

import org.apache.commons.mail.EmailException;

import org.apache.commons.mail.SimpleEmail;

publicclass SimpleMailTest {

publicstaticvoid main(String[] args) throws EmailException {

SimpleEmail email = new SimpleEmail();

//设置发送主机的服务器地址

email.setHostName("smtp.163.com");

//设置收件人邮箱

email.addTo("bulktree@126.com","bulktree");

//发件人邮箱

email.setFrom("bulktree@163.com", "bulktree");

//如果要求身份验证,设置用户名、密码,分别为发件人在邮件服务器上注册的用户名和密码

email.setAuthentication("bulktree", "123456");

//设置邮件的主题

email.setSubject("Hello, This is My First Email Application");

//邮件正文消息

email.setMsg("I am bulktree This is JavaMail Application");[来源 www.iocblog.net]

email.send();

System.out.println("The SimpleEmail send sucessful!!!");

}

}
-------------------------------------------------------------------------
带附件邮件发送:

package com.bulktree.mail;

import java.net.MalformedURLException;

import java.net.URL;

import org.apache.commons.mail.EmailAttachment;

import org.apache.commons.mail.EmailException;

import org.apache.commons.mail.MultiPartEmail;

publicclass AttachmentMailTest {

publicstaticvoid main(String[] args) throws EmailException, MalformedURLException {

// 创建一个Email附件

EmailAttachment emailattachment = new EmailAttachment();

emailattachment.setPath("/biao_05.jpg");

// emailattachment.setURL(new URL("http://www.blogjava.net/bulktree/picture/bulktree.jpg"));

emailattachment.setDisposition(EmailAttachment.ATTACHMENT);

emailattachment.setDescription("This is Smile picture");

emailattachment.setName("bulktree");

// 创建一个email

MultiPartEmail multipartemail = new MultiPartEmail();

multipartemail.setHostName("smtp.163.com");

multipartemail.addTo("bulktree@126.com", "bulktree");

multipartemail.setFrom("bulktree@163.com", "bulktree");

multipartemail.setAuthentication("bulktree", "123456");

multipartemail.setSubject("This is a attachment Email");

multipartemail.setMsg("this a attachment Eamil Test");

//添加附件

multipartemail.attach(emailattachment);

//发送邮件

multipartemail.send();



System.out.println("The attachmentEmail send sucessful!!!");

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值