找回密码
 立即注册
首页 业界区 业界 使用 C# 和 SQL Server 自动化邮件中的用户分配数据处理 ...

使用 C# 和 SQL Server 自动化邮件中的用户分配数据处理

距佰溘 3 天前
引言

在金融应用领域,通过电子邮件手动管理用户分配数据不仅耗时而且容易出错。传统的人工处理方式需要工作人员每天检查邮件、下载附件、解析数据并更新数据库,这一系列重复性工作既低效又存在数据录入错误的风险。本文将介绍如何利用 C# 和 SQL Server 构建一个自动化解决方案,实现从邮件读取、附件下载到数据库更新的全流程自动化处理。该系统特别适用于处理主题包含"AllotmentsFiles"的邮件,并更新 SQL Server 中的 bidfiledetails 表,显著提高数据处理效率和准确性。
正文内容

1. 系统概述与优势

1.1 处理流程

该自动化系统主要包含三个核心处理步骤:

  • 连接邮件服务器:使用 POP3 协议连接到邮件服务器,支持 Gmail 等常见邮件服务提供商。
  • 筛选目标邮件:自动筛选出当天收到的、主题包含"AllotmentsFiles"的邮件。
  • 处理附件并更新数据库:下载邮件中的 CSV 附件,解析内容并更新 SQL Server 数据库中的分配数据。
1.2 系统优势


  • 自动化重复工作:取代人工检查邮件和处理附件的过程
  • 减少人为错误:自动化的数据处理流程避免了手工录入可能导致的错误
  • 全天候运行:系统可配置为每日自动运行,无需人工干预
  • 高效数据处理:批量处理大量分配数据,显著提高工作效率
2. 开发环境与工具配置

