commons-mail+javaMail 实现java的邮件发送功能

1、需要的包为:commons-email-1.2.jar;activation.jar;mail.jar,其中这三个包的下载地址为: http://download.csdn.net/source/3440071

package com.stone.mail;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

public class MailDemo {
 public static void main(String[] args) {

  // 目的地集合
  Collection<String> sendTo = new ArrayList<String>();
  sendTo.add("huangyz@neusoft.com");
  sendTo.add("370708164@qq.com");
  // 添加2个附件
  EmailAttachment attachment1 = new EmailAttachment();
  attachment1.setPath("C:/Users/Public/Pictures/capture/extra1.png");
  attachment1.setName("extra1.png");
  attachment1.setDisposition(EmailAttachment.ATTACHMENT);

  EmailAttachment attachment2 = new EmailAttachment();
  attachment2.setPath("C:/Users/Public/Pictures/capture/extra2.png");
  attachment2.setName("extra2.png");
  attachment2.setDisposition(EmailAttachment.ATTACHMENT);

  // Create email message
  try {
   HtmlEmail email = new HtmlEmail();
   email.setHostName("smtp.foxmail.com");
   email.setSubject("test Mail");
   email.setCharset("GB2312");
   email.setAuthentication("huangyunzeng@foxmail.com", "!@#$%^&*");
   email.setFrom("huangyunzeng@foxmail.com");
   //设置收件人
   for (String receiver : sendTo) {
    email.addTo(receiver);
   }
   email.setHtmlMsg("<html><body><font size='5' color='red'>This is a 测试信 from huangyz@neuqsoft.com.</font></body></html>");
   email.attach(attachment1);
   email.attach(attachment2);
   // send email
   email.send();
  } catch (EmailException e) {
   e.printStackTrace();
  }
  System.out.println("郵件已經成功發送");
 }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值