⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmmain.cs

📁 java编写的邮件群发软件
💻 CS
📖 第 1 页 / 共 3 页
字号:
        {
            try
            {
                SHFILEINFO info = new SHFILEINFO();
                int cbFileInfo = Marshal.SizeOf(info);
                SHGFI flags;

                flags = SHGFI.SHGFI_ICON | SHGFI.SHGFI_LARGEICON | SHGFI.SHGFI_ATTRIBUTES | SHGFI.SHGFI_EXETYPE;

                SHGetFileInfo(strPath, 32 | 0, ref  info, (uint)cbFileInfo, flags);
                return Icon.FromHandle(info.hIcon);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return null;
            }
        }
        #endregion

        #region 添加附件菜单
        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnAddUpfile_Click(null, null);
        }

        private void 删除DToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (lvMailUpfile.Items.Count > 0)
                {
                    StaticInfo.attArray.RemoveAt(lvMailUpfile.SelectedItems[0].Index);
                    lvMailUpfile.Items.RemoveAt(lvMailUpfile.SelectedIndices[0]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (lvMailUpfile.SelectedItems[0].ToString() != "")
                {
                    int length = lvMailUpfile.SelectedItems[0].ToString().Length;
                    string ss = this.lvMailUpfile.SelectedItems[0].ToString().Substring(15, length - 16);
                    ShellExecute(IntPtr.Zero, "Open", ss, null, null, 0);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //EntryPoint 给出Dll入口点的名称,如果没有给出,则用方法本身的名称。
        [DllImport("shell32.dll", EntryPoint = "ShellExecute")]

        private static extern int ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);

        private void 大图标ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lvMailUpfile.View = View.LargeIcon;
        }

        private void 小图标ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lvMailUpfile.View = View.SmallIcon;
        }

        private void 列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lvMailUpfile.View = View.List;
        }
        #endregion

        #region 发送邮件
        private void btnStartSend_Click(object sender, EventArgs e)
        {
            StaticInfo.mailTitle = cbMailTitle.Text.Trim();
            StaticInfo.mailContent = rtbMailContent.Text.Trim();
            StaticInfo.toMails = new ArrayList();

            if (StaticInfo.mailTitle == "" || StaticInfo.mailContent == "")
            {
                ShowMsgBox("请填写或选择邮件!");
                return;
            }

            if (chkIsUseGroup.Checked)
            {
                if (Validator.StrToInt(cbSendMailGroup.SelectedValue.ToString(), 0) > 0)
                {
                    doh.Reset();
                    doh.SqlCmd = "select mail from xk_toMails where groupId=" + cbSendMailGroup.SelectedValue.ToString();
                    DataTable dt = doh.GetDataTable();
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                            StaticInfo.toMails.Add(dt.Rows[i][0].ToString());
                    }
                    else
                    {
                        ShowMsgBox("您选择的邮件组中没有任何地址,请先添加!");
                        return;
                    }
                }
                else
                {
                    ShowMsgBox("请先设置邮件组!");
                    return;
                }
            }
            else
            {
                if (txtReceive.Text.Trim() == "")
                {
                    ShowMsgBox("请填写收件人!");
                    return;
                }
                string[] tem = txtReceive.Text.Split(',');
                for (int i = 0; i < tem.Length; i++)
                    StaticInfo.toMails.Add(tem[i]);
            }

            btnStartSend.Enabled = false;

            statMail.Text = "准备发送...";

            currentThread = 0;
            currentTask = 0;
            sendtimes = 0;
            hasErr = false;
            logs = new ArrayList();
            logs.Add(DateTime.Now.ToString() + "的发送任务------------------------------------------");

            int ts = (int)numTreads.Value;
            if (ts > StaticInfo.toMails.Count) ts = StaticInfo.toMails.Count;
            thread = new Thread[ts];
            totalTask = StaticInfo.toMails.Count * sendMailList.Items.Count;
            timer1.Start();
            process = 0;

            for (int i = 0; i < thread.Length; i++)
            {
                thread[i] = new Thread(SendMail);
                thread[i].IsBackground = true;
                thread[i].Name = currentTask + "," + currentThread;
                thread[i].Start();
                currentThread++;
            }
        }

        private void SendMail()
        {
            sendtimes++;
            string[] ftName = Thread.CurrentThread.Name.Split(',');
            Console.WriteLine(Thread.CurrentThread.Name);
            if (ftName == null || ftName.Length < 2)
                ftName = new string[2] { currentTask.ToString(), currentThread.ToString() };
            int f = int.Parse(ftName[0]);
            int t = int.Parse(ftName[1]);
            ftName[0] = sendMailList.Items[f].SubItems[0].Text;
            ftName[1] = StaticInfo.toMails[t].ToString();

            Dimac.JMail.Message msgObj = new Dimac.JMail.Message();
            msgObj.From = ftName[0];
            msgObj.Subject = StaticInfo.mailTitle;
            //string cToMail = ftName[1];
            msgObj.To.Add(ftName[1], ftName[1]);
            msgObj.Charset = System.Text.Encoding.Default;
            msgObj.BodyHtml = StaticInfo.mailContent;
            for (int j = 0; j < StaticInfo.attArray.Count; j++)
                msgObj.Attachments.Add(StaticInfo.attArray[j].ToString());

            Smtp stmp = new Smtp();
            stmp.Authentication = SmtpAuthentication.Login;
            stmp.Port = 25;
            stmp.HostName = sendMailList.Items[f].SubItems[1].Text;
            stmp.Domain = sendMailList.Items[f].SubItems[1].Text;
            stmp.UserName = sendMailList.Items[f].SubItems[2].Text;
            stmp.Password = sendMailList.Items[f].SubItems[3].Text;
            try
            {
                stmp.Send(msgObj);
                logs.Add("*" + string.Format("{0,-25}", msgObj.From.Email) + "To    " + string.Format("{0,-25}", ftName[1]) + " 发送成功");
            }
            catch (Exception ex)
            {
                logs.Add("#" + string.Format("{0,-25}", msgObj.From.Email) + "To    " + string.Format("{0,-25}", ftName[1]) + " 发送失败,原因如下:");
                logs.Add("\t" + ex.Message);
                hasErr = true;
            }
            statMail.Text = "正在发送..." + string.Format("({0:0%})", (float)process / (float)totalTask);
            //Task Plus
            process++;
            if (process >= totalTask) process = totalTask;
        }
        #endregion

        #region 发送设置
        private void btnAddFromMail_Click(object sender, EventArgs e)
        {
            if (this.mailName.Text.Trim() != null && this.mailName.Text.IndexOf("@") > 1 && this.mailServer.Text.Trim() != "")
            {

                //检查重复性
                for (int i = 0; i < this.sendMailList.Items.Count; i++)
                {
                    string _mn = this.sendMailList.Items[i].SubItems[0].Text;
                    if (_mn.Trim().ToLower() == this.mailName.Text.Trim().ToLower() && this.mailName.Enabled)
                    {
                        i = this.sendMailList.Items.Count;
                        MessageBox.Show("已存在相同的发送邮件地址!");
                        return;
                    }
                }

                //保存
                try
                {
                    doh.Reset();
                    doh.AddFieldItem("mail", this.mailName.Text);
                    doh.AddFieldItem("server", this.mailServer.Text);
                    doh.AddFieldItem("uName", this.mailUsername.Text);
                    doh.AddFieldItem("uPass", this.mailPassword.Text);
                    if (this.sendMailList.Items.Count == 0 || this.sendMailList.SelectedItems.Count == 0)
                    {
                        doh.Insert("xk_fromMail");
                        this.sendMailList.Items.Add(new ListViewItem(new string[] { this.mailName.Text, this.mailServer.Text, this.mailUsername.Text, this.mailPassword.Text }));
                    }
                    else
                    {
                        doh.ConditionExpress = "mail=@mail";
                        doh.AddConditionParameter("@mail", this.mailName.Text);
                        doh.Update("xk_fromMail");
                        ListViewItem lvi = new ListViewItem(new string[] { this.mailName.Text, this.mailServer.Text, this.mailUsername.Text, this.mailPassword.Text });
                        int _selectedIndex = this.sendMailList.SelectedItems[0].Index;
                        this.sendMailList.Items.Remove(this.sendMailList.Items[this.sendMailList.SelectedItems[0].Index]);
                        this.sendMailList.Items.Insert(_selectedIndex, lvi);
                    }

                    this.mailName.Enabled = true;

                    this.mailName.Text = "";
                    this.mailServer.Text = "";
                    this.mailUsername.Text = "";
                    this.mailPassword.Text = "";

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                    return;
                }

            }
            else
            {
                MessageBox.Show("设置有误!");
                return;
            }
        }

        private void sendMailList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.sendMailList.Items.Count > 0 && this.sendMailList.SelectedItems.Count > 0)
            {

                ListViewItem lvi = this.sendMailList.SelectedItems[0];
                this.mailName.Text = lvi.SubItems[0].Text;
                this.mailServer.Text = lvi.SubItems[1].Text;
                this.mailUsername.Text = lvi.SubItems[2].Text;
                this.mailPassword.Text = lvi.SubItems[3].Text;

                this.mailName.Enabled = false;
            }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            if (this.sendMailList.Items.Count > 0 && this.sendMailList.SelectedItems.Count == 1)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -