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

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

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

    sunfruit[請(qǐng)?jiān)L問(wèn)http://www.fruitres.cn]

    --我相信JAVA能走得更遠(yuǎn) QQ:316228067

    [原創(chuàng)]用JAVAMAIL發(fā)送郵件的一個(gè)簡(jiǎn)單例子

        --sunfruit

        寫(xiě)了一個(gè)收發(fā)郵件的應(yīng)用程序[在列表里面可以看到]但是畢竟有些復(fù)雜,關(guān)鍵部分其實(shí)也就是幾行代碼,為了大家使用方便,我把發(fā)送郵件的代碼單獨(dú)拿了出來(lái),并且分為發(fā)送附件/不發(fā)送附件兩個(gè)方法,便于大家查看,只是什么設(shè)計(jì)啦,編程思想啦,等等就談不到了,呵呵,大家將就吧

        JDK版本
            1.4.x
        其   他
            JAVAMAIL相關(guān)包
        功能簡(jiǎn)介:
            簡(jiǎn)單的郵件發(fā)送功能,可以發(fā)送附件

        源代碼如下

    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;
    import java.util.*;
    import java.util.*;
    import java.text.*;
    import java.io.*;
    public class SendMail
    {
        //傳入的參數(shù)有密碼、姓名、誰(shuí)發(fā)、發(fā)給誰(shuí)、主題、正文內(nèi)容、smtp地址、附件文件路徑、附件的新文件名、發(fā)送類型(text/html)
        //發(fā)送郵件主函數(shù)
        public String sendmail(int myport,String password,String username,String myfrom,String myto,String mysubject,String mytext,String mysmtp,String[] filepath,String[] newfilename,String htmlandtext)
        {
                try{
                    int indexstr=0;
                    if (filepath[0]!=null && !filepath[0].equals(""))
                        indexstr=1;
                        //替換字符串
                   //     jbemail myjbemail=new jbemail();
                   //     filepath=myjbemail.myreplace(filepath,"\\","\\\\");
                   //     System.out.println("附件地址"+filepath+"服務(wù)器地址"+mysmtp+mysmtp.length());
                        //Properties props = new Properties();
                    Properties props = System.getProperties();
                    Session sendMailSession;
                    Store store;  //收郵件時(shí)使用
                    Transport transport;//發(fā)郵件時(shí)使用
                    props.put("mail.smtp.host",mysmtp);
                    props.put("mail.smtp.auth","true");
                    SmtpAuthenticator sa=new SmtpAuthenticator(username,password);
                    sendMailSession = Session.getInstance(props,sa);
                    //sendMailSession = Session.getInstance(props,null);
                    sendMailSession.setDebug(true);

                    MimeMessage newMessage = new MimeMessage(sendMailSession);
                    newMessage.setFrom(new InternetAddress(myfrom));
                    newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(myto));
                    //設(shè)定郵件格式
                    newMessage.setSentDate(new Date());
                    System.out.println(htmlandtext+"郵件正文格式");
                    Multipart multipart = new MimeMultipart();
                    if (htmlandtext.equals("text"))
                    {
                        //獲得文本格式的郵件

                        newMessage.setSubject(mysubject);
                        BodyPart messageBodyPart = new MimeBodyPart();
                        messageBodyPart.setText(mytext);
                        multipart.addBodyPart(messageBodyPart);
                    }
                    else if(htmlandtext.equals("html"))
                    {
                        //設(shè)置郵件內(nèi)容,將郵件body部分轉(zhuǎn)化為HTML格式
                        newMessage.setSubject(mysubject,"gb2312");
                        BodyPart messageBodyPart = new MimeBodyPart();
                        messageBodyPart.setDataHandler(new DataHandler(mytext,"text/html;charset=gb2312"));
                        multipart.addBodyPart(messageBodyPart);
                    }
                    if (indexstr>0)
                    {

                        for(int i=0;i                    {
                            if (newfilename[i]!=null)
                            {
                                //創(chuàng)建BodyPart對(duì)象以便獲得附件
                                BodyPart messageBodyPart = new MimeBodyPart();
                                System.out.println("附件地址"+filepath[i]);
                                DataSource source = new FileDataSource(filepath[i]);
                                messageBodyPart.setDataHandler(new DataHandler(source));
                                messageBodyPart.setFileName(newfilename[i]);
                                multipart.addBodyPart(messageBodyPart);
                            }
                        }
                    }
                    //將正文和附件添加到郵件中
                    newMessage.setContent(multipart);
                    newMessage.saveChanges();
                    //transport = sendMailSession.getStore("pop3");
                    transport = sendMailSession.getTransport("smtp");
                    transport.connect(mysmtp,myport,username,password);
                    //transport.connect();
                    transport.send(newMessage,newMessage.getAllRecipients());
                    System.out.println("成功發(fā)送到"+myto);
                    return "ok";
            }
            catch(MessagingException m)
            {
                    System.out.println(m.toString()+"失敗");
                    return myto;
            }
        }
        //不含發(fā)送附件的函數(shù)
        //傳入的參數(shù)有port地址、密碼、姓名、誰(shuí)發(fā)、發(fā)給誰(shuí)、主題、正文內(nèi)容、smtp地址、發(fā)送類型(text/html)
        public String sendmail(String mailPathlog,int myport,String password,String username,String myfrom,String myto,String mysubject,String mytext,String mysmtp,String htmlandtext)
        {
                try{
                    //解碼
                    mysubject=java.net.URLDecoder.decode(mysubject);
                        //Properties props = new Properties();
                    Properties props = System.getProperties();
                    Session sendMailSession;
                    Store store;  //收郵件時(shí)使用
                    Transport transport;//發(fā)郵件時(shí)使用
                    props.put("mail.smtp.host",mysmtp);
                    props.put("mail.smtp.auth","true");
                    SmtpAuthenticator sa=new SmtpAuthenticator(username,password);
                    //身份驗(yàn)證
                    sendMailSession = Session.getInstance(props,sa);
                    //sendMailSession = Session.getInstance(props,null);
                    sendMailSession.setDebug(true);
                    MimeMessage newMessage = new MimeMessage(sendMailSession);
                    try
                    {
                      newMessage.setFrom(new InternetAddress(myfrom,"法律之星"));
                      newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(myto));
                    }
                    catch(java.io.UnsupportedEncodingException ex)
                    {
                       System.out.println(ex.toString());
                    }
                    //設(shè)定郵件格式
                    newMessage.setSentDate(new Date());
                    System.out.println(htmlandtext+"郵件正文格式");
                    Multipart multipart = new MimeMultipart();
                    if (htmlandtext.equals("text"))
                    {
                        //獲得文本格式的郵件

                        newMessage.setSubject(mysubject);
                        BodyPart messageBodyPart = new MimeBodyPart();
                        messageBodyPart.setText(mytext);
                        multipart.addBodyPart(messageBodyPart);
                    }
                    else if(htmlandtext.equals("html"))
                    {
                        //設(shè)置郵件內(nèi)容,將郵件body部分轉(zhuǎn)化為HTML格式
                        newMessage.setSubject(mysubject,"gb2312");
                        BodyPart messageBodyPart = new MimeBodyPart();
                        messageBodyPart.setDataHandler(new DataHandler(mytext,"text/html;charset=gb2312"));
                        multipart.addBodyPart(messageBodyPart);
                    }
                    //將正文添加到郵件中
                    newMessage.setContent(multipart);
                    newMessage.saveChanges();
                    //transport = sendMailSession.getStore("pop3");
                    transport = sendMailSession.getTransport("smtp");
                    transport.connect(mysmtp,myport,username,password);
                    //transport.connect();
                    transport.send(newMessage,newMessage.getAllRecipients());
                    System.out.println("成功發(fā)送到"+myto+mytext);
                    return "ok";
            }
            catch(MessagingException m)
            {
                    System.out.println(m.toString()+"失敗");
                    //生成當(dāng)前日期
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                    Date dateTime= new Date();
                    String sDateTime=dateFormat.format(dateTime);
                    //生成日志文件
                    try
                    {
                        File filelog=new File(mailPathlog+"\\"+"mainlog.txt");
                        BufferedWriter out2=new BufferedWriter(new FileWriter(filelog.getPath(),true));
                        String newline = System.getProperty("line.separator");
                        out2.write(sDateTime+"/"+mysmtp+"/"+myfrom+"/"+myto+"/"+m.toString()+"/"+newline);
                        out2.close();
                    }
                    catch (IOException ex)
                    {
                        System.out.println(ex.toString());
                    }
                    return myto;
            }
        }
        class SmtpAuthenticator extends Authenticator
        {
            //SMTP身份驗(yàn)證
            public SmtpAuthenticator(String username,String password)
            {
                this.username=username;
                this.password=password;
            }
            public PasswordAuthentication getPasswordAuthentication()
            {
                return new  PasswordAuthentication(this.username,this.password);
            }
            String username=null;
            String password=null;
        }
    }

    posted on 2006-02-19 18:03 sunfruit 閱讀(1805) 評(píng)論(4)  編輯  收藏 所屬分類: JAVA SE & EE

    評(píng)論

    # re: [原創(chuàng)]用JAVAMAIL發(fā)送郵件的一個(gè)簡(jiǎn)單例子 2007-12-01 07:34 ghjk

    chinos putos

    y ojetes

    deberian de publicar algo mas claro

    esto no se entiende nada  回復(fù)  更多評(píng)論   

    # re: [原創(chuàng)]用JAVAMAIL發(fā)送郵件的一個(gè)簡(jiǎn)單例子 2007-12-01 07:36 ghjk

    郵件例程-JavaMail-發(fā)送HTML郵件
    郵件例程-JavaMail-發(fā)送HTML郵件
    郵件例程-JavaMail-發(fā)送HTML郵件
    郵件例程-JavaMail-發(fā)送HTML郵件
    郵件例程-JavaMail-發(fā)送HTML郵件   回復(fù)  更多評(píng)論   

    # re: [原創(chuàng)]用JAVAMAIL發(fā)送郵件的一個(gè)簡(jiǎn)單例子[未登錄](méi) 2011-06-15 13:21 wo

    sdasd  回復(fù)  更多評(píng)論   

    # re: [原創(chuàng)]用JAVAMAIL發(fā)送郵件的一個(gè)簡(jiǎn)單例子[未登錄](méi) 2011-06-15 13:22 wo

    asd  回復(fù)  更多評(píng)論   

    主站蜘蛛池模板: 国产成人AV片无码免费| 伊人久久国产免费观看视频| 午夜视频免费在线观看| 亚洲乱码国产一区三区| 黄色网站软件app在线观看免费| 亚洲老妈激情一区二区三区| 一级特黄录像免费播放肥| 国内精品久久久久久久亚洲| 手机看片国产免费永久| 亚洲伦理一区二区| 麻豆一区二区免费播放网站| 亚洲精品无码日韩国产不卡av| 日韩电影免费在线| 猫咪免费人成在线网站| 亚洲综合AV在线在线播放| 久久九九全国免费| avtt天堂网手机版亚洲| 日本免费人成视频播放| 一区二区免费在线观看| 色拍自拍亚洲综合图区| 毛片免费在线视频| 日韩毛片一区视频免费| 亚洲AV综合色区无码一区| 日韩欧毛片免费视频| 国产亚洲精品美女久久久久久下载| 亚洲人成电影在线播放| 在线日本高清免费不卡| 亚洲精品无码久久久久牙蜜区| 亚洲熟妇少妇任你躁在线观看无码 | 59pao成国产成视频永久免费| 亚洲国产成人精品无码区在线秒播 | 亚洲国产精品lv| 中文字幕影片免费在线观看| 青草青草视频2免费观看| 亚洲av一综合av一区| 嫩草影院免费观看| 在线毛片片免费观看| 亚洲精品无码av片| 亚洲bt加勒比一区二区| 破了亲妺妺的处免费视频国产 | 免费视频精品一区二区|