如何在 asp .net 中将字符串作为电子邮件发送到 HTML 文件
How to add a string to a HTML file while sending it as email in asp .net
我正在使用 asp .net。我想在他注册时向用户的电子邮件发送验证码。我正在使用此代码发送电子邮件。我正在添加要发送的 html 文件。如何将验证码添加到此 HTML 文件?
这是我用来通过电子邮件发送 html 文件的代码。那么我如何添加一个 string(authenticationCode)
到 HTML 文件以将其发送给用户进行身份验证。
var fromAddress = new MailAddress("hamza230@gmail.com", "From Hamza");
var toAddress = new MailAddress("usama90@gmail.com", "To Usama");
const string fromPassword = "****";
const string subject = "email";
const string body = "hello world";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
MailMessage message = new MailMessage(fromAddress, toAddress);
message.Subject = subject;
message.Body = body;
message.BodyEncoding = Encoding.UTF8;
String plainBody = "Title";
AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainBody, Encoding.UTF8, "text/plain");
message.AlternateViews.Add(plainView);
MailDefinition msg = new MailDefinition();
msg.BodyFileName = "~/newsletter.html";
msg.IsBodyHtml = true;
msg.From = "usamaazam10@gmail.com";
msg.Subject = "Subject";
ListDictionary replacements = new ListDictionary();
MailMessage msgHtml = msg.CreateMailMessage("usamaazam10@gmail.com", replacements, new LiteralControl());
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(msgHtml.Body, Encoding.UTF8, "text/html");
LinkedResource logo = new LinkedResource(System.Web.HttpContext.Current.Server.MapPath("~/images/logo.jpg"), MediaTypeNames.Image.Jpeg);
logo.ContentId = "logo";
htmlView.LinkedResources.Add(logo);
LinkedResource cover = new LinkedResource(System.Web.HttpContext.Current.Server.MapPath("~/images/cover.jpg"), MediaTypeNames.Image.Jpeg);
cover.ContentId = "cover";
htmlView.LinkedResources.Add(cover);
message.AlternateViews.Add(htmlView);
smtp.Send(message);
您可以在邮件正文中添加验证码,例如:
...
message.Body = body + authenicationCode;
...
或者,如果您不将 body
创建为 const
,那么您可以在代码的顶部创建它,例如;
...
const string subject = "email";
string body = "Your authentication code is: " + authenicationCode;
...
您可以为小 html 页面执行此操作
string smalHhtml = "<body>"
+ "your some designing"
+ "your Code : " + code
+ "</body>";
Note: This is for small body html and sending verification code via email is usually small html.
我正在使用 asp .net。我想在他注册时向用户的电子邮件发送验证码。我正在使用此代码发送电子邮件。我正在添加要发送的 html 文件。如何将验证码添加到此 HTML 文件?
这是我用来通过电子邮件发送 html 文件的代码。那么我如何添加一个 string(authenticationCode)
到 HTML 文件以将其发送给用户进行身份验证。
var fromAddress = new MailAddress("hamza230@gmail.com", "From Hamza");
var toAddress = new MailAddress("usama90@gmail.com", "To Usama");
const string fromPassword = "****";
const string subject = "email";
const string body = "hello world";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
MailMessage message = new MailMessage(fromAddress, toAddress);
message.Subject = subject;
message.Body = body;
message.BodyEncoding = Encoding.UTF8;
String plainBody = "Title";
AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainBody, Encoding.UTF8, "text/plain");
message.AlternateViews.Add(plainView);
MailDefinition msg = new MailDefinition();
msg.BodyFileName = "~/newsletter.html";
msg.IsBodyHtml = true;
msg.From = "usamaazam10@gmail.com";
msg.Subject = "Subject";
ListDictionary replacements = new ListDictionary();
MailMessage msgHtml = msg.CreateMailMessage("usamaazam10@gmail.com", replacements, new LiteralControl());
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(msgHtml.Body, Encoding.UTF8, "text/html");
LinkedResource logo = new LinkedResource(System.Web.HttpContext.Current.Server.MapPath("~/images/logo.jpg"), MediaTypeNames.Image.Jpeg);
logo.ContentId = "logo";
htmlView.LinkedResources.Add(logo);
LinkedResource cover = new LinkedResource(System.Web.HttpContext.Current.Server.MapPath("~/images/cover.jpg"), MediaTypeNames.Image.Jpeg);
cover.ContentId = "cover";
htmlView.LinkedResources.Add(cover);
message.AlternateViews.Add(htmlView);
smtp.Send(message);
您可以在邮件正文中添加验证码,例如:
...
message.Body = body + authenicationCode;
...
或者,如果您不将 body
创建为 const
,那么您可以在代码的顶部创建它,例如;
...
const string subject = "email";
string body = "Your authentication code is: " + authenicationCode;
...
您可以为小 html 页面执行此操作
string smalHhtml = "<body>"
+ "your some designing"
+ "your Code : " + code
+ "</body>";
Note: This is for small body html and sending verification code via email is usually small html.