//插入新User private void InertUsers(SendPerson sp) { if (!CheckAccount(sp.UAccount)) { MessageBox.Show("该账号已存在,请重新输入!"); return; } string sql = @"insert into Users(uAccount,uPwd,uDomain,uName,uEmail,uServiceName,uServiceFullName) values(@uAccount,@uPwd,@uDomain,@uName,@uEmail,@uServiceName,@uServiceFullName)"; OleDbParameter[] oPara = { new OleDbParameter("@uAccount", sp.UAccount) , new OleDbParameter("@uPwd", sp.UPwd) , new OleDbParameter("@uDomain", sp.UDomain) , new OleDbParameter("@uName", sp.UName) , new OleDbParameter("@uEmail", sp.UEmail) , new OleDbParameter("@uServiceName", sp.UServiceName) , new OleDbParameter("@uServiceFullName", sp.UServiceFullName) }; try { AccessHelper.ExecuteNonQuery(sql, oPara); MessageBox.Show("保存成功!"); LoadAccount(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } }
//保存发件人信息到xml按钮事件 private void btnSave_Click(object sender, EventArgs e) { #region 序列化SendPerson对象 ////序列化SendPerson对象 //using (FileStream fs = new FileStream("SendPerson.xml", FileMode.Create, FileAccess.Write)) //{ // XmlSerializer xs = new XmlSerializer(typeof(SendPerson)); // xs.Serialize(fs, sp); //} #endregion if (CheckSendPerson()) { SendPerson sp = ControlsToPerson(); #region 保存数据到Access数据库 if (string.IsNullOrEmpty(txtID.Text)) { InertUsers(sp); } else { UpdateUsers(sp, Convert.ToInt32(txtID.Text)); } #endregion } }
/// <summary> /// 发送邮件方法 /// </summary> /// <param name="sp">发件人对象</param> /// <param name="contacts">收件人数组</param> /// <param name="subject">主题</param> /// <param name="content">内容</param> /// <param name="delay">延迟</param> private void SendEmail(SendPerson sp, string[] contacts, string subject, string content, int delay) { int temp = 0; for (int i = 0; i < contacts.Length; i++) { MailMessage mail = new MailMessage(); mail.From = new MailAddress(sp.UEmail, sp.UName); //原邮件地址,发件人 mail.To.Add(contacts[i]); //目的邮件地址,可以有多个人 mail.Subject = subject; //发送邮件的标题 mail.SubjectEncoding = Encoding.UTF8; mail.Body = content; //发送邮件内容 mail.BodyEncoding = Encoding.UTF8; mail.IsBodyHtml = true; //SMTP服务器 SmtpClient client = new SmtpClient(sp.UServiceFullName);//默认端口为25 //client.DeliveryMethod = SmtpDeliveryMethod.Network; //client.EnableSsl = true; //client.UseDefaultCredentials = true; client.Credentials = new NetworkCredential(sp.UAccount, sp.UPwd);//获取或者设置用户验证发件人的凭据 用户名和密码初始化 try { client.Send(mail); temp++; } catch (Exception ex) { continue; } SetCount(contacts.Length, temp, true); Thread.Sleep(delay); } SetCount(contacts.Length, temp, false); MessageBox.Show("发送完毕!"); }
//发送按钮事件 private void btnSend_Click(object sender, EventArgs e) { if (CheckInput()) { SendPerson sp = ControlsToPerson(); List <string> list = new List <string>(); for (int i = 0; i < clbContactList.Items.Count; i++) { if (clbContactList.GetItemChecked(i)) { list.Add(clbContactList.Items[i].ToString()); } } if (list.Count == 0) { MessageBox.Show("请选择收件人!"); return; } else { progressBar1.Maximum = list.Count; progressBar1.Value = 0; } string subject = txtProject.Text; string content = rtxtContent.Text; int delay = Convert.ToInt32(txtDelay.Text) * 1000; Thread thread = new Thread(delegate() { SendEmail(sp, list.ToArray(), subject, content, delay); }); thread.Name = "sendEmail"; thread.IsBackground = true; thread.Start(); } }
//sendperson对象添加到控件中 private void PersonToControls(SendPerson sp) { cmbEmailList.Text = sp.UAccount; txtPwd.Text = sp.UPwd; cmbDomain.Text = sp.UDomain; cmbService.Text = sp.UServiceName; txtName.Text = sp.UName; txtID.Text = sp.Id.ToString(); }
//输入添加到对象中 private SendPerson ControlsToPerson() { SendPerson sp = new SendPerson(); sp.UAccount = cmbEmailList.Text; sp.UDomain = cmbDomain.SelectedItem.ToString(); sp.UEmail = cmbEmailList.Text + cmbDomain.SelectedItem.ToString(); sp.UServiceName = cmbService.SelectedItem.ToString(); sp.UPwd = txtPwd.Text; sp.UName = txtName.Text; sp.UServiceFullName = (cmbDomain.SelectedItem as EmailSuper).GetServiceName(cmbService.SelectedItem.ToString()); return(sp); }
//将datatable转化成sendperson数组 private SendPerson[] DataTableToPerson(DataTable dt) { if (dt.Rows.Count == 0) { return(null); } List <SendPerson> list = new List <SendPerson>(); for (int i = 0; i < dt.Rows.Count; i++) { SendPerson sp = new SendPerson(); sp.Id = Convert.ToInt32(dt.Rows[i]["ID"]); sp.UAccount = dt.Rows[i]["uAccount"].ToString(); sp.UPwd = dt.Rows[i]["uPwd"].ToString(); sp.UDomain = dt.Rows[i]["uDomain"].ToString(); sp.UEmail = dt.Rows[i]["uEmail"].ToString(); sp.UName = dt.Rows[i]["uName"].ToString(); sp.UServiceName = dt.Rows[i]["uServiceName"].ToString(); sp.UServiceFullName = dt.Rows[i]["uServiceFullName"].ToString(); list.Add(sp); } return(list.ToArray()); }
//更新users private void UpdateUsers(SendPerson sp, int id) { string sql = @"update Users set uAccount=@uAccount,uPwd=@uPwd,uDomain=@uDomain,uName=@uName,uEmail=@uEmail,uServiceName=@uServiceName,uServiceFullName=@uServiceFullName where ID=@ID"; OleDbParameter[] oPara = { new OleDbParameter("@uAccount", sp.UAccount) , new OleDbParameter("@uPwd", sp.UPwd) , new OleDbParameter("@uDomain", sp.UDomain) , new OleDbParameter("@uName", sp.UName) , new OleDbParameter("@uEmail", sp.UEmail) , new OleDbParameter("@uServiceName", sp.UServiceName) , new OleDbParameter("@uServiceFullName", sp.UServiceFullName) , new OleDbParameter("@ID", id) }; try { AccessHelper.ExecuteNonQuery(sql, oPara); MessageBox.Show("保存成功!"); LoadAccount(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } }
//更新users private void UpdateUsers(SendPerson sp, int id) { string sql = @"update Users set uAccount=@uAccount,uPwd=@uPwd,uDomain=@uDomain,uName=@uName,uEmail=@uEmail,uServiceName=@uServiceName,uServiceFullName=@uServiceFullName where ID=@ID"; OleDbParameter[] oPara = {new OleDbParameter("@uAccount",sp.UAccount) ,new OleDbParameter("@uPwd",sp.UPwd) ,new OleDbParameter("@uDomain",sp.UDomain) ,new OleDbParameter("@uName",sp.UName) ,new OleDbParameter("@uEmail",sp.UEmail) ,new OleDbParameter("@uServiceName",sp.UServiceName) ,new OleDbParameter("@uServiceFullName",sp.UServiceFullName) ,new OleDbParameter("@ID",id) }; try { AccessHelper.ExecuteNonQuery(sql, oPara); MessageBox.Show("保存成功!"); LoadAccount(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } }
/// <summary> /// 发送邮件方法 /// </summary> /// <param name="sp">发件人对象</param> /// <param name="contacts">收件人数组</param> /// <param name="subject">主题</param> /// <param name="content">内容</param> /// <param name="delay">延迟</param> private void SendEmail(SendPerson sp, string[] contacts, string subject, string content, int delay) { int temp = 0; for (int i = 0; i < contacts.Length; i++) { MailMessage mail = new MailMessage(); mail.From = new MailAddress(sp.UEmail, sp.UName);//原邮件地址,发件人 mail.To.Add(contacts[i]);//目的邮件地址,可以有多个人 mail.Subject = subject;//发送邮件的标题 mail.SubjectEncoding = Encoding.UTF8; mail.Body = content;//发送邮件内容 mail.BodyEncoding = Encoding.UTF8; mail.IsBodyHtml = true; //SMTP服务器 SmtpClient client = new SmtpClient(sp.UServiceFullName);//默认端口为25 //client.DeliveryMethod = SmtpDeliveryMethod.Network; //client.EnableSsl = true; //client.UseDefaultCredentials = true; client.Credentials = new NetworkCredential(sp.UAccount, sp.UPwd);//获取或者设置用户验证发件人的凭据 用户名和密码初始化 try { client.Send(mail); temp++; } catch (Exception ex) { continue; } SetCount(contacts.Length, temp, true); Thread.Sleep(delay); } SetCount(contacts.Length, temp, false); MessageBox.Show("发送完毕!"); }
//插入新User private void InertUsers(SendPerson sp) { if (!CheckAccount(sp.UAccount)) { MessageBox.Show("该账号已存在,请重新输入!"); return; } string sql = @"insert into Users(uAccount,uPwd,uDomain,uName,uEmail,uServiceName,uServiceFullName) values(@uAccount,@uPwd,@uDomain,@uName,@uEmail,@uServiceName,@uServiceFullName)"; OleDbParameter[] oPara = {new OleDbParameter("@uAccount",sp.UAccount) ,new OleDbParameter("@uPwd",sp.UPwd) ,new OleDbParameter("@uDomain",sp.UDomain) ,new OleDbParameter("@uName",sp.UName) ,new OleDbParameter("@uEmail",sp.UEmail) ,new OleDbParameter("@uServiceName",sp.UServiceName) ,new OleDbParameter("@uServiceFullName",sp.UServiceFullName) }; try { AccessHelper.ExecuteNonQuery(sql, oPara); MessageBox.Show("保存成功!"); LoadAccount(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } }
//将datatable转化成sendperson数组 private SendPerson[] DataTableToPerson(DataTable dt) { if (dt.Rows.Count == 0) return null; List<SendPerson> list = new List<SendPerson>(); for (int i = 0; i < dt.Rows.Count; i++) { SendPerson sp = new SendPerson(); sp.Id = Convert.ToInt32(dt.Rows[i]["ID"]); sp.UAccount = dt.Rows[i]["uAccount"].ToString(); sp.UPwd = dt.Rows[i]["uPwd"].ToString(); sp.UDomain = dt.Rows[i]["uDomain"].ToString(); sp.UEmail = dt.Rows[i]["uEmail"].ToString(); sp.UName = dt.Rows[i]["uName"].ToString(); sp.UServiceName = dt.Rows[i]["uServiceName"].ToString(); sp.UServiceFullName = dt.Rows[i]["uServiceFullName"].ToString(); list.Add(sp); } return list.ToArray(); }
//输入添加到对象中 private SendPerson ControlsToPerson() { SendPerson sp = new SendPerson(); sp.UAccount = cmbEmailList.Text; sp.UDomain = cmbDomain.SelectedItem.ToString(); sp.UEmail = cmbEmailList.Text + cmbDomain.SelectedItem.ToString(); sp.UServiceName = cmbService.SelectedItem.ToString(); sp.UPwd = txtPwd.Text; sp.UName = txtName.Text; sp.UServiceFullName = (cmbDomain.SelectedItem as EmailSuper).GetServiceName(cmbService.SelectedItem.ToString()); return sp; }