JavaMail发送邮件实例

实现简单的邮件发送功能


1、需要两个jar包

(1)mail.jar [JavaMail]

下载地址: http://www.oracle.com/technetwork/java/javamail/index.html

(2)activation.jar[JavaBeans Activation Framework]

下载地址: http://www.oracle.com/technetwork/java/jaf11-139815.html


2、代码

public static void sendEmail() {
	try {
		String to = "xx@xx.com";//收件人邮箱
		String from = "xx@xx.com";//发件人邮箱
		String host = "smtp.xx.com";//发件邮箱SMTP地址
		String username = "xx@xx.com";//发件邮箱
		String password = "xxx";//发件邮箱密码
		
		Properties props = new Properties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.auth", "true");
		Session session = Session.getInstance(props, new EmailAutherticator(username, password));
		
		MimeMessage msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(from, title));
		msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
		msg.setSentDate(new Date());
		
		msg.setSubject(subject);
		msg.setText(text);
		
		Transport.send(msg);
	} catch(Exception e) {
		//............
	}
}

3、注意点

有时Spring框架中会有类似geronimo-javamail_1.4_xxxxxxxxxx.jar的包,需要删除掉!




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值