<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    少年阿賓

    那些青春的歲月

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
    郵件發(fā)送和接收已經(jīng)全部實(shí)現(xiàn),大家在轉(zhuǎn)發(fā)市請注明出處(我這也是參考了N多大牛的作品):

    //發(fā)送郵件類:

    import java.util.Properties;

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

    public class SendMail {
        
        public boolean Send(MailInfo mailInfo)throws Exception{
            Properties props=new Properties();
            //設(shè)置發(fā)送郵件的郵件服務(wù)器的屬性(這里使用網(wǎng)易的smtp服務(wù)器)
            props.put("mail.smtp.host", mailInfo.getHost());
            //需要經(jīng)過授權(quán),也就是有戶名和密碼的校驗(yàn),這樣才能通過驗(yàn)證(一定要有這一條)
            props.put("mail.smtp.auth", mailInfo.getValidate());
            Session session=Session.getDefaultInstance(props);// 用剛剛設(shè)置好的props對象構(gòu)建一個session
            session.setDebug(true);
            MimeMessage message=new MimeMessage(session);
            try{
                message.setFrom(new InternetAddress(mailInfo.getFromAddress()));
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailInfo.getToAddress()));
                message.setSubject(mailInfo.getSubject());
                message.setSentDate(mailInfo.getSentdate());
                //向multipart對象中添加郵件的各個部分內(nèi)容,包括文本內(nèi)容和附件
                Multipart multipart=new MimeMultipart();
                BodyPart contentPart=new MimeBodyPart();
                contentPart.setText(mailInfo.getContent());
                multipart.addBodyPart(contentPart);
                //添加附件
                if(mailInfo.getFilepath()!=null){
                    BodyPart messageBodyPart=new MimeBodyPart();
                    DataSource source=new FileDataSource(mailInfo.getFilepath());
                    
                    messageBodyPart.setDataHandler(new DataHandler(source));
                    sun.misc.BASE64Encoder enc=new sun.misc.BASE64Encoder();
                    String fname=mailInfo.getFilename();
                    if(fname!=null){
                        messageBodyPart.setFileName("=?GBK?B?"+enc.encode(fname.getBytes())+"?=");
                //        messageBodyPart.setFileName(MimeUtility.encodeText(fname));
                        multipart.addBodyPart(messageBodyPart);
                    }else{
                        String lname=mailInfo.getFilepath().substring(mailInfo.getFilepath().lastIndexOf("\\")+1, mailInfo.getFilepath().length());
                        System.out.println(lname);
                        messageBodyPart.setFileName("=?GBK?B?"+enc.encode(lname.getBytes())+"?=");
                //        messageBodyPart.setFileName(MimeUtility.encodeText(lname));
                        multipart.addBodyPart(messageBodyPart);
                    }
                    
                }

                
                message.setContent(multipart);//text/plain表示純文本內(nèi)容
                message.saveChanges();
                Transport transport=session.getTransport("smtp");
                transport.connect(mailInfo.getHost(),mailInfo.getUsername(),mailInfo.getPassword());
                transport.sendMessage(message,message.getAllRecipients());
                transport.close();
                return true;
            }catch(Exception e){
                e.printStackTrace();
            }
            
            return false;
        }
    }



    //測試郵件發(fā)送類
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;

    public class SendTest {

        @Before
        public void setUp() throws Exception {
        }

        @After
        public void tearDown() throws Exception {
        }
        @Test
        public void mailTest(){
            SendMail mail=new SendMail();
            MailInfo mailInfo=new MailInfo();
            mailInfo.setContent("郵件內(nèi)容44444444444444444444");
            mailInfo.setFilepath("E:\\Emotion\\Photo\\乳溝.jpg");
            String fname=mailInfo.getFilepath().substring(mailInfo.getFilepath().lastIndexOf("."), mailInfo.getFilepath().length());
            System.out.println(fname);
    //        mailInfo.setFilename("希望"+fname);
            mailInfo.setFromAddress("leeposter@163.com");
            mailInfo.setHost("smtp.163.com");
            mailInfo.setSubject("郵件主題444444444444444444444");
            mailInfo.setToAddress("liposter@163.com");
            mailInfo.setUsername("leeposter");
            mailInfo.setValidate("true");
            mailInfo.setPassword("*********");
            try{
                boolean flag=mail.Send(mailInfo);
                if(flag==true){
                    System.out.println("發(fā)送成功");
                }else{
                    System.out.println("發(fā)送失敗");
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            
            
        }

    }
    posted on 2011-10-17 23:56 abin 閱讀(1860) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲欧洲免费无码| 四虎国产精品免费久久| 国产精品免费视频一区| 国产成人精品日本亚洲直接| 最好看的中文字幕2019免费| 久久国产亚洲高清观看| 色猫咪免费人成网站在线观看| 亚洲成AV人片在| 午夜老司机永久免费看片| 亚洲综合在线观看视频| 永久免费在线观看视频| 亚洲国产精品综合福利专区| 日本免费人成在线网站| 亚洲日韩国产精品乱-久| 日韩一级免费视频| 日韩a毛片免费观看| 亚洲精品自在在线观看| 69精品免费视频| 亚洲欧洲无码一区二区三区| 全黄a免费一级毛片人人爱| 中文字幕在线免费看| 亚洲午夜精品一区二区 | 黄色网站软件app在线观看免费| 亚洲精品卡2卡3卡4卡5卡区| 日韩免费人妻AV无码专区蜜桃| 亚洲免费电影网站| 免费人成在线观看播放国产| 丝瓜app免费下载网址进入ios| 亚洲精品美女视频| 国产成人高清精品免费软件| 精品久久久久久国产免费了| 亚洲黄色在线网站| 国产免费资源高清小视频在线观看| 中文字幕在线观看免费| 亚洲AV综合色区无码二区偷拍| 免费人成视频在线观看视频| 久草视频在线免费看| 亚洲日本一线产区和二线| 国产成人亚洲精品狼色在线| 国产成人免费高清激情明星| 免费激情网站国产高清第一页 |