我正在尝试从c#发送电子邮件问题是当我在我的身体中进行iam格式化时似乎没有任何解决方案,我可以使用此断行也需要在邮件正文中创建表。下面是我的代码,其中body1是我的电子邮件内容。
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);
}
答案 0 :(得分:1)
在MailMessage设置中输入IsBodyHtml = true
mailMessage = new MailMessage()
{
From = new MailAddress(senderAddress),
Subject = subject,
Body = message,
IsBodyHtml = true
};
在消息中使用html标签而不是纯文本。
希望这有帮助。
答案 1 :(得分:1)
您的问题是您没有将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