XietongAuto,包含了NPOI的下载。
C# System.Net.Mail命名空间
官网API地址为:https://docs.microsoft.com/zh-cn/dotnet/api/system.net.mail?redirectedfrom=MSDN&view=netframework-4.8
通过该命名空间,可实现基于POP协议的邮箱自动发送功能,具体的C# DLL后面提供。
public static int SendEmail(string mailContent, string mailSubject, string mailTo)
{
string smtpServer = "smtp.qq.com";
string mailFrom = "XXXX@qq.com";
string userPassword = "XXXXXXXXX";
SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Host = smtpServer;
smtpClient.Timeout = 5000;
smtpClient.Port = 587;
smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, userPassword);
MailMessage mailMessage = new MailMessage(mailFrom, mailTo);
mailMessage.Subject = mailSubject;
mailMessage.Body = mailContent;
mailMessage.BodyEncoding = Encoding.UTF8;
mailMessage.IsBodyHtml = true;
mailMessage.Priority = MailPriority.Low;
try
{
ServicePointManager.ServerCertificateValidationCallback =
delegate (Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; };
smtpClient.Send(mailMessage);
return 1;
}
catch (SmtpException ex)
{
Logger.Create("- 邮件发送异常", ex.ToString());
return 0;
}
}
NPOI of C#
- 介绍
NPOI是指构建在POI 3.x版本之上的一个程序,NPOI可以在没有安装Office的情况下对Word或Excel文档进行读写操作。其中有Java,C#的实现,具体的C# DLL后面提供。
- excel读操作
private int read_excel()
{
string excelpath = textBox_path.Text;
IWorkbook wk = null;
if (File.Exists(excelpath))
{
string extension = System.IO.Path.GetExtension(excelpath);
FileStream fs = File.OpenRead(excelpath);
if (extension.Equals(".xls"))
{
wk = new HSSFWorkbook(fs);
}
else
{
wk = new XSSFWorkbook(fs);
}
fs.Close();
ISheet sheet = wk.GetSheetAt(0);
string sheetname = wk.GetSheetName(0);
int rowcount = sheet.LastRowNum;
for (int j = 2; j <= rowcount; j++)
{
IRow row = sheet.GetRow(j);
MessageBox.Show(row.GetCell(2));
}
return count;
}
else
{
return 0;
}
}
- excel写操作 (基于已有的文件进行写操作,避免设置样式)
public static void WriteExcel(string path)
{
string cell_val1 = "cell_val1";
string cell_val2 = "cell_val2";
if (File.Exists(path))
{
IWorkbook wk = null;
string extension = System.IO.Path.GetExtension(path);
FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
if (extension.Equals(".xls"))
{
wk = new HSSFWorkbook(fs);
}
else
{
wk = new XSSFWorkbook(fs);
}
fs.Close();
ISheet sheet = wk.GetSheetAt(0);
IRow row = sheet.GetRow(1);
row.Height = 20 * 20;
ICell c1 = row.CreateCell(1);
ICell c2 = row.CreateCell(3);
c1.SetCellValue(cell_val1);
c2.SetCellValue(cell_val2);
fs.Close();
using (FileStream fss = File.Open(@"E: est.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
wk.Write(fss);
fss.Close();
}
}
}
- excel写操作 ( 全新写入一个excel文件)
IWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet("Test Result");
IFont font = workbook.CreateFont();
ICellStyle style = workbook.CreateCellStyle();
style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CenterSelection;
style.VerticalAlignment = VerticalAlignment.Justify;
font.Boldweight = short.MaxValue;
font.FontHeightInPoints = 12;
style.SetFont(font);
IFont font1 = workbook.CreateFont();
ICellStyle style1 = workbook.CreateCellStyle();
style1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CenterSelection;
style1.VerticalAlignment = VerticalAlignment.Justify;
sheet.SetColumnWidth(1, 18 * 400);
sheet.SetColumnWidth(2, 18 * 256);
string[] value1 = { "NO", "UUT SN", "Condition"};
IRow row = sheet.CreateRow(0);
for (int a = 0; a < value1.Length; a++)
{
row.CreateCell(a).SetCellValue(Convert.ToString(value1[a]));
row.GetCell(a).CellStyle = style;
}
for (int i = 0; i < 32; i++)
{
IRow row1 = sheet.CreateRow((sheet.LastRowNum + 1));
row1.CreateCell(0).SetCellValue(i + 1);
row1.CreateCell(1).SetCellValue("5473567I008D9193541A1000041");
row1.CreateCell(2).SetCellValue("Temperature ");
row1.GetCell(0).CellStyle = style1;
row1.GetCell(1).CellStyle = style1;
row1.GetCell(2).CellStyle = style1;
font1.Boldweight = short.MaxValue;
style1.SetFont(font1);
FileStream fs = new FileStream(@"C:UserslenovoDesktopTest Result.xls", FileMode.Create, FileAccess.Write);
workbook.Write(fs);
fs.Close();
}
public static void SetCellValue(ICell cell, object obj)
{
if (obj.GetType() == typeof(int))
{
cell.SetCellValue((int)obj);
}
else if (obj.GetType() == typeof(double))
{
cell.SetCellValue((double)obj);
}
else if (obj.GetType() == typeof(IRichTextString))
{
cell.SetCellValue((IRichTextString)obj);
}
else if (obj.GetType() == typeof(string))
{
cell.SetCellValue(obj.ToString());
}
else if (obj.GetType() == typeof(DateTime))
{
cell.SetCellValue((DateTime)obj);
}
else if (obj.GetType() == typeof(bool))
{
cell.SetCellValue((bool)obj);
}
else
{
cell.SetCellValue(obj.ToString());
}
}
XietongAuto应用
代码仓库地址为:https://github.com/luozhengszj/XietongAuto
使用该应用,可以自动批量发送自定义的邮件(适用与任何的POP协议邮箱),主要具备特点:
- 该仓库代码可以根据选择的excel文件路径,进行读取excel;
- 邮件的标题的统一设置;
- 邮件的内容,可以根据luozhengQAQ+数字,及读取的excel文件的第三行往后、第四列往后的内容进行替换;
- 当发送邮件的目标邮件错误时,会有日志记录;
- 该仓库包含了c# npoi的dll文件,直接下载该代码仓库即可看到npoi的文件。
个人博客:Loak 正 - 关注人工智能及互联网的个人博客
文章地址:使用c#的System.Net.Mail包、NPOI包实现了基于excel表格的邮箱自定义批量发送