1 Star 8 Fork 5

Ken He/Excel-AddIn

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Form2.cs 28.64 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Net.Mail;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelAddIn
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private Excel.Workbook workbook = ThisAddIn.app.ActiveWorkbook;
private string pictureType = "hide";
private string myAttachment_dir;
private List<string> myMailsto = new List<string>();
private List<string> myAttachment = new List<string>();
private List<string> errRecord = new List<string>();
private Dictionary<string, List<string>> address_attachment = new Dictionary<string, List<string>>();
private async void Form2_Load(object sender, EventArgs e)
{
this.MaximizeBox = false;
send_progress_label.Visible = false;
send_progressBar.Visible = false;
attachment_no_radioButton.Select();
attachment_textBox.Visible = false;
attachment_checkBox.Visible = false;
mailpassword_textBox.UseSystemPasswordChar = true;
pictureType = "hide";
// 使用异步方法加载数据
await LoadMailToComboBoxAsync();
foreach (Control control in this.Controls)
{
if (control is Label)
{
control.TabStop = false;
}
}
}
//异步加载数据
private async Task LoadMailToComboBoxAsync()
{
List<string> mailAddresses = new List<string>();
int columnCount = ThisAddIn.app.ActiveSheet.UsedRange.Columns.Count;
for (int i = 1; i <= columnCount; i++)
{
// 异步获取单元格文本
string mailAddress = await Task.Run(() => ThisAddIn.app.ActiveSheet.Cells[1, i].Text);
mailAddresses.Add(mailAddress);
}
// 在UI线程上更新ComboBox
this.Invoke(new MethodInvoker(() =>
{
foreach (string mailAddress in mailAddresses)
{
mailto_comboBox.Items.Add(mailAddress);
}
}));
}
//发送附件radioButton选中或未选中
private void attachment_yes_radioButton_CheckedChanged(object sender, EventArgs e)
{
if (attachment_yes_radioButton.Checked == true)
{
attachment_textBox.Visible = true;
attachment_checkBox.Visible = true;
}
else
{
attachment_textBox.Visible = false;
attachment_checkBox.Visible = false;
}
}
//附件textBox被单击时
private void attachment_textBox_Click(object sender, EventArgs e)
{
if (attachment_textBox.Text == "请手工输入附件所在目录的完整路径,或双击选择目录" || attachment_textBox.Text == "请手工输入文件的完整路径,或双击选择文件")
{
attachment_textBox.Text = "";
attachment_textBox.ForeColor = Color.Black;
}
}
//进度条更新函数
private void UpdateProgressBar(ProgressBar progressBar, int currentSheet, int totalSheets, Label progressBar_result_label, string progressBar_result)
{
// 计算进度百分比
int progressPercentage = (int)((double)currentSheet / totalSheets * 100);
// 更新进度条控件
progressBar.Value = progressPercentage;
progressBar.Update();
// 显示百分比数字
progressBar_result_label.Text = progressBar_result + progressPercentage.ToString() + "%";
}
//发送按钮
private void send_button_Click(object sender, EventArgs e)
{
if (mailfrom_textBox.Text != "" && mailfrom_comboBox.Text != "" && smtp_textBox.Text != "" && port_textBox.Text != "" && mailpassword_textBox.Text != "")
{
string myMail = mailfrom_textBox.Text + "@" + mailfrom_comboBox.Text;
string myPassword = mailpassword_textBox.Text;
string mySmtp = smtp_textBox.Text;
string myPort = port_textBox.Text;
string mySubject = subject_textBox.Text;
string myAttachpath = attachment_textBox.Text;
string myBody = body_richTextBox.Text;
int address_column = 0;
int attachment_column = 0;
if (attachment_textBox.Text == "请手工输入附件所在目录的完整路径,或双击选择目录" || attachment_textBox.Text == "请手工输入文件的完整路径,或双击选择文件")
{
attachment_textBox.Text = "";
attachment_textBox.ForeColor = Color.Black;
}
//读取收件人地址并写入myMailsto列表
if (mailto_textBox.Text != "" || mailto_comboBox.Text != "")
{
myMailsto.Clear();
//读取手工输入收件人地址
if (mailto_textBox.Text != "")
{
foreach (string mail in mailto_textBox.Text.Split(",".ToCharArray()))
{
myMailsto.Add(mail);
}
}
//读取excel表中收件人地址列内容
if (mailto_comboBox.Text != "")
{
foreach (Excel.Range fields_range in ThisAddIn.app.ActiveSheet.Range[ThisAddIn.app.ActiveSheet.Cells[1, 1], ThisAddIn.app.ActiveSheet.Cells[1, ThisAddIn.app.ActiveSheet.UsedRange.Columns.Count]])
{
if (fields_range.Value == mailto_comboBox.Text)
{
address_column = fields_range.Column;
break;
}
}
foreach (Excel.Range records_range in ThisAddIn.app.ActiveSheet.Range[ThisAddIn.app.ActiveSheet.Cells[2, address_column], ThisAddIn.app.ActiveSheet.Cells[ThisAddIn.app.ActiveSheet.UsedRange.Rows.Count, address_column]])
{
if (!string.IsNullOrEmpty(records_range.Value) && !myMailsto.Contains(records_range.Value))
{
myMailsto.Add(records_range.Value);
}
}
}
}
else
{
MessageBox.Show("收件人邮箱地址不能为空,请核对后再次运行");
return;
}
//读取附件
if (attachment_yes_radioButton.Checked)
{
if (attachment_checkBox.Checked)
{
foreach (Excel.Range fields_range in ThisAddIn.app.ActiveSheet.Range[ThisAddIn.app.ActiveSheet.Cells[1, 1], ThisAddIn.app.ActiveSheet.Cells[1, ThisAddIn.app.ActiveSheet.UsedRange.Columns.Count]])
{
if (fields_range.Value == "附件")
{
attachment_column = fields_range.Column;
break;
}
}
address_attachment.Clear();
for (int i = 2; i <= ThisAddIn.app.ActiveSheet.UsedRange.Rows.Count; i++)
{
if (!address_attachment.ContainsKey(ThisAddIn.app.ActiveSheet.Cells[i, address_column].Value))
{
string dic_key = ThisAddIn.app.ActiveSheet.Cells[i, address_column].Value;
List<string> dic_value = new List<string>();
string attach_path = ThisAddIn.app.ActiveSheet.Cells[i, attachment_column].Value;
if (attach_path.Contains(";"))
{
string[] attachmentPathValues = attach_path.Split(';');
foreach (string attachmentPathValue in attachmentPathValues)
{
dic_value.Add(myAttachpath + "\\" + attachmentPathValue);
}
}
else if (attach_path.Contains(";"))
{
string[] attachmentPathValues = attach_path.Split(';');
foreach (string attachmentPathValue in attachmentPathValues)
{
dic_value.Add(myAttachpath + "\\" + attachmentPathValue);
}
}
else
{
dic_value.Add(myAttachpath + "\\" + attach_path);
}
address_attachment.Add(dic_key, dic_value);
}
else
{
continue;
}
}
}
else
{
address_attachment.Clear();
foreach (string address_key in myMailsto)
{
//MessageBox.Show(address_key);
address_attachment.Add(address_key, myAttachment);
}
}
}
else
{
address_attachment.Clear();
foreach (string address_key in myMailsto)
{
//MessageBox.Show(address_key);
address_attachment.Add(address_key, myAttachment);
}
}
int current_maito = 1;
int total_mailto = myMailsto.Count;
//遍历收件人地址,调用发邮件函数发送邮件
foreach (string myMailto in myMailsto)
{
//更新进度条
string result_text = "正在发送" + current_maito.ToString() + "个,共" + total_mailto.ToString() + "个,已完成";
UpdateProgressBar(send_progressBar, current_maito, total_mailto, send_progress_label, result_text);
send_progress_label.Visible = true;
send_progressBar.Visible = true;
string result;
if (ssl_checkBox.Checked)
{
result = SendMail(myMailto, myMail, myPassword, mySmtp, myPort, mySubject, myBody, address_attachment[myMailto], true);
}
else
{
result = SendMail(myMailto, myMail, myPassword, mySmtp, myPort, mySubject, myBody, address_attachment[myMailto]);
}
current_maito++;
if (result != "finished")
{
errRecord.Add(myMailto + ":" + result);
}
}
if (errRecord.Count == 0)
{
MessageBox.Show("共" + total_mailto.ToString() + "封邮件,全部发送成功");
}
else
{
MessageBox.Show("共" + total_mailto.ToString() + "封邮件,其中有" + errRecord.Count.ToString() + "未发送成功,原因是:" + string.Join("\n", errRecord));
}
}
else
{
MessageBox.Show("发件人邮箱和密码不能为空");
}
}
//邮件发送
private string SendMail(string mailTo, string mailFrom, string password, string mailSmtp, string smtPort, string mailSubject, string mailBody, List<string> mailAttachPaths = null, bool ssl = false)
{
try
{
//设置SMTP服务器和发送者信息
SmtpClient smtpServer = new SmtpClient(mailSmtp);
MailMessage mail = new MailMessage()
{
From = new MailAddress(mailFrom)
};
smtpServer.Credentials = new System.Net.NetworkCredential(mailFrom, password);
if (ssl)
{
smtpServer.EnableSsl = true;
}
else { smtpServer.EnableSsl = false; }
//设置收件人和邮件内容
mail.To.Add(mailTo);
mail.Subject = mailSubject;
mail.Body = mailBody;
mail.IsBodyHtml = true;
//添加附件
if (mailAttachPaths != null)
{
foreach (string mailAttachPath in mailAttachPaths)
{
Attachment attachment = new Attachment(mailAttachPath);
mail.Attachments.Add(attachment);
}
}
//发送邮件
smtpServer.Send(mail);
//清空收件人和附件列表
mail.To.Clear();
mail.Attachments.Clear();
//关闭SMTP连接
smtpServer.Dispose();
return "finished";
}
catch (Exception ex)
{
return ex.Message;
}
}
//清空按钮
private void clear_button_Click(object sender, EventArgs e)
{
subject_textBox.Text = "";
body_richTextBox.Text = "";
attachment_textBox.Text = "";
mailto_textBox.Text = "";
mailto_comboBox.Text = "";
mailfrom_textBox.Text = "";
mailfrom_comboBox.Text = "";
mailpassword_textBox.Text = "";
smtp_textBox.Text = "";
port_textBox.Text = "";
}
//退出按钮
private void quit_button_Click(object sender, EventArgs e)
{
Close();
}
//双击文本框选择附件名称和路径,并写入全局列表中
private void attachment_textBox_DoubleClick(object sender, EventArgs e)
{
ThisAddIn.app.DisplayAlerts = false;
ThisAddIn.app.ScreenUpdating = false;
if (attachment_checkBox.Checked == true)
{
attachment_folderBrowserDialog.Description = "请选择附件所在文件夹";
attachment_folderBrowserDialog.ShowDialog();
if (attachment_folderBrowserDialog.SelectedPath.Length > 0)
{
attachment_textBox.Text = attachment_folderBrowserDialog.SelectedPath;
attachment_textBox.ForeColor = Color.Black;
myAttachment_dir = attachment_textBox.Text;
}
else
{
return;
}
}
else
{
attachment_openFileDialog.Title = "选择一个或多个文件";
attachment_openFileDialog.Filter = "所有文件|*.*";
attachment_openFileDialog.Multiselect = true;
attachment_openFileDialog.ShowDialog();
if (attachment_openFileDialog.FileNames.Length > 0)
{
attachment_textBox.Text = string.Join(";", attachment_openFileDialog.SafeFileNames);
attachment_textBox.ForeColor = Color.Black;
myAttachment.Clear();
foreach (string file_name in attachment_openFileDialog.FileNames)
{
myAttachment.Add(file_name);
}
}
else
{
return;
}
}
}
//邮箱smtp服务器和端口号自动匹配
private void mailfrom_comboBox_TextChanged(object sender, EventArgs e)
{
switch (mailfrom_comboBox.Text)
{
case "163.com":
smtp_textBox.Text = "smtp.163.com";
smtp_textBox.ReadOnly = true;
if (ssl_checkBox.Checked == true)
{
port_textBox.Text = "465";
port_textBox.ReadOnly = false;
}
else
{
port_textBox.Text = "25";
port_textBox.ReadOnly = true;
}
break;
case "qq.com":
smtp_textBox.Text = "smtp.qq.com";
smtp_textBox.ReadOnly = true;
if (ssl_checkBox.Checked == true)
{
port_textBox.Text = "465";
port_textBox.ReadOnly = false;
}
else
{
port_textBox.Text = "25";
port_textBox.ReadOnly = true;
}
break;
case "sina.com":
smtp_textBox.Text = "smtp.sina.com";
smtp_textBox.ReadOnly = true;
if (ssl_checkBox.Checked == true)
{
port_textBox.Text = "465";
port_textBox.ReadOnly = false;
}
else
{
port_textBox.Text = "25";
port_textBox.ReadOnly = true;
}
break;
case "sina.cn":
smtp_textBox.Text = "smtp.sina.cn";
smtp_textBox.ReadOnly = true;
if (ssl_checkBox.Checked == true)
{
port_textBox.Text = "465";
port_textBox.ReadOnly = false;
}
else
{
port_textBox.Text = "25";
port_textBox.ReadOnly = true;
}
break;
case "126.com":
smtp_textBox.Text = "smtp.126.com";
smtp_textBox.ReadOnly = true;
if (ssl_checkBox.Checked == true)
{
port_textBox.Text = "465";
port_textBox.ReadOnly = false;
}
else
{
port_textBox.Text = "25";
port_textBox.ReadOnly = true;
}
break;
case "sohu.com":
smtp_textBox.Text = "smtp.sohu.com";
smtp_textBox.ReadOnly = true;
if (ssl_checkBox.Checked == true)
{
port_textBox.Text = "465";
port_textBox.ReadOnly = false;
}
else
{
port_textBox.Text = "25";
port_textBox.ReadOnly = true;
}
break;
case "yeah.net":
smtp_textBox.Text = "smtp.yeah.net";
smtp_textBox.ReadOnly = true;
if (ssl_checkBox.Checked == true)
{
port_textBox.Text = "465";
port_textBox.ReadOnly = false;
}
else
{
port_textBox.Text = "25";
port_textBox.ReadOnly = true;
}
break;
case "139.com":
smtp_textBox.Text = "smtp.139.com";
smtp_textBox.ReadOnly = true;
if (ssl_checkBox.Checked == true)
{
port_textBox.Text = "465";
port_textBox.ReadOnly = false;
}
else
{
port_textBox.Text = "25";
port_textBox.ReadOnly = true;
}
break;
case "189.cn":
smtp_textBox.Text = "smtp.189.cn";
smtp_textBox.ReadOnly = true;
if (ssl_checkBox.Checked == true)
{
port_textBox.Text = "465";
port_textBox.ReadOnly = false;
}
else
{
port_textBox.Text = "25";
port_textBox.ReadOnly = true;
}
break;
case "gmail.com":
smtp_textBox.Text = "smtp.gmail.com";
smtp_textBox.ReadOnly = true;
if (ssl_checkBox.Checked == true)
{
port_textBox.Text = "465";
port_textBox.ReadOnly = false;
}
else
{
port_textBox.Text = "25";
port_textBox.ReadOnly = true;
}
break;
case "outlook.com":
smtp_textBox.Text = "smtp-mail.outlook.com";
smtp_textBox.ReadOnly = true;
if (ssl_checkBox.Checked == true)
{
port_textBox.Text = "465";
port_textBox.ReadOnly = false;
}
else
{
port_textBox.Text = "25";
port_textBox.ReadOnly = true;
}
break;
case "hotmail.com":
smtp_textBox.Text = "smtp-mail.outlook.com";
smtp_textBox.ReadOnly = true;
if (ssl_checkBox.Checked == true)
{
port_textBox.Text = "465";
port_textBox.ReadOnly = false;
}
else
{
port_textBox.Text = "25";
port_textBox.ReadOnly = true;
}
break;
case "aliyun.com":
smtp_textBox.Text = "smtp.aliyun.com";
smtp_textBox.ReadOnly = true;
if (ssl_checkBox.Checked == true)
{
port_textBox.Text = "465";
port_textBox.ReadOnly = false;
}
else
{
port_textBox.Text = "25";
port_textBox.ReadOnly = true;
}
break;
case "wo.cn":
smtp_textBox.Text = "smtp.wo.cn";
smtp_textBox.ReadOnly = true;
if (ssl_checkBox.Checked == true)
{
port_textBox.Text = "465";
port_textBox.ReadOnly = false;
}
else
{
port_textBox.Text = "25";
port_textBox.ReadOnly = true;
}
break;
default:
smtp_textBox.Text = "";
smtp_textBox.ReadOnly = false;
port_textBox.Text = "";
port_textBox.ReadOnly = false;
break;
}
}
//ssl加密选项被选中
private void ssl_checkBox_CheckedChanged(object sender, EventArgs e)
{
if (ssl_checkBox.Checked == true)
{
if (port_textBox.Text != "")
{
port_textBox.Text = "465";
port_textBox.ReadOnly = false;
}
else
{
port_textBox.Text = "";
port_textBox.ReadOnly = false;
}
}
else
{
if (smtp_textBox.Text != "")
{
port_textBox.Text = "25";
port_textBox.ReadOnly = true;
}
else
{
port_textBox.Text = "";
port_textBox.ReadOnly = false;
}
}
}
//发送不同附件checkBox选中时
private void attachment_checkBox_CheckedChanged(object sender, EventArgs e)
{
if (attachment_checkBox.Checked == true)
{
attachment_textBox.ForeColor = Color.LightGray;
toolTip1.SetToolTip(attachment_textBox, "请手工输入附件所在目录的完整路径,或双击选择目录");
attachment_textBox.Text = "请手工输入附件所在目录的完整路径,或双击选择目录";
mailto_textBox.Enabled = false;
}
else
{
attachment_textBox.ForeColor = Color.LightGray;
toolTip1.SetToolTip(attachment_textBox, "请手工输入文件的完整路径,或双击选择文件");
attachment_textBox.Text = "请手工输入文件的完整路径,或双击选择文件";
mailto_textBox.Enabled = true;
}
}
//不同附件选项改变时
private void attachment_checkBox_CheckStateChanged(object sender, EventArgs e)
{
if (attachment_checkBox.Checked == true)
{
List<string> messages = new List<string>() {
"1.发送不同附件只能使用表结构维护,需要在当前excel表中存在‘附件’列。",
"2.请注意所有附件均应放入同一文件夹,并可使用功能包中的‘批读文件名’读入文件名。",
"3.将批量读入文件名复制入‘附件’列,并做好与电子邮箱所在列的对应关系。",
"4.如某一发件人需接收多个附件,请将文件名维护在同一单元格内,并用;隔开。",
"请务必按以上说明操作,否则发送附件将会出错!"
};
string message = string.Join(Environment.NewLine, messages);
string caption = "重要提示";
DialogResult dr = MessageBox.Show(message, caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr != DialogResult.OK)
{
attachment_checkBox.Checked = false;
}
}
}
//密码可见图片单击时
private void mailpassword_pictureBox_Click(object sender, EventArgs e)
{
switch (pictureType)
{
case "hide":
mailpassword_pictureBox.Image = ExcelAddIn.Properties.Resources.eye_open;
mailpassword_textBox.UseSystemPasswordChar = false;
pictureType = "open";
break;
case "open":
mailpassword_pictureBox.Image = ExcelAddIn.Properties.Resources.eye_hide;
mailpassword_textBox.UseSystemPasswordChar = true;
pictureType = "hide";
break;
}
}
//窗体最小化时
private void Form2_VisibleChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
ThisAddIn.app.Visible = true;
ThisAddIn.app.WindowState = Excel.XlWindowState.xlMaximized;
}
}
private void mailfrom_comboBox_GotFocus(object sender, EventArgs e)
{
mailfrom_comboBox.Text = mailfrom_comboBox.Items[0].ToString();
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/aken721/excel-addin.git
[email protected]:aken721/excel-addin.git
aken721
excel-addin
Excel-AddIn
master

搜索帮助