.net 在控制台应用程序中发送Html邮件
我已经创建了一个控制台应用程序来发送邮件,现在我有了html模板 我从数据库中获取数据是根据用户现在如何在html中绑定这些数据。 我的代码是.net 在控制台应用程序中发送Html邮件,.net,email,c#-4.0,.net,Email,C# 4.0,我已经创建了一个控制台应用程序来发送邮件,现在我有了html模板 我从数据库中获取数据是根据用户现在如何在html中绑定这些数据。 我的代码是 private static void Main(string[] args) { // Get All User var userList = BLLMailAlert.GetAllSubscribedUser(); // Send UserID as Parameter and Find
private static void Main(string[] args)
{
// Get All User
var userList = BLLMailAlert.GetAllSubscribedUser();
// Send UserID as Parameter and Find Tenders For Users
foreach (var user in userList)
{
BLLMailAlert.GetAllTenderForPaidUser(user.UserID);
// For Each AlertId Find Template Name and EmailId
foreach (var usr in userList)
{
var detail = BLLMailAlert.GetUserAllDetail(usr.AlertID);
// According to TemplateName FIND Template and Write Tender in that Template
switch (usr.UserTypeTemplate.TemplateType.TemplateTypeName)
{
case "TemplateForPaidUser.htm":
{
// Send Template in Mail To Each User
var fromEmail = string.Empty;
var toEmail = string.Empty;
toEmail = usr.Email;
fromEmail = Convert.ToString(ConfigurationManager.AppSettings["adminmail"]);
const string TemplatePath = "[Path to resource]";
var mailContent = new StreamReader(TemplatePath).ReadToEnd();
mailContent += detail;
BLLMailAlert.SendTenderAlert(fromEmail, toEmail, mailContent);
}
break;
}
}
}
}
//function to send mail
// I have Class named Mail which i am using to send mail
public static bool SendTenderAlert(string fromEmail, string toEmail, string mailContent)
{
try
{
var currentDate = System.DateTime.Now.Date;
Mail.SendHtmlMail(fromEmail, toEmail, "TenderKhabar | Fresh Tenders Of |" + currentDate, mailContent);
return true;
}
catch
{
return false;
}
}
您可以使用
String.Replace
。每个人都这样做。这听起来可能不优雅,但它有很多优点
亲爱的{Customer.Name},
,但它可以是亲爱的$(Customer.Name),
在一个例子中,我必须为模板编写代码
if
s和for
s,并且我使用了正则表达式和字符串替换。您可以使用string.replace
。每个人都这样做。这听起来可能不优雅,但它有很多优点
亲爱的{Customer.Name},
,但它可以是亲爱的$(Customer.Name),
在一个例子中,我必须为模板编写代码
if
s和for
s,并且我使用了regex和string replace。给未来读者的提示:如果这样做(顺便问一下,为什么要避免使用T4文本模板?)不要忘记转义字符串(例如,如果它们来自用户输入,它们可能包含HTML中禁止的字符).给未来读者的提示:如果您这样做(顺便说一句,为什么要避免使用T4文本模板?)不要忘记转义字符串(例如,如果它们来自用户输入,则可能包含HTML中禁止的字符)。