發郵件方法

 public int SendMail(MailAccount mailAccount)
        {
            //查找本賬戶的待發郵件
            var query = from m in liWaitMail
                        where m.MailAccountId == mailAccount.id
                        select m;
            MailServer _currentMailServer = new EMailManage.Model.MailServer();
            MailAccountManage mam = new MailAccountManage();
            AttachmentsManage atm = new AttachmentsManage();
            DataTable data = mam.GetTable(Convert.ToInt32(mailAccount.id));


            string login = data.Rows[0]["EmailAccount"].ToString();
            string password = mailAccount.EmailPass;
            string smtphost = data.Rows[0]["SmtpHost"].ToString();
            int smtpport = Convert.ToInt32(data.Rows[0]["SmtpPort"]);
            bool smtpssl = Convert.ToBoolean(data.Rows[0]["SmtpSSL"]);
            SmtpClient client = new SmtpClient(smtphost, smtpport);
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(login, password);
            client.EnableSsl = smtpssl;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;


           
            while (query.Count() > 0) 
            {
                #region  初始化郵件基礎信息
                Mail sm = query.First();
               // 系統配置中最大連接次數
                ConfigManage cmm = new ConfigManage();
                Config config = cmm.GetOne("SendEmailMaxNo");
                 // 修改郵件發送連接次數
                int connectno = sm.ConnectNo + 1;
                mM.UpdateConnectNo(sm.id, connectno);


                List<Attachments> _currentAttachmentlist = new List<Attachments>();




                _currentAttachmentlist = atm.GetAttachmentsList(Convert.ToInt32(sm.id));


                String serverPhysicalPath = Config.ServerPhysicalPath;
                #endregion


                string[] toadress = sm.ToAddress.Split(';');
                MailMessage message = new MailMessage();
                message.From = new MailAddress(login);


                foreach (string toson in toadress)
                {
                    if (!Regex.IsMatch(toson, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
                    {
                        continue;
                    }
                    else
                    {
                        MailAddress to = new MailAddress(toson);
                        message.To.Add(to);
                    }
                }


                //主題
                message.Subject = sm.Title;
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = sm.HTMLFormat;
                string[] cc = sm.CCAddress.Split(';');
                foreach (string cson in cc)
                {
                    if (!Regex.IsMatch(cson, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
                    {
                        continue;
                    }
                    else
                    {
                        message.CC.Add(new MailAddress(cson));
                    }
                }
                //內容
                message.Body = sm.Body.Replace("\\\"", "");
                message.Body = message.Body.Replace("\\r\\n", "<br/>");
                message.Body = message.Body.Replace("\\t", "");


                message.BodyEncoding = System.Text.Encoding.UTF8;
                //在有附件的情況下添加附件
                foreach (Attachments item in _currentAttachmentlist)
                {
                    Attachment attachFile = null;
                    // string url=Server.MapPath(serverPhysicalPath) + "\\"+item.Url;//注意:URL要重新組合
                    //string url = System.Web.HttpContext.Current.Server.MapPath("~/Attachments") + "\\" + serverPhysicalPath + "\\" + item.Url;
                    string url = serverPhysicalPath + "\\" + item.Url.ToString().Replace("/", "\\");


                    attachFile = new Attachment(url);
                    message.Attachments.Add(attachFile);
                }
                try
                {
                    client.Send(message);


                    mM.UpdateSendStatus(sm.id,3);


                    //移除隊列
                    liWaitMail.Remove(sm);
                }
                catch (Exception ex)
                {
                    new Sys.Model.SysException("M", "發送郵件時發生異常", ex);


                    if (connectno == Convert.ToInt32(config.Value))
                    {
                        mM.UpdateSendStatus(sm.id, 4);
                        liWaitMail.Remove(sm);
                    }
                }
                finally
                {
                    //及時釋放佔用的資源
                    message.Dispose();
                } 
                
            }
            
            return 0;
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章