發送電子郵件


    //發送電子郵件的方法
    private void SendEmail(string mailServerName, string from, string password, string to, string subject, string body)
    {
        try
        {

            //編碼暫硬性規定爲GB2312
            Encoding encoding = Encoding.GetEncoding("GB2312");
            MailMessage Message = new MailMessage(
                    new MailAddress(from, "XX", encoding),//第一個是發信人的地址,第二個參數是發信人
            new MailAddress(to));//收信人郵箱
            Message.SubjectEncoding = encoding;
            Message.Subject = subject;//標題
            Message.BodyEncoding = encoding;
            Message.Body = body; //body
            SmtpClient smtpClient = new SmtpClient(mailServerName);//信箱服務器
            smtpClient.Credentials = new NetworkCredential(from, password);//信箱的用戶名和密碼
            smtpClient.Timeout = 999999;
            smtpClient.Send(Message);
        }
        catch (FormatException ex)
        {
            Console.WriteLine(ex.Message);
        }
        catch (SmtpException ex)
        {
            Console.WriteLine(ex.Message);
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章