在发送系统生成的邮件时在 C# 中格式化邮件 body
Formatting Mail body in C# while sending system generated mail
我正在尝试从 c# 发送电子邮件问题是当我的 body
中的 iam 格式似乎没有在那里工作时我可以使用此断线的任何解决方案我也需要创建table 在邮件中 body。下面是我的代码,其中 body1 是我的 body 电子邮件内容。
var fromAddress = new MailAddress("something@gmail.com", "Name");
const string fromPassword = "pwd123";
const string subject = "System generated test mail ";
string email = bind_email(analyst);
string body1 = "Hi " + analyst.ToString();
body1 = body1 + "<br/>";
body1 = body1 + " This is system generated test mail for " + Session["TaskAssigned"].ToString();
body1 = body1 + " To be competed before" + Session["Enddate"].ToString() + "<br/><br/> ";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage()
{
From = fromAddress,
Subject = subject,
Body = body1,
})
{
message.To.Add(email);
smtp.Send(message);
}
只需在 MailMessage 设置中输入 IsBodyHtml =true
mailMessage = new MailMessage()
{
From = new MailAddress(senderAddress),
Subject = subject,
Body = message,
IsBodyHtml = true
};
Use html tags instead of plain text inside message.
希望对您有所帮助。
您的问题是您没有将 MailMessage.IsBodyHtml 分配给 true。您可以使用真正的 html.
格式化您的正文
我曾经对来自 html 的生成电子邮件进行格式化。首先创建一个test.html:
<html>
<body>
<p>
Hi #analyst#
<br/>
This is a system generated test mail for #task_assigned# to be completed before #enddate#
<br/>
<br/>
</p>
</body>
</html>
然后替换需要的数据即可。
这是我编辑的代码:
var fromAddress = new MailAddress("something@gmail.com", "Name");
const string fromPassword = "pwd123";
const string subject = "System generated test mail ";
string email = bind_email(analyst);
System.IO.StreamReader sr = new System.IO.StreamReader(Server.MapPath("~/App_Data/test.html"));
string body1 = sr.ReadToEnd();
body1 = body1.Replace("#analyst#", analyst.ToString());
body1 = body1.Replace("#task_assigned#", Session["TaskAssigned"].ToString());
body1 = body1.Replace("#enddate#", Session["Enddate"].ToString());
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage()
{
From = fromAddress,
Subject = subject,
Body = body1,
IsBodyHtml = true
})
{
message.To.Add(email);
smtp.Send(message);
}
Hope this helps
我正在尝试从 c# 发送电子邮件问题是当我的 body
中的 iam 格式似乎没有在那里工作时我可以使用此断线的任何解决方案我也需要创建table 在邮件中 body。下面是我的代码,其中 body1 是我的 body 电子邮件内容。
var fromAddress = new MailAddress("something@gmail.com", "Name");
const string fromPassword = "pwd123";
const string subject = "System generated test mail ";
string email = bind_email(analyst);
string body1 = "Hi " + analyst.ToString();
body1 = body1 + "<br/>";
body1 = body1 + " This is system generated test mail for " + Session["TaskAssigned"].ToString();
body1 = body1 + " To be competed before" + Session["Enddate"].ToString() + "<br/><br/> ";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage()
{
From = fromAddress,
Subject = subject,
Body = body1,
})
{
message.To.Add(email);
smtp.Send(message);
}
只需在 MailMessage 设置中输入 IsBodyHtml =true
mailMessage = new MailMessage()
{
From = new MailAddress(senderAddress),
Subject = subject,
Body = message,
IsBodyHtml = true
};
Use html tags instead of plain text inside message.
希望对您有所帮助。
您的问题是您没有将 MailMessage.IsBodyHtml 分配给 true。您可以使用真正的 html.
格式化您的正文我曾经对来自 html 的生成电子邮件进行格式化。首先创建一个test.html:
<html>
<body>
<p>
Hi #analyst#
<br/>
This is a system generated test mail for #task_assigned# to be completed before #enddate#
<br/>
<br/>
</p>
</body>
</html>
然后替换需要的数据即可。
这是我编辑的代码:
var fromAddress = new MailAddress("something@gmail.com", "Name");
const string fromPassword = "pwd123";
const string subject = "System generated test mail ";
string email = bind_email(analyst);
System.IO.StreamReader sr = new System.IO.StreamReader(Server.MapPath("~/App_Data/test.html"));
string body1 = sr.ReadToEnd();
body1 = body1.Replace("#analyst#", analyst.ToString());
body1 = body1.Replace("#task_assigned#", Session["TaskAssigned"].ToString());
body1 = body1.Replace("#enddate#", Session["Enddate"].ToString());
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage()
{
From = fromAddress,
Subject = subject,
Body = body1,
IsBodyHtml = true
})
{
message.To.Add(email);
smtp.Send(message);
}
Hope this helps