如何在asp.net中通过EAsendmail发送邮件?

时间:2013-06-03 06:30:39

标签: asp.net smtp easendmail

我正在用这样的asp发送邮件:

    MailMessage msg = new MailMessage();

    msg.From = new MailAddress("from@email", "From Name");
    msg.Subject = "Subject";
    msg.To.Add("to@email");
    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 = "from@email";
    message.Subject = "Subject";

    ListDictionary replacements = new ListDictionary();
    replacements.Add("<%USERNAME%>", "ToUsername");//example of dynamic content for Username
    MailMessage msgHtml = message.CreateMailMessage("to@email", 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 = "...@...";
        oServer.Password = "123";
        oMail.To = "@";

oMail.Subject = ;
        oMail.HtmlBody =;

        oSmtp.SendMail(oServer, oMail);

我不想在代码中使用html,我想设计一个漂亮的html文件并发送它。

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码行读取HTML文件...然后将文本传递到您的电子邮件对象。

using system.IO;


string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.html");


oMail.HtmlBody = text;