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

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

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

    sunfruit[請訪問http://www.fruitres.cn]

    --我相信JAVA能走得更遠 QQ:316228067

    [原創]用JAVAMAIL發送郵件的一個簡單例子

        --sunfruit

        寫了一個收發郵件的應用程序[在列表里面可以看到]但是畢竟有些復雜,關鍵部分其實也就是幾行代碼,為了大家使用方便,我把發送郵件的代碼單獨拿了出來,并且分為發送附件/不發送附件兩個方法,便于大家查看,只是什么設計啦,編程思想啦,等等就談不到了,呵呵,大家將就吧

        JDK版本
            1.4.x
        其   他
            JAVAMAIL相關包
        功能簡介:
            簡單的郵件發送功能,可以發送附件

        源代碼如下

    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
    {
        //傳入的參數有密碼、姓名、誰發、發給誰、主題、正文內容、smtp地址、附件文件路徑、附件的新文件名、發送類型(text/html)
        //發送郵件主函數
        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+"服務器地址"+mysmtp+mysmtp.length());
                        //Properties props = new Properties();
                    Properties props = System.getProperties();
                    Session sendMailSession;
                    Store store;  //收郵件時使用
                    Transport transport;//發郵件時使用
                    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));
                    //設定郵件格式
                    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"))
                    {
                        //設置郵件內容,將郵件body部分轉化為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)
                            {
                                //創建BodyPart對象以便獲得附件
                                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("成功發送到"+myto);
                    return "ok";
            }
            catch(MessagingException m)
            {
                    System.out.println(m.toString()+"失敗");
                    return myto;
            }
        }
        //不含發送附件的函數
        //傳入的參數有port地址、密碼、姓名、誰發、發給誰、主題、正文內容、smtp地址、發送類型(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;  //收郵件時使用
                    Transport transport;//發郵件時使用
                    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);
                    try
                    {
                      newMessage.setFrom(new InternetAddress(myfrom,"法律之星"));
                      newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(myto));
                    }
                    catch(java.io.UnsupportedEncodingException ex)
                    {
                       System.out.println(ex.toString());
                    }
                    //設定郵件格式
                    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"))
                    {
                        //設置郵件內容,將郵件body部分轉化為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("成功發送到"+myto+mytext);
                    return "ok";
            }
            catch(MessagingException m)
            {
                    System.out.println(m.toString()+"失敗");
                    //生成當前日期
                    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身份驗證
            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 閱讀(1793) 評論(4)  編輯  收藏 所屬分類: JAVA SE & EE

    評論

    # re: [原創]用JAVAMAIL發送郵件的一個簡單例子 2007-12-01 07:34 ghjk

    chinos putos

    y ojetes

    deberian de publicar algo mas claro

    esto no se entiende nada  回復  更多評論   

    # re: [原創]用JAVAMAIL發送郵件的一個簡單例子 2007-12-01 07:36 ghjk

    郵件例程-JavaMail-發送HTML郵件
    郵件例程-JavaMail-發送HTML郵件
    郵件例程-JavaMail-發送HTML郵件
    郵件例程-JavaMail-發送HTML郵件
    郵件例程-JavaMail-發送HTML郵件   回復  更多評論   

    # re: [原創]用JAVAMAIL發送郵件的一個簡單例子[未登錄] 2011-06-15 13:21 wo

    sdasd  回復  更多評論   

    # re: [原創]用JAVAMAIL發送郵件的一個簡單例子[未登錄] 2011-06-15 13:22 wo

    asd  回復  更多評論   

    主站蜘蛛池模板: 在线观看国产情趣免费视频| 亚洲av永久无码精品网站| 成人在线免费视频| 亚洲国产成人片在线观看| 免费观看国产网址你懂的| 国产精品亚洲精品久久精品| 国产黄色一级毛片亚洲黄片大全| 四虎国产成人永久精品免费| 久久综合久久综合亚洲| 亚洲国产日韩成人综合天堂| 久久w5ww成w人免费| 亚洲精品av无码喷奶水糖心| 国产精品亚洲αv天堂无码| 日本免费xxxx| jizz18免费视频| 亚洲天堂2017无码中文| 中文字幕精品亚洲无线码二区| 亚洲一区在线免费观看| 一级毛片a免费播放王色| 亚洲午夜久久久久久尤物| 亚洲午夜av影院| 成人au免费视频影院| 久久精品一区二区免费看| 精品久久久久久亚洲中文字幕| 精品亚洲成a人片在线观看 | jizzjizz亚洲| 亚洲一区在线免费观看| 国产精品玖玖美女张开腿让男人桶爽免费看 | 亚洲阿v天堂在线2017免费| 亚洲婷婷天堂在线综合| 国产亚洲欧洲Aⅴ综合一区| 成年在线网站免费观看无广告 | 国产在线19禁免费观看国产| 91福利视频免费| 91在线视频免费观看| 亚洲AV无码一区二区三区网址| 亚洲免费网站在线观看| 久久伊人久久亚洲综合| 精品国产人成亚洲区| 亚洲AV蜜桃永久无码精品| 天天操夜夜操免费视频|