我必须通过SMTP
服务器发送邮件。我可以用单个值发送它。但是,我希望以表格格式或其他结构发送List<>
值作为消息正文。我在下面包含了我的代码:
MailMessage mailObj = new MailMessage("abc@gmail.com", "xyz@gmail.com", "Reg : send mail",
"Emp Name :" + "Emp1" + Environment.NewLine + "Date From : " + Mon + "Date To : " + Fri);
SmtpClient SMTPServer = new SmtpClient("smtp.gmail.com", ***);
SMTPServer.Host = "smtp.gmail.com";
SMTPServer.EnableSsl = true;
SMTPServer.Timeout = 200000;
SMTPServer.Credentials = new System.Net.NetworkCredential("asd@gmail.com", "******");
SMTPServer.Send(mailObj);
我的值列表如下:
List<TimesheetValues> mailBody = new SampleDAO().GetDataForMail(dt1, satDate);
如何将此List<>
值包含在body中并发送?
我试着跟随:
List<TimesheetValues> Msg = new List<TimesheetValues>(); string strMsg = ""; int n=1;
foreach(var item in mailBody)
{
strMsg = strMsg + "<table><tr><td>" + n + "</td><td>" + item.Project_Name + "</td><td>" + item.Task_Name + "</td><td>" + item.Notes + "</td><td>" + item.Hrs_Worked + "</td></tr></table>";
n++;
}
strMsg = strMsg + "</br>";
MailMessage mailObj = new MailMessage("abc123@gmail.com", "xyz123@gmail.com", "Reg : Timesheet",
"Emp Name :" + "Emp1" + Environment.NewLine + "Date From : " + Mon + "Date To : " + Fri);
mailObj.Body = "Emp Name : " + "Emp1" + Environment.NewLine + "Date From : " + Date2 + " Date To : " + Date6 + Environment.NewLine + strMsg;
现在我得到了所有记录,但我有记录中的tr标记,而不是显示为表格。我怎么能实现这个目标呢?
任何人都可以帮助克服这一点.. 提前谢谢......
答案 0 :(得分:0)
如果您想再次将字符串读入列表,则序列化更好。
application: project-id
version: 1
runtime: php55 //Has to be php55 in order to work
module: my-module //You have to declare a module in order for the app to run
api_version: 1
您也可以在另一方反序列化:
using Newtonsoft.Json;
var json = JsonConvert.SerializeObject(yourList);
mailObj.Body = json;
答案 1 :(得分:0)
我尝试以下内容:
string strMsg = ""
strMsg = timsheetMail + " <table style=\"border-collapse:collapse; text-align:center;\" border=\"1\"><tr><td> Date </td><td> Working Hours </td><td> Task </td><td>Comments</td></tr>";
List<TimesheetValues> Msg = new List<TimesheetValues>(); int n=1;
foreach(var item in mailBody)
{
timesheetData = new TimesheetDetailModel();
timesheetData.Task_Id = matrix.Task_Id;
timesheetData.Hours = matrix.Hours;
//timesheetData.Date = matrix.Date.Date;
timesheetData.Date = matrix.Date.AddDays(1);
timesheetData.EmpId = Convert.ToInt32(Session["EmpId"].ToString());
timesheetData.Notes = matrix.Notes;
strMsg = strMsg+ " <tr><td> " + timesheetData.Date.ToString("dd/MM/yyyy") + "</td><td>" + timesheetData.Hours + "</td><td>" + task + "</td><td>" + timesheetData.Notes + "</td></tr>";
n++;
}
strMsg = strMsg + "</table>";
现在工作得很好..