以下是源代吗? 简单版的
问题有两个,一个是读到的信息内容有乱码,第二个是发送不了信息。
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;public class MailText {
    public MailText(){
        }
    
    public static void main(String []args)throws Exception{
        String hostname="smtp.qq.com";   //localhost
        String username="";  //请随便输一个QQ号
        String password="";  //一个QQ密码        //set properties
        Properties props=System.getProperties();
    
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.store.protocol", "pop3");  //imap
        props.put("mail.smtp.class", "com.sun.mail.smtp.SMTPTransport");
        props.put("mail.pop3.class", "com.sun.mail.pop3.POP3Store");//com.sun.mail.pop3.POP3Store /com.sun.mail.imap.IMAPStore
        props.put("mail.smtp.host", hostname);
        props.put("mail.smtp.auth","true"); 
        //Get a Session object
        
        Session mailsession=Session.getDefaultInstance(props,null);//props
    
        //Get a Store object
        Store store=mailsession.getStore("pop3");
        //Connect
        store.connect(hostname, username, password);   //登录的判断
        //check inbox
        Folder folder=store.getFolder("inbox");
        folder.open(Folder.READ_WRITE);
        System.out.println("You have"+folder.getMessageCount()+"message in inbox");
        System.out.println("You have"+folder.getUnreadMessageCount()+"unread message in inbox");
        //read first Message in inbox
        for(int i=0;i<folder.getMessageCount();i++){
        Message msg=folder.getMessage(1);
        System.out.println("-----this <"+(i+1)+"> message in inbox------");
        System.out.println("From:"+msg.getFrom()[0]);  //返回的是一个数组
        System.out.println("Subject:"+msg.getSubject());
        System.out.println("Text:"+msg.getContent());
        }
        
        
        
        Message msg;
        //creat a message
        msg=new MimeMessage(mailsession);
        InternetAddress[] toAdds=InternetAddress.parse("[email protected]",false);
        msg.setRecipients(Message.RecipientType.TO, toAdds);
        msg.setSubject("hello");
        msg.setFrom(new InternetAddress("[email protected]"));
        msg.setText("How are you");
        
        //send a message
        Transport.send(msg);
    }
}

解决方案 »

  1.   

    你先要确定是服务端收到是不是正常的。
    如果问题出在服务器端。那么你小重写javamail API中的发有件的哪个servlet,对他的中文数据进行编码。
    如果你不想重写。那么请你把你的tomcat的server.xml文件8080端口描述的地方加的URIEncoding="UTF-8";
      

  2.   

    有些杀软或防火墙可能会让你的javamail发不出去
    另外某些邮箱新注册的账号没有开启服务(好像叫pop3的啥子服务)也是无法发送成功的
    另外楼主还要检查下账号密码smtp什么的配置是否正确
      

  3.   

    一個簡單的內部網使用 
    1.有處理亂碼
    2.你的沒有保存要發送的信息message.saveChanges();public void sendMail(List<Passport> pasList,List<Tw_Card> tcList,List<Live_Card> lcList,List<In_Out_Date> ioList){
    try{
    // 創建 properties ,裏面包含了發送郵件服務器的地址。
    java.util.Properties mailProps = new java.util.Properties();
    mailProps.put("mail.smtp.host", "192.168.12.3"); //"mail.smtp.host"隨便叫啥都行,"192.0.0.1"必須是真實可用的。
    // 創建 session
    Session mailSession = javax.mail.Session.getDefaultInstance(mailProps);
    //mailSession.setDebug(true);//是否在控制台顯示debug信息
    // 創建 郵件的message,message對象包含了郵件眾多有的部件,都是封裝成了set方法去設置的
    MimeMessage message = new javax.mail.internet.MimeMessage(mailSession);
    // 設置發信人
    //多個發件人可以使用addFrom()方法增加發件人
    message.setFrom(new InternetAddress("[email protected]"));

    //收信人
    message.setRecipient(javax.mail.Message.RecipientType.TO,
    //[email protected]
    //多個收件人可以使用addRecipient()方法增加收件人
    new javax.mail.internet.InternetAddress("[email protected]")); // 郵件標題 [email protected]
    String aa = "台胞証到期郵件通知";
    String subject = aa;
    //message.setSubject(subject); //haha,嚇唬人
    message.setSubject(subject.toString(),"utf-8");
    //textBodyPart.setText("詳情進入系統查看。地址﹕http://192.168.12.22:8000/manpower");
    BodyPart textBodyPart = new MimeBodyPart(); //第一個BodyPart.主要寫一些一般的信件內容。
    StringBuffer pasSbuff = new StringBuffer();
    Passport pas = new Passport();
    Tw_Card tc = new Tw_Card();
    for(int i=0;i<pasList.size();i++){
    pas = pasList.get(i);
    pasSbuff.append(pas.getEmp_CH_Name());
    pasSbuff.append("; ");
    }
    StringBuffer tcSbuff = new StringBuffer();
    for(int i=0;i<tcList.size();i++){
    tc = tcList.get(i);
    tcSbuff.append(tc.getEmp_CH_Name());
    tcSbuff.append("; ");
    }
    Live_Card lc = new Live_Card();
    StringBuffer lcSbuff = new StringBuffer();
    for(int i=0;i<lcList.size();i++){
    lc = lcList.get(i);
    lcSbuff.append(lc.getEmp_CH_Name());
    lcSbuff.append("; ");
    }
    In_Out_Date iod = new In_Out_Date();
    StringBuffer iodSbuff = new StringBuffer();
    for(int i=0;i<ioList.size();i++){
    iod = ioList.get(i);
    iodSbuff.append(iod.getEmp_CH_Name());
    iodSbuff.append("; ");
    }
    message.setText("護照即將到期﹕"+pasSbuff+". \r\n"+"台胞証/簽証即將到期﹕"+tcSbuff+". \r\n"+"暫住証即將到期﹕"+lcSbuff+". \r\n"+"近兩天入境人員﹕"+iodSbuff+". \r\n"+"詳情請進入系統查看。地址﹕http://192.168.12.22:8000/manpower","utf-8");
    // 所有以上的工作必須保存。
    message.saveChanges();
    // 發送,利用Transport類,它是SMTP的郵件發送協議,
    javax.mail.Transport.send(message);
    }catch (Exception exc){
    exc.printStackTrace();
    }