java发送邮件

使用stmp 邮件发送服务需要我们启用邮箱的stmp服务,一般都是启动的不用自己去设置,如果没有设置的话,拿自己就去设置里找。

需要4个jar包

Dsn.jar   Mailapi.jar  Pop3.jar  stmp.jar

我的一个Demo:

  1 package com.cglin.mail;
  2 
  3 import java.io.File;
  4 import java.util.Date;
  5 import java.util.Properties;
  6 
  7 import javax.activation.DataHandler;
  8 import javax.activation.FileDataSource;    
  9 import javax.mail.BodyPart;
 10 import javax.mail.Multipart;
 11 import javax.mail.Session;
 12 import javax.mail.Transport;
 13 import javax.mail.internet.InternetAddress;
 14 import javax.mail.internet.MimeBodyPart;
 15 import javax.mail.internet.MimeMessage;
 16 import javax.mail.internet.MimeMultipart;
 17 import javax.mail.internet.MimeUtility;
 18 import javax.mail.internet.MimeMessage.RecipientType;
 19 
 20 
 21 
 22 
 23 /**
 24  * 
 25  * 用来发送字符串内容的邮件(不带附件的发送)
 26  * 
 27  * @author student
 28  *

 29  */
 30 public class Javamailmain{
 31 
 32     public static void main(String[] args) {
 33         
 34         //跟properties文件的作用相同
 35         Properties prop = new Properties();
 36         
 37         //设置属性=值
 38         prop.put("mail.smtp.auth", "true");//启动验证//表示不使用匿名登陆.(启用登陆验证  100%的邮件服务器都需要用户验证)
 39         
 40         //获取Session对象
 41         Session session = Session.getInstance(prop);
 42         session.setDebug(true);
 43         
 44         //设置邮件的内容
 45         MimeMessage msg = new MimeMessage(session);
 46         
 47         try {
 48             //发件人
 49             msg.setFrom(new InternetAddress("827477620@qq.com"));
 50             //收件人
 51             msg.setRecipient(RecipientType.TO, new InternetAddress("237431449@qq.com",false));//RecipientType.TO 收件人  CC抄送   CC秘密操作
 52             //msg.setRecipient(RecipientType.CC, new InternetAddress("admin@mini163.com"));
 53             //主题
 54             msg.setSubject("我通过Java给你发送一个邮件(html)! 哈哈 !高兴吧    我要挤爆你   !!!");
 55             
 56             
 57             //带有附件的内容
 58             Multipart mm = new MimeMultipart();
 59             
 60             //正文
 61             BodyPart b = new MimeBodyPart();
 62             b.setContent("我是一个带有附件的HTML邮件 !<a href='http://www.baidu.com' style='color=red'>百度</a>", "text/html;charset=GBK");
 63             
 64             //附件
 65             BodyPart fj = new MimeBodyPart();
 66             fj.setFileName(MimeUtility.encodeText("Follow.jpg", "utf-8", null));
 67             //读取图片文件
 68             FileDataSource fds = new FileDataSource(new File("e:/Follow.jpg"));
 69             fj.setDataHandler(new DataHandler(fds));//文件位置的指针
 70             
 71             //将发送的所有BodyPart,存放在mm对象中
 72             mm.addBodyPart(b);
 73             mm.addBodyPart(fj);
 74             
 75             //设置邮件的内容
 76             msg.setContent(mm);
 77             
 78             //发送邮件的时间
 79             msg.setSentDate(new Date());
 80         
 81             //发送邮件
 82             Transport tran = session.getTransport("smtp");
 83             while(true){
 84             //登陆到服务器
 85             tran.connect("ip", "qq", "mima");
 86             //tran.connect("smtp.qq.com", "qq号码", "密码");
 87             //tran.connect("smtp.163.com", "用户", "密码");
 88             
 89             //发送指定的邮件
 90             tran.sendMessage(msg, msg.getAllRecipients());
 91             
 92             tran.close();
 93             
 94             System.out.println("邮件发送成功!!!!");
 95             }
 96             
 97         } catch (Exception e) {
 98             e.printStackTrace();
 99             System.out.println("邮件发送失败!!!");
100         }
101     }
102     
103 }
查看代码

 

 

 

 

 

posted @ 2013-06-08 18:52  果c子  阅读(139)  评论(0编辑  收藏  举报