JavaMail学习

package com.test.mail;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class testSendMail {
 public static void main(String[] arg) throws Exception{
  sendMailWithAttachement();
//  recpMail();
 }
 
 
 
 /**
  * 不需要验证的邮件发送
  * @throws Exception
  */
 private static void setMessage() throws Exception {
  Properties props = System.getProperties();
  props.put("mail.smtp.host","mail.infoservice.com.cn");  //设置smtp的服务器地址:该邮件服务器不需要身份验证
  props.put("mail.smtp.auth","false");             //设置smtp服务器要身份验证:缺省设置为false
  
  Address from = new InternetAddress("chencheng@infoservice.com.cn");
  Address to = new InternetAddress("zouqingbing@infoservice.com.cn");
  
  
  Session session = Session.getDefaultInstance(props,null);
  Message message = new MimeMessage(session);
  message.setFrom(from);
  message.addRecipient(Message.RecipientType.TO, to);
  message.setText("I love U!!!");
  message.setSubject("Test");
  
  Transport.send(message);
  System.out.println("邮件发送完毕!");
 }
 
 /**
  * 带授权的发送邮件
  * @throws Exception
  */
 private static void setMessageWithAuthentica() throws Exception{
  Properties props = new Properties();
  props.put("mail.smtp.host","smtp.126.com");  //设置smtp的服务器地址是smtp.126.com
  props.put("mail.smtp.auth","true");          //设置smtp服务器要身份验证。
  PopupAuthenticator auth = new PopupAuthenticator();
  Session session = Session.getInstance(props, auth);

  // 发送人地址
  Address addressFrom = new InternetAddress("zqbchina@126.com", "zqbchina");
  // 收件人地址
  Address addressTo = new InternetAddress("zqbchina@sina.com", "zqbchina");
  
  // 抄送地址
  // Address addressCopy = new InternetAddress("haocongping@gmail.com", "George Bush");
  Message message = new MimeMessage(session);
  message.setContent("This is mail content!", "text/plain");//或者使用message.setText("Hello");更详细的信息请参看后续文章.
  message.setSubject("测试邮件标题");
  message.setFrom(addressFrom);
  message.addRecipient(Message.RecipientType.TO,addressTo);
  // message.addRecipient(Message.RecipientType.CC,addressCopy);
  message.saveChanges();
  // session.setDebug(true);
  Transport transport = session.getTransport("smtp");  //创建连接
  transport.connect("smtp.126.com", "zqbchina", "你的密码");//连接服务器
  transport.send(message); //发送信息
  transport.close();   //关闭连接
  System.out.println("邮件发送完毕!");
 }
 
 /**
  * 带附件的邮件发送
  * @throws Exception
  */
 private static void sendMailWithAttachement() throws Exception{
  Properties props = new Properties();
  props.put("mail.smtp.host","smtp.126.com");  //设置smtp的服务器地址是smtp.126.com
  props.put("mail.smtp.auth","true");          //设置smtp服务器要身份验证。
  PopupAuthenticator auth = new PopupAuthenticator();
  Session session = Session.getInstance(props, auth);
  File filename = new File("c:\\cmd.txt");
  
  // 发送人地址
  Address from = new InternetAddress("zqbchina@126.com", "zqbchina");
  Address to = new InternetAddress("zqbchina@sina.com", "zqbchina");
  
  //Define message
  Message message = new MimeMessage(session);
  message.setFrom(from);
  message.addRecipient(Message.RecipientType.TO,to);
  message.setSubject("Hello JavaMail Attachment");
  // Create the message part
  BodyPart messageBodyPart = new MimeBodyPart();
  // Fill the message
  messageBodyPart.setText("Pardon Ideas");
  Multipart multipart = new MimeMultipart();
  multipart.addBodyPart(messageBodyPart);
  // Part two is attachment
  messageBodyPart = new MimeBodyPart();
  DataSource source = new FileDataSource(filename);
  messageBodyPart.setDataHandler(new DataHandler(source));
  messageBodyPart.setFileName("cmd.txt");
  multipart.addBodyPart(messageBodyPart);
  // Put parts in message
  message.setContent(multipart);
  // Send the message
  Transport.send(message);
  
  
 }
 
 private static void recpMail() throws Exception{
  BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  
  /*Properties props = new Properties();
  props.put("mail.smtp.host","smtp.sina.com");
  props.put("mail.smtp.auth","true");
//  props.put("mail.pop3.socketFactory.fallback", "false");
  Session session = Session.getInstance(props, new PopupAuthenticator());
  
  Store store = session.getStore("pop3");
  store.connect();
  Folder folder = store.getFolder("INBOX");
  folder.open(Folder.READ_ONLY);
//  Message[] messages = folder.getMessages();
  System.out.println(folder.getMessageCount());
//  for (int i=0;i<messages.length;i++)
  folder.close(true);
  store.close();*/
  
  
  Properties props = new Properties();
  props.put("mail.smtp.host","mail.infoservice.com.cn");
  props.put("mail.smtp.auth","false");
  Session session = Session.getInstance(props, new PopupAuthenticator());
  
  Store store = session.getStore("pop3");
  store.connect("mail.infoservice.com.cn","zouqingbing","你的密码");
  Folder folder = store.getFolder("INBOX");
  folder.open(Folder.READ_WRITE);
  System.out.println(folder.getPermanentFlags().getSystemFlags().length);
  Message[] messages = folder.getMessages();
  for (int i=0;i<messages.length;i++){
   if (i==19) messages[i].reply(true);
   
   System.out.println(i+": 发件人=["+messages[i].getFrom()[0]+"],标题=["+messages[i].getSubject()+"]");
//   System.out.println("需要查看邮件内容吗?[Y]---查看;[N]---暂时不看。");
//   String line = reader.readLine();
//   if ("Y".equals(line))
//    System.out.println(messages[i].getContent().toString().getBytes("GB2312"));
  }
  folder.close(true);
  store.close();
 }
 
}

其中,身份验证类:
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class PopupAuthenticator extends Authenticator {
    public PasswordAuthentication getPasswordAuthentication() {
        String username = "zqbchina"; //邮箱登录帐号
        String pwd = "密码";     //登录密码
        return new PasswordAuthentication(username, pwd);
    }
}

posted on 2008-05-26 12:24 心砚 阅读(652) 评论(0)  编辑  收藏 所属分类: Java


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

常用链接

留言簿(2)

随笔分类

文章分类

文章档案

Forum

搜索

最新评论

阅读排行榜

评论排行榜