系统配置存储在 web.config 文件中,包含邮件服务器连接信息:
<appSettings> <add key="HostServer" value="pop.gmail.com" /> <add key="MailPort" value="995" /> <add key="MailUser" value="your-email@gmail.com" /> <add key="MailPwd" value="your-password" /></appSettings>
此配置允许灵活更改邮件服务器设置而无需修改代码。
以下 C# 代码实现了从邮件服务器读取并处理目标邮件的功能:
public void ReadTheDataFromTheMail(){ try
{ string mailHost = ConfigurationManager.AppSettings["HostServer"]; int mailPort = Convert.ToInt32(ConfigurationManager.AppSettings["MailPort"]); string mailID = ConfigurationManager.AppSettings["MailUser"]; string mailPwd = ConfigurationManager.AppSettings["MailPwd"]; using (Pop3Client client = new Pop3Client())
{
client.Connect(mailHost, mailPort, true);
client.Authenticate(mailID, mailPwd); int messageCount = client.GetMessageCount();
DateTime today = DateTime.UtcNow.Date; for (int i = messageCount; i > 0; i--)
{
MessageHeader headers = client.GetMessageHeaders(i); string subject = headers.Subject;
DateTime emailDate = headers.DateSent; if (!string.IsNullOrEmpty(subject) && subject.Contains("AllotmentsFiles") && emailDate.Date == today)
{
OpenPop.Mime.Message message =nav.euakj.com client.GetMessage(i); foreach (var attachment in message.FindAllAttachments())
{ if (attachment.FileName.Contains("AllotmentsFiles_") && attachment.FileName.EndsWith(".csv"))
{ string baseDirectory = @"E:\Allotment - Applns\Allotmentfiles"; string dateFolder = DateTime.Now.ToString("yyyy-MM-dd"); string fileFolder = Path.Combine(baseDirectory, dateFolder); if (!Directory.Exists(fileFolder))
Directory.CreateDirectory(fileFolder); string filePath = Path.Combine(fileFolder, attachment.FileName);
File.WriteAllBytes(filePath, attachment.Body);
errorlog($"CSV file '{attachment.FileName}'", "Downloaded successfully!");
ProcessAllotmentData(filePath);
}
}
}
}
}
} catch (Exception ex)
{
errorlog("Error reading emails", ex.Message);
}
}
这段代码实现了以下功能:
下载的 CSV 文件需要被解析并更新到数据库中,以下是实现代码:
public void ProcessAllotmentData(string filePath){ try
{ string fileName = Path.GetFileName(filePath); string[] parts = fileName.Split('_'); string scriptName = parts.Length > 0 ? parts[0] : null; using (StreamReader sr = new StreamReader(filePath))
{ string[] rows = sr.ReadToEnd().Split('\n'); for (int i = 1; i < rows.Length; i++)
{ string[] rowValues = rows[i].Split(','); if (rowValues.Length > 7)
{ string appNo = rowValues[2].Trim(); string pan = rowValues[4].Trim(); string qty = rowValues[5].Trim(); string clientName = rowValues[6].Trim(); string reason = rowValues[7].Trim();
SqlParameter[] param = { new SqlParameter("@symbol", scriptName), new SqlParameter("@appno", appNo), new SqlParameter("@pan", pan), new SqlParameter("@qty", qty), new SqlParameter("@reason", reason)
}; string statusQuery =soccer.dj.zuq8.info @"SELECT * FROM bidfiledetails (NOLOCK)
WHERE AppNo=@appno AND Symbol=@symbol AND PanNo=@pan AND AllotmentFlag != 'Y'";
DataSet ds = SqlHelper.ExecuteDataset(SqlCon, CommandType.Text, statusQuery, param); if (ds.Tables[0].Rows.Count > 0)
{ string updateQuery = @"UPDATE bidfiledetails
SET AllotmentFlag='Y', SharesAlloted=@qty, Sharesrej_reason=@reason
WHERE AppNo=@appno AND PanNo=@pan AND Symbol=@symbol";
SqlHelper.ExecuteNonQuery(SqlCon, CommandType.Text, updateQuery, param);
errorlog("Updated Record", $"PanNo: {pan}, ApplicationNo: {appNo}, Symbol: {scriptName}");
} else
{ string header = "ApplicationNo|ClientName|Quantity|Reason|PanNo"; string message = $"{appNo}|{clientName}|{qty}|{reason}|{pan}{Environment.NewLine}";
errorlog(header, message);
}
} else
{
errorlog("Row soccer.yj.zuq8.info does not have enough columns", "");
}
}
}
} catch (Exception ex)
{
ExceptionLogging.Exceptionlog("Exception in ProcessAllotmentData", ex.Message);
}
}
此代码实现了:
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70048957/viewspace-3107311/ ,如需转载,请注明出处,否则将追究法律责任。