2.1 所需工具


  • 开发工具:Visual Studio(C#)
  • 数据库:SQL Server
  • NuGet 包

    • OpenPop.NET:用于通过 POP3 协议读取邮件
    • System.Data.SqlClient:用于数据库连接和操作

2.2 配置设置

系统配置存储在 web.config 文件中,包含邮件服务器连接信息:
  1.   
  2.   
  3.   
  4.   
  5. </appSettings>
复制代码
此配置允许灵活更改邮件服务器设置而无需修改代码。
3. 邮件处理核心代码实现

3.1 读取邮件功能

以下 C# 代码实现了从邮件服务器读取并处理目标邮件的功能:
  1. public void ReadTheDataFromTheMail(){    try    {  
  2.   
  3.   
  4.   
  5. </appSettings>string mailHost = ConfigurationManager.AppSettings["HostServer"];  
  6.   
  7.   
  8.   
  9. </appSettings>int mailPort = Convert.ToInt32(ConfigurationManager.AppSettings["MailPort"]);  
  10.   
  11.   
  12.   
  13. </appSettings>string mailID = ConfigurationManager.AppSettings["MailUser"];  
  14.   
  15.   
  16.   
  17. </appSettings>string mailPwd = ConfigurationManager.AppSettings["MailPwd"];  
  18.   
  19.   
  20.   
  21. </appSettings>using (Pop3Client client = new Pop3Client())  
  22.   
  23.   
  24.   
  25. </appSettings>{  
  26.   
  27.   
  28.   
  29. </appSettings>    client.Connect(mailHost, mailPort, true);  
  30.   
  31.   
  32.   
  33. </appSettings>    client.Authenticate(mailID, mailPwd);  
  34.   
  35.   
  36.   
  37. </appSettings>    int messageCount = client.GetMessageCount();  
  38.   
  39.   
  40.   
  41. </appSettings>    DateTime today = DateTime.UtcNow.Date;  
  42.   
  43.   
  44.   
  45. </appSettings>    for (int i = messageCount; i > 0; i--)  
  46.   
  47.   
  48.   
  49. </appSettings>    {  
  50.   
  51.   
  52.   
  53. </appSettings>  
  54.   
  55.   
  56.   
  57. </appSettings>MessageHeader headers = client.GetMessageHeaders(i);  
  58.   
  59.   
  60.   
  61. </appSettings>  
  62.   
  63.   
  64.   
  65. </appSettings>string subject = headers.Subject;  
  66.   
  67.   
  68.   
  69. </appSettings>  
  70.   
  71.   
  72.   
  73. </appSettings>DateTime emailDate = headers.DateSent;  
  74.   
  75.   
  76.   
  77. </appSettings>  
  78.   
  79.   
  80.   
  81. </appSettings>if (!string.IsNullOrEmpty(subject) && subject.Contains("AllotmentsFiles") && emailDate.Date == today)  
  82.   
  83.   
  84.   
  85. </appSettings>  
  86.   
  87.   
  88.   
  89. </appSettings>{  
  90.   
  91.   
  92.   
  93. </appSettings>  
  94.   
  95.   
  96.   
  97. </appSettings>    OpenPop.Mime.Message message = client.GetMessage(i);  
  98.   
  99.   
  100.   
  101. </appSettings>  
  102.   
  103.   
  104.   
  105. </appSettings>    foreach (var attachment in message.FindAllAttachments())  
  106.   
  107.   
  108.   
  109. </appSettings>  
  110.   
  111.   
  112.   
  113. </appSettings>    {  
  114.   
  115.   
  116.   
  117. </appSettings>  
  118.   
  119.   
  120.   
  121. </appSettings>  
  122.   
  123.   
  124.   
  125. </appSettings>if (attachment.FileName.Contains("AllotmentsFiles_") && attachment.FileName.EndsWith(".csv"))  
  126.   
  127.   
  128.   
  129. </appSettings>  
  130.   
  131.   
  132.   
  133. </appSettings>  
  134.   
  135.   
  136.   
  137. </appSettings>{  
  138.   
  139.   
  140.   
  141. </appSettings>  
  142.   
  143.   
  144.   
  145. </appSettings>  
  146.   
  147.   
  148.   
  149. </appSettings>    string baseDirectory = @"E:\Allotment - Applns\Allotmentfiles";  
  150.   
  151.   
  152.   
  153. </appSettings>  
  154.   
  155.   
  156.   
  157. </appSettings>  
  158.   
  159.   
  160.   
  161. </appSettings>    string dateFolder = DateTime.Now.ToString("yyyy-MM-dd");  
  162.   
  163.   
  164.   
  165. </appSettings>  
  166.   
  167.   
  168.   
  169. </appSettings>  
  170.   
  171.   
  172.   
  173. </appSettings>    string fileFolder = Path.Combine(baseDirectory, dateFolder);  
  174.   
  175.   
  176.   
  177. </appSettings>  
  178.   
  179.   
  180.   
  181. </appSettings>  
  182.   
  183.   
  184.   
  185. </appSettings>    if (!Directory.Exists(fileFolder))  
  186.   
  187.   
  188.   
  189. </appSettings>  
  190.   
  191.   
  192.   
  193. </appSettings>  
  194.   
  195.   
  196.   
  197. </appSettings>  
  198.   
  199.   
  200.   
  201. </appSettings>Directory.CreateDirectory(fileFolder);  
  202.   
  203.   
  204.   
  205. </appSettings>  
  206.   
  207.   
  208.   
  209. </appSettings>  
  210.   
  211.   
  212.   
  213. </appSettings>    string filePath = Path.Combine(fileFolder, attachment.FileName);  
  214.   
  215.   
  216.   
  217. </appSettings>  
  218.   
  219.   
  220.   
  221. </appSettings>  
  222.   
  223.   
  224.   
  225. </appSettings>    File.WriteAllBytes(filePath, attachment.Body);  
  226.   
  227.   
  228.   
  229. </appSettings>  
  230.   
  231.   
  232.   
  233. </appSettings>  
  234.   
  235.   
  236.   
  237. </appSettings>    errorlog($"CSV file '{attachment.FileName}'", "Downloaded successfully!");  
  238.   
  239.   
  240.   
  241. </appSettings>  
  242.   
  243.   
  244.   
  245. </appSettings>  
  246.   
  247.   
  248.   
  249. </appSettings>    ProcessAllotmentData(filePath);  
  250.   
  251.   
  252.   
  253. </appSettings>  
  254.   
  255.   
  256.   
  257. </appSettings>  
  258.   
  259.   
  260.   
  261. </appSettings>}  
  262.   
  263.   
  264.   
  265. </appSettings>  
  266.   
  267.   
  268.   
  269. </appSettings>    }  
  270.   
  271.   
  272.   
  273. </appSettings>  
  274.   
  275.   
  276.   
  277. </appSettings>}  
  278.   
  279.   
  280.   
  281. </appSettings>    }  
  282.   
  283.   
  284.   
  285. </appSettings>}    }    catch (Exception ex)    {  
  286.   
  287.   
  288.   
  289. </appSettings>errorlog("Error reading emails", ex.Message);    }}
复制代码
这段代码实现了以下功能:

  • 从配置读取邮件服务器连接信息
  • 使用 POP3 连接到邮件服务器
  • 筛选当天且主题包含"AllotmentsFiles"的邮件
  • 下载符合条件的 CSV 附件并保存到按日期组织的文件夹中
  • 调用数据处理方法处理下载的附件
4. CSV数据处理与数据库更新

4.1 数据处理流程

下载的 CSV 文件需要被解析并更新到数据库中,以下是实现代码:
  1. public void ProcessAllotmentData(string filePath){    try    {  
  2.   
  3.   
  4.   
  5. </appSettings>string fileName = Path.GetFileName(filePath);  
  6.   
  7.   
  8.   
  9. </appSettings>string[] parts = fileName.Split('_');  
  10.   
  11.   
  12.   
  13. </appSettings>string scriptName = parts.Length > 0 ? parts[0] : null;  
  14.   
  15.   
  16.   
  17. </appSettings>using (StreamReader sr = new StreamReader(filePath))  
  18.   
  19.   
  20.   
  21. </appSettings>{  
  22.   
  23.   
  24.   
  25. </appSettings>    string[] rows = sr.ReadToEnd().Split('\n');  
  26.   
  27.   
  28.   
  29. </appSettings>    for (int i = 1; i < rows.Length; i++)  
  30.   
  31.   
  32.   
  33. </appSettings>    {  
  34.   
  35.   
  36.   
  37. </appSettings>  
  38.   
  39.   
  40.   
  41. </appSettings>string[] rowValues = rows[i].Split(',');  
  42.   
  43.   
  44.   
  45. </appSettings>  
  46.   
  47.   
  48.   
  49. </appSettings>if (rowValues.Length > 7)  
  50.   
  51.   
  52.   
  53. </appSettings>  
  54.   
  55.   
  56.   
  57. </appSettings>{  
  58.   
  59.   
  60.   
  61. </appSettings>  
  62.   
  63.   
  64.   
  65. </appSettings>    string appNo = rowValues[2].Trim();  
  66.   
  67.   
  68.   
  69. </appSettings>  
  70.   
  71.   
  72.   
  73. </appSettings>    string pan = rowValues[4].Trim();  
  74.   
  75.   
  76.   
  77. </appSettings>  
  78.   
  79.   
  80.   
  81. </appSettings>    string qty = rowValues[5].Trim();  
  82.   
  83.   
  84.   
  85. </appSettings>  
  86.   
  87.   
  88.   
  89. </appSettings>    string clientName = rowValues[6].Trim();  
  90.   
  91.   
  92.   
  93. </appSettings>  
  94.   
  95.   
  96.   
  97. </appSettings>    string reason = rowValues[7].Trim();  
  98.   
  99.   
  100.   
  101. </appSettings>  
  102.   
  103.   
  104.   
  105. </appSettings>    SqlParameter[] param = {  
  106.   
  107.   
  108.   
  109. </appSettings>  
  110.   
  111.   
  112.   
  113. </appSettings>  
  114.   
  115.   
  116.   
  117. </appSettings>new SqlParameter("@symbol", scriptName),  
  118.   
  119.   
  120.   
  121. </appSettings>  
  122.   
  123.   
  124.   
  125. </appSettings>  
  126.   
  127.   
  128.   
  129. </appSettings>new SqlParameter("@appno", appNo),  
  130.   
  131.   
  132.   
  133. </appSettings>  
  134.   
  135.   
  136.   
  137. </appSettings>  
  138.   
  139.   
  140.   
  141. </appSettings>new SqlParameter("@pan", pan),  
  142.   
  143.   
  144.   
  145. </appSettings>  
  146.   
  147.   
  148.   
  149. </appSettings>  
  150.   
  151.   
  152.   
  153. </appSettings>new SqlParameter("@qty", qty),  
  154.   
  155.   
  156.   
  157. </appSettings>  
  158.   
  159.   
  160.   
  161. </appSettings>  
  162.   
  163.   
  164.   
  165. </appSettings>new SqlParameter("@reason", reason)  
  166.   
  167.   
  168.   
  169. </appSettings>  
  170.   
  171.   
  172.   
  173. </appSettings>    };  
  174.   
  175.   
  176.   
  177. </appSettings>  
  178.   
  179.   
  180.   
  181. </appSettings>    string statusQuery = @"SELECT * FROM bidfiledetails  (NOLOCK)  
  182.   
  183.   
  184.   
  185. </appSettings>  
  186.   
  187.   
  188.   
  189. </appSettings>  
  190.   
  191.   
  192.   
  193. </appSettings>  
  194.   
  195.   
  196.   
  197. </appSettings>  
  198.   
  199.   
  200.   
  201. </appSettings>   WHERE AppNo=@appno AND Symbol=@symbol AND PanNo=@pan AND AllotmentFlag != 'Y'";  
  202.   
  203.   
  204.   
  205. </appSettings>  
  206.   
  207.   
  208.   
  209. </appSettings>    DataSet ds = SqlHelper.ExecuteDataset(SqlCon, CommandType.Text, statusQuery, param);  
  210.   
  211.   
  212.   
  213. </appSettings>  
  214.   
  215.   
  216.   
  217. </appSettings>    if (ds.Tables[0].Rows.Count > 0)  
  218.   
  219.   
  220.   
  221. </appSettings>  
  222.   
  223.   
  224.   
  225. </appSettings>    {  
  226.   
  227.   
  228.   
  229. </appSettings>  
  230.   
  231.   
  232.   
  233. </appSettings>  
  234.   
  235.   
  236.   
  237. </appSettings>string updateQuery = @"UPDATE bidfiledetails  
  238.   
  239.   
  240.   
  241. </appSettings>  
  242.   
  243.   
  244.   
  245. </appSettings>  
  246.   
  247.   
  248.   
  249. </appSettings>  
  250.   
  251.   
  252.   
  253. </appSettings>  
  254.   
  255.   
  256.   
  257. </appSettings>       SET AllotmentFlag='Y', SharesAlloted=@qty, Sharesrej_reason=@reason  
  258.   
  259.   
  260.   
  261. </appSettings>  
  262.   
  263.   
  264.   
  265. </appSettings>  
  266.   
  267.   
  268.   
  269. </appSettings>  
  270.   
  271.   
  272.   
  273. </appSettings>  
  274.   
  275.   
  276.   
  277. </appSettings>       WHERE AppNo=@appno AND PanNo=@pan AND Symbol=@symbol";  
  278.   
  279.   
  280.   
  281. </appSettings>  
  282.   
  283.   
  284.   
  285. </appSettings>  
  286.   
  287.   
  288.   
  289. </appSettings>SqlHelper.ExecuteNonQuery(SqlCon, CommandType.Text, updateQuery, param);  
  290.   
  291.   
  292.   
  293. </appSettings>  
  294.   
  295.   
  296.   
  297. </appSettings>  
  298.   
  299.   
  300.   
  301. </appSettings>errorlog("Updated Record", $"PanNo: {pan}, ApplicationNo: {appNo}, Symbol: {scriptName}");  
  302.   
  303.   
  304.   
  305. </appSettings>  
  306.   
  307.   
  308.   
  309. </appSettings>    }  
  310.   
  311.   
  312.   
  313. </appSettings>  
  314.   
  315.   
  316.   
  317. </appSettings>    else  
  318.   
  319.   
  320.   
  321. </appSettings>  
  322.   
  323.   
  324.   
  325. </appSettings>    {  
  326.   
  327.   
  328.   
  329. </appSettings>  
  330.   
  331.   
  332.   
  333. </appSettings>  
  334.   
  335.   
  336.   
  337. </appSettings>string header = "ApplicationNo|ClientName|Quantity|Reason|PanNo";  
  338.   
  339.   
  340.   
  341. </appSettings>  
  342.   
  343.   
  344.   
  345. </appSettings>  
  346.   
  347.   
  348.   
  349. </appSettings>string message = $"{appNo}|{clientName}|{qty}|{reason}|{pan}{Environment.NewLine}";  
  350.   
  351.   
  352.   
  353. </appSettings>  
  354.   
  355.   
  356.   
  357. </appSettings>  
  358.   
  359.   
  360.   
  361. </appSettings>errorlog(header, message);  
  362.   
  363.   
  364.   
  365. </appSettings>  
  366.   
  367.   
  368.   
  369. </appSettings>    }  
  370.   
  371.   
  372.   
  373. </appSettings>  
  374.   
  375.   
  376.   
  377. </appSettings>}  
  378.   
  379.   
  380.   
  381. </appSettings>  
  382.   
  383.   
  384.   
  385. </appSettings>else  
  386.   
  387.   
  388.   
  389. </appSettings>  
  390.   
  391.   
  392.   
  393. </appSettings>{  
  394.   
  395.   
  396.   
  397. </appSettings>  
  398.   
  399.   
  400.   
  401. </appSettings>    errorlog("Row does not have enough columns", "");  
  402.   
  403.   
  404.   
  405. </appSettings>  
  406.   
  407.   
  408.   
  409. </appSettings>}  
  410.   
  411.   
  412.   
  413. </appSettings>    }  
  414.   
  415.   
  416.   
  417. </appSettings>}    }    catch (Exception ex)    {  
  418.   
  419.   
  420.   
  421. </appSettings>ExceptionLogging.Exceptionlog("Exception in ProcessAllotmentData", ex.Message);    }}
复制代码
此代码实现了:

  • 从CSV文件名提取脚本名称
  • 逐行读取CSV文件内容
  • 验证数据完整性(确保有足够列数)
  • 检查数据库中的记录状态
  • 更新符合条件的记录或记录未匹配的情况
  • 全面的错误处理和日志记录
5. 关键技术与实现要点

5.1 核心技术组件


  • 邮件处理:使用 OpenPop.NET 库通过 POP3 协议读取邮件
  • 文件处理:系统自动按日期组织下载的附件文件
  • 数据验证:严格检查CSV文件结构和数据完整性
  • 数据库操作:使用参数化查询确保SQL注入防护
  • 错误处理:全面的异常捕获和日志记录机制
5.2 系统部署方式


  • 任务计划程序:配置为每日自动运行的Windows计划任务
  • Windows服务:可转换为服务实现24/7运行
  • 日志监控:通过日志文件监控系统运行状态
6. 系统扩展与优化方向

6.1 功能增强建议


  • 转换为Windows服务:实现完全自动化的后台处理
  • 邮件通知功能:在处理完成或出现错误时发送通知邮件
  • 日志数据库存储:将日志存入SQL表便于历史查询和分析
  • 重试机制:为失败的邮件下载实现自动重试逻辑
6.2 性能优化建议


  • 实现批量更新操作减少数据库往返
  • 添加并发处理能力提高大量邮件的处理速度
  • 优化内存使用处理超大附件文件
结论

本文详细介绍了一个基于C#和SQL Server的自动化邮件处理系统,能够高效地从邮件中提取分配数据并更新数据库。该系统通过自动化取代了繁琐的手工操作,显著提高了金融应用中分配数据处理的效率和准确性。核心功能包括邮件服务器连接、目标邮件筛选、CSV附件下载与解析以及数据库更新等完整流程。系统采用模块化设计,具有良好的可扩展性,可通过转换为Windows服务、添加通知功能等方式进一步增强。该解决方案不仅适用于金融领域的分配数据处理,其设计思路和实现方法也可应用于其他需要从邮件自动提取数据并更新数据库的类似场景。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册