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

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

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

    少年阿賓

    那些青春的歲月

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

    //發送郵件類:

    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();
            //設置發送郵件的郵件服務器的屬性(這里使用網易的smtp服務器)
            props.put("mail.smtp.host", mailInfo.getHost());
            //需要經過授權,也就是有戶名和密碼的校驗,這樣才能通過驗證(一定要有這一條)
            props.put("mail.smtp.auth", mailInfo.getValidate());
            Session session=Session.getDefaultInstance(props);// 用剛剛設置好的props對象構建一個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對象中添加郵件的各個部分內容,包括文本內容和附件
                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表示純文本內容
                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;
        }
    }



    //測試郵件發送類
    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("郵件內容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("發送成功");
                }else{
                    System.out.println("發送失敗");
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            
            
        }

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

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


    網站導航:
     
    主站蜘蛛池模板: 中文字幕亚洲综合小综合在线| 亚洲乱妇老熟女爽到高潮的片 | 免费看黄福利app导航看一下黄色录像| 可以免费观看一级毛片黄a| 中文字幕av免费专区| 亚洲欧洲日本国产| 波多野结衣免费视频观看| 国产色无码精品视频免费| 亚洲人成网网址在线看| 亚洲av中文无码乱人伦在线r▽ | 成年女人毛片免费视频| 免费国产在线精品一区| 亚洲酒色1314狠狠做| 免费国产怡红院在线观看| 久久国产精品萌白酱免费| 亚洲天然素人无码专区| 日本亚洲视频在线| 日韩一区二区在线免费观看| 国产精品白浆在线观看免费| 亚洲AV无码一区二区三区性色 | 韩国免费A级毛片久久| 国产成人精品日本亚洲18图| 国产亚洲精品久久久久秋霞| 丁香花在线观看免费观看| a级大片免费观看| 亚洲国产欧洲综合997久久| 四虎免费大片aⅴ入口| 国产真人无码作爱视频免费 | 在线精品一卡乱码免费| 亚洲精品在线免费看| 亚洲精品国产电影| 在线观看的免费网站| 免费观看在线禁片| 成人午夜影视全部免费看| 亚洲无人区码一二三码区别图片 | 可以免费看黄的网站| 国产日韩AV免费无码一区二区 | 国产国拍精品亚洲AV片| 宅男666在线永久免费观看| 国产黄色免费网站| 免费无码一区二区三区蜜桃|