📄 frmmain.cs
字号:
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Collections;
using System.Runtime.InteropServices;
using System.IO;
using Xkzi;
//using Quiksoft.EasyMail.SMTP;
using Dimac.JMail;
namespace Xkzi.MailForm
{
public partial class FrmMain : XkForm
{
private int currentThread = 0;
private int sendtimes = 0;
private int totalTask = 0;
private int currentTask = 0;
private int process = 0;
private bool hasErr = false;
private ArrayList logs;
Thread[] thread;
public FrmMain()
{
InitializeComponent();
ConnectDb("data/DataBase.mdb", false);
LoadMails();
LoadGroups();
doh.Reset();
doh.SqlCmd = "select mail,server,uName,uPass from xk_fromMail";
DataTable dt = doh.GetDataTable();
for (int i = 0; i < dt.Rows.Count; i++)
{
this.sendMailList.Items.Add(new ListViewItem(new string[] { dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString(), dt.Rows[i][2].ToString(), dt.Rows[i][3].ToString() }));
}
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
if (!File.Exists(Application.StartupPath + "\\log.txt"))
File.Create(Application.StartupPath + "\\log.txt").Close();
}
#region 邮件内容
private void LoadMails()
{
doh.Reset();
doh.SqlCmd = "select id,title from xk_content order by id desc";
DataTable dt = doh.GetDataTable();
ArrayList mylist = new ArrayList();
mylist.Add(new DictionaryEntry(0, ""));
for (int i = 0; i < dt.Rows.Count; i++)
{
mylist.Add(new DictionaryEntry(dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString()));
}
cbMailTitle.DataSource = mylist;
cbMailTitle.DisplayMember = "Value";
cbMailTitle.ValueMember = "Key";
}
private void LoadGroups()
{
doh.Reset();
doh.SqlCmd = "select id,title from xk_mailGroup";
DataTable dt = doh.GetDataTable();
ArrayList mylist = new ArrayList();
mylist.Add(new DictionaryEntry(0, ""));
for (int i = 0; i < dt.Rows.Count; i++)
{
mylist.Add(new DictionaryEntry(dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString()));
}
cbSendMailGroup.DataSource = mylist;
cbSendMailGroup.DisplayMember = "Value";
cbSendMailGroup.ValueMember = "Key";
//mylist.Insert(0, new DictionaryEntry(0, ""));
cbMailGroup.DataSource = mylist;
cbMailGroup.DisplayMember = "Value";
cbMailGroup.ValueMember = "Key";
}
private void chkIsUseGroup_CheckedChanged(object sender, EventArgs e)
{
if (chkIsUseGroup.Checked)
txtReceive.Enabled = false;
else
txtReceive.Enabled = true;
}
private void btnSaveMail_Click(object sender, EventArgs e)
{
if (ChkMailContent())
{
string mailTitle = cbMailTitle.Text;
doh.Reset();
doh.AddFieldItem("title", mailTitle);
doh.AddFieldItem("toUser", txtReceive.Text);
if (cbSendMailGroup.SelectedValue != null)
{
doh.AddFieldItem("groupId", cbSendMailGroup.SelectedValue.ToString());
doh.AddFieldItem("groupName", cbSendMailGroup.Text);
}
else
{
doh.AddFieldItem("groupId", 0);
doh.AddFieldItem("groupName", "");
}
if (chkIsUseGroup.Checked)
doh.AddFieldItem("isUseGroup", 1);
else
doh.AddFieldItem("isUseGroup", 0);
doh.AddFieldItem("content", rtbMailContent.Text);
doh.Insert("xk_content");
LoadMails();
cbMailTitle.Text = mailTitle;
ShowMsgBox("保存成功");
}
}
private bool ChkMailContent()
{
errPor.Clear();
if (cbMailTitle.Text.Trim() == "")
{
errPor.SetError(cbMailTitle, "不能为空!");
return false;
}
if (!chkIsUseGroup.Checked)
{
if (txtReceive.Text.Trim() == "")
{
errPor.SetError(txtReceive, "不能为空!");
return false;
}
}
else
{
if (cbSendMailGroup.Text == "")
{
errPor.SetError(cbSendMailGroup, "请先添加邮件分组!");
return false;
}
}
if (rtbMailContent.Text.Trim() == "")
{
errPor.SetError(rtbMailContent, "不能为空!");
return false;
}
return true;
}
private void cbMailTitle_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbMailTitle.SelectedValue != null && Validator.StrToInt(cbMailTitle.SelectedValue.ToString(), 0) > 0)
{
doh.Reset();
doh.ConditionExpress = "id=" + cbMailTitle.SelectedValue.ToString();
object[] _obj = doh.GetValues("xk_content", "toUser,groupName,isUseGroup,content");
if (_obj != null)
{
txtReceive.Text = _obj[0].ToString();
cbSendMailGroup.Text = _obj[1].ToString();
if (_obj[2].ToString().Trim() == "0" || _obj[2].ToString().Trim() == "")
chkIsUseGroup.Checked = false;
else
chkIsUseGroup.Checked = true;
rtbMailContent.Text = _obj[3].ToString();
}
}
else
{
txtReceive.Text = "";
chkIsUseGroup.Checked = false;
rtbMailContent.Text = "";
lvMailUpfile.Clear();
}
}
private void btnDelMail_Click(object sender, EventArgs e)
{
if (MessageBox.Show(this, "确定要删除吗?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
if (cbMailTitle.SelectedValue != null && Validator.StrToInt(cbMailTitle.SelectedValue.ToString(), 0) > 0)
{
doh.Reset();
doh.ConditionExpress = "id=" + cbMailTitle.SelectedValue.ToString();
doh.Delete("xk_content");
LoadMails();
ShowMsgBox("删除成功!");
}
}
}
private void btnAddUpfile_Click(object sender, EventArgs e)
{
this.openFileDialog1.Filter = "全部文件(*.*)|*.*";
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fileName = this.openFileDialog1.FileName;
StaticInfo.attArray.Add(fileName);
AddIcon(fileName);
}
}
#endregion
#region 添加附件显示Icon
/// <summary>
/// 判断加入的文件格式,并添加到附件ListView中
/// </summary>
/// <param name="fileType"></param>
private void AddIcon(string fileType)
{
try
{
string Name = fileType.Substring(fileType.LastIndexOf(@"\") + 1);
this.imglMailUpfile.Images.Add(GetIcon(fileType));
this.lvMailUpfile.LargeImageList = this.imglMailUpfile;
this.lvMailUpfile.SmallImageList = this.imglMailUpfile;
this.lvMailUpfile.Items.Add(Name, StaticInfo.attNum++);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
//函数SHGetFileInfo()的返回值也随uFlags的取值变化而有所不同。
//通过调用SHGetFileInfo()可以由psfi参数得到文件的图标句柄,但要注意在uFlags参数中不使用SHGFI_PIDL时,SHGetFileInfo()不能获得"我的电脑"等虚似文件夹的信息。
[DllImport("Shell32.dll")]
private static extern int SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, SHGFI uFlags);
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
//重载一个构造函数
public SHFILEINFO(bool b)
{
hIcon = IntPtr.Zero; iIcon = 0; dwAttributes = 0; szDisplayName = ""; szTypeName = "";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 256)]
public string szDisplayName;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 80)]
public string szTypeName;
};
//图标类型的结构体
private enum SHGFI
{
SHGFI_ICON = 0x000000100, // get icon
SHGFI_DISPLAYNAME = 0x000000200, // get display name
SHGFI_TYPENAME = 0x000000400, // get type name
SHGFI_ATTRIBUTES = 0x000000800, // get attributes
SHGFI_ICONLOCATION = 0x000001000, // get icon location
SHGFI_EXETYPE = 0x000002000, // return exe type
SHGFI_SYSICONINDEX = 0x000004000, // get system icon index
SHGFI_LINKOVERLAY = 0x000008000, // put a link overlay on icon
SHGFI_SELECTED = 0x000010000, // show icon in selected state
SHGFI_ATTR_SPECIFIED = 0x000020000, // get only specified attributes
SHGFI_LARGEICON = 0x000000000, // get large icon
SHGFI_SMALLICON = 0x000000001, // get small icon
SHGFI_OPENICON = 0x000000002, // get open icon
SHGFI_SHELLICONSIZE = 0x000000004, // get shell size icon
SHGFI_PIDL = 0x000000008, // pszPath is a pidl
SHGFI_USEFILEATTRIBUTES = 0x000000010, // use passed dwFileAttribute
SHGFI_ADDOVERLAYS = 0x000000020, // apply the appropriate overlays
SHGFI_OVERLAYINDEX = 0x000000040 // Get the index of the overlay
}
/// <summary>
/// 调用获的程序图标句柄的函数
/// </summary>
/// <param name="strPath">选定程序的绝对路径</param>
/// <returns></returns>
private static Icon GetIcon(string strPath)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -