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

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

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

    我的漫漫程序之旅

    專注于JavaWeb開發
    隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
    數據加載中……

    JavaMail發送郵件的一個例子(全)

     

    package mail;

    import java.util.Date;
    import java.util.Properties;
    import javax.mail.Address;
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;

    /**
     * 發送普通郵件,接受普通郵件 發送帶有附件的郵件,接收帶有附件的郵件 發送html形式的郵件,接受html形式的郵件 發送帶有圖片的郵件等做了一個總結。
     
    */

    public class Test
    {
        
    // 郵箱服務器
        private String host = "smtp.163.com";
        
    // 這個是你的郵箱用戶名
        private String username = "******";
        
    // 你的郵箱密碼
        private String password = "******";
        
        
    private String mail_head_name = "this is head of this mail";

        
    private String mail_head_value = "this is head of this mail";

        
    private String mail_to = "zdw@live.cn";

        
    private String mail_from = "*****@163.com";

        
    private String mail_subject = "this is the subject of this test mail";

        
    private String mail_body = "this is the mail_body of this test mail";

        
    private String personalName = "我的郵件";

        
    public Test()
        
    {
        }


        
    /**
         * 此段代碼用來發送普通電子郵件
         
    */

        
    public void send() throws Exception
        
    {
            
    try
            
    {
                Properties props 
    = new Properties(); // 獲取系統環境
                Authenticator auth = new Email_Autherticator(); // 進行郵件服務器用戶認證
                props.put("mail.smtp.host", host);
                props.put(
    "mail.smtp.auth""true");
                Session session 
    = Session.getDefaultInstance(props, auth);
                
    // 設置session,和郵件服務器進行通訊。
                MimeMessage message = new MimeMessage(session);
                
    // message.setContent("foobar, "application/x-foobar"); // 設置郵件格式
                message.setSubject(mail_subject); // 設置郵件主題
                message.setText(mail_body); // 設置郵件正文
                message.setHeader(mail_head_name, mail_head_value); // 設置郵件標題
                message.setSentDate(new Date()); // 設置郵件發送日期
                Address address = new InternetAddress(mail_from, personalName);
                message.setFrom(address); 
    // 設置郵件發送者的地址
                Address toAddress = new InternetAddress(mail_to); // 設置郵件接收方的地址
                message.addRecipient(Message.RecipientType.TO, toAddress);
                Transport.send(message); 
    // 發送郵件
                System.out.println("send ok!");
            }
     catch (Exception ex)
            
    {
                ex.printStackTrace();
                
    throw new Exception(ex.getMessage());
            }

        }


        
    /**
         * 用來進行服務器對用戶的認證
         
    */

        
    public class Email_Autherticator extends Authenticator
        
    {
            
    public Email_Autherticator()
            
    {
                
    super();
            }


            
    public Email_Autherticator(String user, String pwd)
            
    {
                
    super();
                username 
    = user;
                password 
    = pwd;
            }


            
    public PasswordAuthentication getPasswordAuthentication()
            
    {
                
    return new PasswordAuthentication(username, password);
            }

        }


        
    public static void main(String[] args)
        
    {
            Test sendmail 
    = new Test();
            
    try
            
    {
                sendmail.send();
            }
     catch (Exception ex)
            
    {
            }

        }


    }


    經測試在126,163,sina上成功.

    posted on 2007-12-24 17:45 々上善若水々 閱讀(15818) 評論(12)  編輯  收藏 所屬分類: JavaWeb

    評論

    # re: JavaMail發送郵件的一個例子(全)  回復  更多評論   

    "main" java.lang.NoClassDefFoundError: com/sun/mail/util/BEncoderStream
    報這錯呢?
    2008-09-03 09:32 | ypf8312

    # re: JavaMail發送郵件的一個例子(全)  回復  更多評論   

    請確保你的jar包已經加全。
    2008-09-03 11:00 | 々上善若水々

    # re: JavaMail發送郵件的一個例子(全)  回復  更多評論   

    "main" java.lang.NoClassDefFoundError: com/sun/mail/util/BEncoderStream
    報這錯呢?樓主這個錯誤如何解決呀.
    2008-09-05 15:31 | yxl

    # re: JavaMail發送郵件的一個例子(全)  回復  更多評論   

    @yxl
    main" java.lang.NoClassDefFoundError: com/sun/mail/util/BEncoderStream
    報這錯呢?樓主這個錯誤
    主要還是你的包沒有導入啊,導入就好了
    放到lib文件夾下
    Q我吧;332920289
    2009-01-07 17:28 | 清晨

    # re: JavaMail發送郵件的一個例子(全)[未登錄]  回復  更多評論   

    1111
    2011-08-29 11:13 | 111

    # re: JavaMail發送郵件的一個例子(全)  回復  更多評論   

    java程序員之家 QQ群號 2218986
    2011-12-01 11:20 | 黃振

    # re: JavaMail發送郵件的一個例子(全)  回復  更多評論   

    sdsdsds
    2012-01-04 11:11 | liuyilingwangyi@163.com

    # re: JavaMail發送郵件的一個例子(全)  回復  更多評論   

    @liuyilingwangyi@163.com
    2012-01-04 11:15 | liuyilingwangyi@163.com

    # re: JavaMail發送郵件的一個例子(全)[未登錄]  回復  更多評論   

    lz v5 很好使啊
    2012-01-13 20:47 | test

    # re: JavaMail發送郵件的一個例子(全)  回復  更多評論   

    樓主你好,我的郵件可以發過去,但是報錯了javax.mail.MessagingException: Can't send command to SMTP host;你遇到過沒
    2012-06-08 09:23 | 張萬勝

    # re: JavaMail發送郵件的一個例子(全)  回復  更多評論   

    sだあsd
    2013-07-08 08:55 | dsas

    # re: JavaMail發送郵件的一個例子(全)  回復  更多評論   

    sdfg
    2013-07-08 12:52 | gsd
    主站蜘蛛池模板: 亚洲av无码国产综合专区 | 午夜亚洲国产成人不卡在线| 国产午夜亚洲精品不卡电影| 亚洲综合伊人久久综合| www视频免费看| 免费国产黄网站在线观看动图 | 狠狠色伊人亚洲综合成人| 免费在线看v网址| 一级黄色毛片免费看| 亚洲日本香蕉视频| 亚洲精品456播放| 黄页网站在线观看免费高清| 美女18毛片免费视频| 亚洲视频网站在线观看| 免费国产高清视频| 亚洲精品在线免费观看视频| 黄色一级视频免费| 亚洲乱码无限2021芒果| 亚洲午夜av影院| 成人免费无码大片a毛片| a级毛片黄免费a级毛片| 色噜噜的亚洲男人的天堂| 久久夜色精品国产噜噜亚洲AV| 国产猛烈高潮尖叫视频免费| 久久国产乱子伦免费精品| 国产午夜亚洲精品不卡电影| 亚洲va精品中文字幕| 国产aⅴ无码专区亚洲av| 免费一级毛片不卡不收费| 国内精品乱码卡1卡2卡3免费| 中文字幕免费在线看线人动作大片| 亚洲高清一区二区三区| 亚洲第一精品在线视频| 在线精品亚洲一区二区三区| 免费无码又爽又刺激毛片| **真实毛片免费观看| 国产精品区免费视频| a毛片全部播放免费视频完整18| 国产一区二区三区亚洲综合 | 十八禁在线观看视频播放免费| 精品国产亚洲一区二区三区在线观看|