Пример #1
0
        //發Mail
        private void btnSend_Click(object sender, EventArgs e)
        {                                                 //http://opendata2.epa.gov.tw/UV/UV.json
            lblMsg.Text = "";
            List <string> receiver = new List <string>(); //收件人
            List <string> attach   = new List <string>(); //附檔

            //處理收件人
            if (txtReceiver.Text.Length > 0)
            {
                string[] temp = txtReceiver.Text.Replace(Environment.NewLine, ",").Split(',');
                foreach (var t in temp)
                {
                    if (!string.IsNullOrEmpty(t))
                    {
                        if (CheckMailFormat(@"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]+$", t))
                        {
                            receiver.Add(t);
                        }
                    }
                }
            }
            else
            {
                lblMsg.Text = "不填收件人是要我寄給好兄弟唷?";
                return;
            }

            //檢查是否有輸入json網址 有的話就去抓資料下來轉成excel
            if (txtUrl.Text.Length > 0)
            {
                bool check = CheckMailFormat(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?", txtUrl.Text);
                if (check == false)
                {
                    lblMsg.Text = "網址格式不正確";
                    return;
                }
                ExcelUrlService temp = new ExcelUrlService();
                //產生Excel
                try
                {
                    temp.GenerateExcel(txtUrl.Text);
                }
                catch (Exception ex)
                {
                    ErrorService.WriteLog("產生excel失敗" + ex.ToString());
                    lblMsg.Text = "產生excel失敗";
                }
                attach.Add(Application.StartupPath + "/File/file.xls");
            }
            //有加附加檔案
            if (!String.IsNullOrEmpty(txtFileName.Text))
            {
                attach.Add(txtFileName.Text);
            }

            try
            {
                mailservice.SendGMail("拉拉拉這是測試信", receiver, attach);
            }
            catch (Exception ex)
            {
                ErrorService.WriteLog("寄信失敗" + ex.Message);
                lblMsg.Text = "寄信失敗";
            }

            lblMsg.Text = "發送成功";
        }