使用asp.net发送邮件以及嵌入的图像和HTML文本。
我已经这样尝试过:
public ActionResult Contact(tblCredential data)
{
string emailAddress = data.UserName;
string password = data.Password;
if (!string.IsNullOrEmpty(emailAddress))
{
//Send email to consultancy
string htmlText = "<img src='R:/MVC@2/EmailWithHtmlBody/EmailWithHtmlBody/images/message.jpg'></img>
<h1>Thank you</h1>";
string from = "******@gmail.com"; // Your Mail-id here emailAddress
string @touser = emailAddress; // To Mail-id
MailMessage mail = new MailMessage(from, touser);
{
mail.Subject = emailAddress + " sent a message";
mail.Body = htmlText;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient { Host = "smtp.gmail.com", EnableSsl = true };
NetworkCredential networkCredential = new NetworkCredential(from, "*****"); //Your Password here..
smtp.UseDefaultCredentials = false;
smtp.Credentials = networkCredential;
smtp.Port = 587;
smtp.Send(mail);
}
}
return RedirectToAction("Index");
}
电子邮件已发送,但HTML代码不起作用。邮件中显示HTML标签。帮我。
试试这个模板。
使用smtp.port = 25有帮助
try
{
MailMessage msg = new MailMessage ();
MailAddress fromAdd = new MailAddress("[email protected]");
msg.[To].Add("[email protected]");
msg.Subject = "Choose Session Members";
msg.From = fromAdd;
msg .IsBodyHtml = true;
msg.Priority = MailPriority.Normal;
msg .BodyEncoding = Encoding.Default;
msg.Body = "<center><table><tr><td><h1>Your Message</h1><br/><br/></td></tr>";
msg.Body = msg.Body + "</table></center>";
SmtpClient smtpClient = new SmtpClient ("smtp.yourserver.com", "25");
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
smtpClient .DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(msg);
smtpClient.Dispose();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句