Java实现邮件的自动发送

  1. public static void sendTxtMail()  
  2.     {  
  3.        Properties props = new Properties();  
  4.        props.put("mail.smtp.host""smtp.163.com"); //smtp服务器地址  
  5.   
  6.        props.put("mail.smtp.auth"true);  //是否需要认证  
  7.          
  8.        /**实例化一个验证里,继承abstract Authenticator 
  9.         * 实现     
  10.         *   protected PasswordAuthentication getPasswordAuthentication(){ 
  11.         *       return new PasswordAuthentication(userName,password); 
  12.         *   } 
  13.         */   
  14.        MyAuthenticator myauth = new MyAuthenticator("账号","密码");  
  15.        //获得一个带有authenticator的session实例  
  16.        Session session = Session.getInstance(props,myauth);  
  17.        session.setDebug(true);//打开debug模式,会打印发送细节到console  
  18.        Message message = new MimeMessage(session); //实例化一个MimeMessage集成自abstract Message 。参数为session  
  19.        try  
  20.        {  
  21.        message.setFrom(new InternetAddress("[email protected]")); //设置发出方,使用setXXX设置单用户,使用addXXX添加InternetAddress[]  
  22.   
  23.        message.setText("只是一个简简单单的文本内容哟!"); //设置文本内容 单一文本使用setText,Multipart复杂对象使用setContent  
  24.   
  25.        message.setSubject("只是简简单单的文本标题哟!"); //设置标题   
  26.   
  27.        message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]")); //设置接收方  
  28.   
  29.       Transport.send(message); //使用Transport静态方法发送邮件  
  30.     
  31.        }catch(AddressException e)  
  32.        {  
  33.            //此处处理AddressException异常  [The exception thrown when a wrongly formatted address is encountered.]   
  34.   
  35.        }catch(MessagingException e)  
  36.        {  
  37.            //此处处理MessagingException异常 [The base class for all exceptions thrown by the Messaging classes ]  
  38.        }  
  39.          

  1.     }  

  1. //发送带有附件的邮件,将邮件的每个部分初始化一个bodypart。  
  2.     //邮件是由多个部分组成,每个部分称为一个邮件体部分,是一个 BodyPart 类对象,  
  3.     //对于 MIME 类型 邮件来讲就是 MimeBodyPart 类对象.这些邮件体包含在成为 Multipart 的容器中  
  4.     public static void sendMailWithAttachment(){  
  5.           
  6.         Properties props = new Properties();  
  7.         Session session = Session .getDefaultInstance(props);  
  8.         Message message = new MimeMessage(session);  
  9.         try  
  10.         {  
  11.         message.setSubject("这个是带有附件的标题");  
  12.         message.setFrom(new InternetAddress("[email protected]"));  
  13.         message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));  
  14.         Multipart multipart = new MimeMultipart();  
  15.         //实例化一个bodypart用于封装内容  
  16.         BodyPart bodyPart = new MimeBodyPart();  
  17.         bodyPart.setContent("<font color='red'>这个是带有附件的HTML内容</font>","text/html;charset=utf8");  
  18.         //添加bodypart到multipart  
  19.         multipart.addBodyPart(bodyPart);  
  20.         //每一个部分实例化一个bodypart,故每个附件也需要实例化一个bodypart  
  21.         bodyPart = new MimeBodyPart();  
  22.         //实例化DataSource(来自jaf),参数为文件的地址  
  23.         DataSource dataSource = new FileDataSource(file.getAbsolutePath());  
  24.         //使用datasource实例化datahandler  
  25.         DataHandler dataHandler = new DataHandler(dataSource);  
  26.         bodyPart.setDataHandler(dataHandler);  
  27.         //设置附件标题,使用MimeUtility进行名字转码,否则接收到的是乱码  
  28.         bodyPart.setFileName(javax.mail.internet.MimeUtility.encodeText(file.getName()));  
  29.         multipart.addBodyPart(bodyPart);  
  30.         message.setContent(multipart);  
  31.         Transport transport = session.getTransport("smtp");  
  32.         transport.connect("smtp.163.com","账号" , "密码");  
  33.         transport.sendMessage(message, message.getAllRecipients());  
  34.         transport.close();  
  35.         }catch(MessagingException  e)  
  36.         {}catch(UnsupportedEncodingException e){}  
  37.           
  38.     }  


1. 什么是javamail   JavaMail API是读取、撰写、发送电子信息的可选包。 
2. javamail开发需要依赖的jar包   
   mail.jar(javamail API 目前是1.4.3)-与收发有关的类都在其中 
   activation.jar(javabeans activation framework包 目前是1.0.2)--可以提供对Mime类型数据的支持。比如收发附件。 

猜你喜欢

转载自blog.csdn.net/sinat_38167943/article/details/73742197
今日推荐