2
我在ASP中發送郵件是這樣的:如何通過在asp.net中的EAsendmail發送郵件?
MailMessage msg = new MailMessage();
msg.From = new MailAddress("[email protected]", "From Name");
msg.Subject = "Subject";
msg.To.Add("[email protected]");
msg.BodyEncoding = Encoding.UTF8;
String plainBody = "Body of plain email";
AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainBody, Encoding.UTF8, "text/plain");
msg.AlternateViews.Add(plainView);
//now create the HTML version
MailDefinition message = new MailDefinition();
message.BodyFileName = "~/MailTemplates/template1.htm";
message.IsBodyHtml = true;
message.From = "[email protected]";
message.Subject = "Subject";
ListDictionary replacements = new ListDictionary();
replacements.Add("<%USERNAME%>", "ToUsername");//example of dynamic content for Username
MailMessage msgHtml = message.CreateMailMessage("[email protected]", replacements, new LiteralControl ());
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(msgHtml.Body, Encoding.UTF8, "text/html");
LinkedResource imgRes = new LinkedResource(Server.MapPath("~/MailTemplates/images/imgA.jpg"), System.Net.Mime.MediaTypeNames.Image.Jpeg);
imgRes.ContentId = "imgA";
imgRes.ContentType.Name = "imgA.jpg";
imgRes.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(imgRes);
msg.AlternateViews.Add(htmlView);
//sending prepared email
SmtpClient smtp = new SmtpClient();//It reads the SMPT params from Web.config
smtp.Send(msg);
這沒關係。
我應該改變郵件格式,但我如何以新格式添加html文件?
新格式:
SmtpClient oSmtp = new SmtpClient();
SmtpMail oMail = new SmtpMail("TryIt"); //should be TryIt
SmtpServer oServer = new SmtpServer("mail.ir");
oServer.Port = 465;
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
oMail.From = "";
oServer.User = "[email protected]";
oServer.Password = "123";
oMail.To = "@";
oMail.Subject = ;
oMail.HtmlBody =;
oSmtp.SendMail(oServer, oMail);
我不想裏面的代碼使用HTML,我想設計一個漂亮的html文件,並將其發送。
我想將一些參數傳遞給html文件,例如用戶名。我如何傳遞參數? – NASRIN
爲什麼不能創建一個函數...... string myFunction(user,pwd)返回文本;做所有的處理函數,然後在你的電子郵件類中調用它。 oMail.HtmlBody = myFunction(「我的用戶名」,「我的密碼」); – highwingers
您可以在HTML文件(如### expression ###)中創建和使用特定表達式,並在您使用de String.Replace方法的C#代碼中替換值(text.Replace(「### expression# ##「, 用戶名))。 –