5

我有一个页面 ( somePage.aspx),我需要已生成为电子邮件正文的内容

<div id="DV_TimeReportWraper" runat="server" style="display:block">
    <table id="TBL_UsersinTblTime">
       <tr id="TR_UsersinTblTime">
         <td id="TD_H_Name" class="Dg">
             name                
         </td>
         <td id="TD_H_UserID" class="Dg">
             ID
         </td>
         <td id="TD_H_Status" class="Dg">
             initial Stage
         </td>
         <td id="TD_H_SignOutAutoExeState" class="Dg">
             current Stage
         </td>
       </tr>
       <%
           if(edata != null)
               for (int idx=0;idx<edata.Count;idx++) {
                   var row = edata[idx];
                   bool bgcl = (idx % 2) == 0;
                   string BgCol = "";
                   if (bgcl)
                       BgCol = "#70878F";
                   else
                       BgCol = "#E6E6B8";
       %>
       <tr style=" background-color:<%=BgCol%>">
           <td id="TD_Name">
               <% = row["name"] %>
            </td>
            <td id="TD_UserID">
                <%= row["UserId"]%>
            </td>
            <td id="TD_Status">
                <%
                    int uidForSrc = Convert.ToInt32(row["UserId"]);
                    string src = "images/SignedOutNoFrame.png";
                    if (UserDidnotSignOutTimeOut(uidForSrc))
                        src = "images/didnotSignOut.png";
                 %>
                 <input type="image" src="<% =src %>" style="width:25px" />
             </td>
             <td id="TD_SignOutAutoExeState" >
                 <% 
                     string EexcSrc = "";
                     string inputType ="hidden";
                     //this updates if needed then returns true= needed update false = isn't need
                     if (UpdatedTimeOutOKForUSER(uidForSrc))
                     {
                         inputType = "image";
                         excSrc = "/images/SignedOutNoFrame.png";
                     }
                 %>
                 <input type="<%=inputType %>" src="<%=EexcSrc %>" style="width:25px" />
             </td>
        </tr>
        <%
            if (idx == edata.Count - 1)
                sendLog();
            }
        %>
    </table>
</div>

代码sendLog()

public void sendLog()
{
    mail.aReciver="username@gmail.com";
    mail.bSubject="ttest";
    mail.cBody = DV_UsersInTblTime.InnerHtml;
    mail.HentalSend();
}

我无法获得要分配的内容的价值mail.cBody
这是在说价值不是文字等。

这就是我在外部类中使用的方法,直到最后一次尝试将页面内容的功能添加为主体之前,它都可以正常工作,如何在这里实现所需的结果?

public static class mail
{
    public static string aReciver, bSubject, cBody;
    public static void HentalSend()
    {
        string SmtpServer = "smtp.gmail.com";
        int port = 587;
        string sender = "Sender@domain.com";
        string ReCiver = aReciver;
        string Subject = bSubject;
        string Body = cBody;
        string account = "mail@domain.com";
        string Pass = "123456";
        Send(SmtpServer, port, account, Pass, sender, Receiver, Subject, Body);
        ... //send() is another relevant method to this question, it does rest of mail settings
    }
}
4

5 回答 5

8

此代码会将生成的动态控件的 HTML 放入字符串变量中。

StringBuilder stringBuilder = new StringBuilder();
StringWriter writer = new StringWriter(stringBuilder);
HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
try {
    DV_TimeReportWraper.RenderControl(htmlWriter);
} catch (HttpException generatedExceptionName) {
}

string DV_TimeReportWraper_innerHTML = stringBuilder.ToString();

然后只需DV_TimeReportWraper_innerHTML用作电子邮件的正文

如果此控件有子控件,您可能必须创建一个循环。更多信息在这里:http: //msdn.microsoft.com/en-us/library/htwek607.aspx#Y472

于 2012-11-04T03:49:08.720 回答
1

我在这里假设您正在加载该页面,然后在页面加载后或在页面加载期间或在页面生命周期的某些部分期间尝试运行 sendLog 方法。试图获取尚未呈现的内容的 innerhtml 将无法正常工作。

本质上,您需要先渲染页面,然后获取内容。

[编辑] 我正在更新我的答案以包括我将如何做到这一点。而不是编写 .NET 代码来强制渲染,正如我在上面的评论中所说的那样。让 ASP.NET 为您完成肮脏的工作。

    using(WebClient c=new WebClient()){
    var result=c.DownloadString(@"http://yourdomain.com/somePage.aspx/");
}

现在,您可以将该调用的结果用作电子邮件的正文。

于 2012-11-03T20:59:17.700 回答
0

试试这个 ,

添加命名空间

using System;
using System.Net.Mail;
using System.IO;
using System.Configuration;

邮件功能

public void SendHTMLMail()
{
StreamReader reader = new StreamReader(Server.MapPath("~/somePage.aspx"));
string readFile = reader.ReadToEnd();
string myString = "";
myString = readFile;
myString = myString.Replace("$$Admin$$", "Sajan SJ");
myString = myString.Replace("$$CompanyName$$", "RFT");
myString = myString.Replace("$$Email$$", "sajan@roomfortekki.com");
myString = myString.Replace("$$Website$$", "http://www.roomfortekki.com");
MailMessage Msg = new MailMessage();
MailAddress fromMail = new MailAddress("sajan@roomfortekki.com");
// Sender e-mail address.
Msg.From = fromMail;
// Recipient e-mail address.
Msg.To.Add(new MailAddress("sajan@roomfortekki.com"));
// Subject of e-mail
Msg.Subject = "Send Mail with HTML File";
Msg.Body = myString.ToString();
Msg.IsBodyHtml = true;
string sSmtpServer = "";
sSmtpServer = "10.2.69.121";
SmtpClient a = new SmtpClient();
a.Host = sSmtpServer;
a.Send(Msg);
reader.Dispose();
}
于 2012-11-09T04:48:52.343 回答
0

哈德拉达在这里指出了正确的方向

在 .ASPX 文件下的服务器上,添加所需的本地化文件

\App_LocalResources
    somePage.aspx.en.resx
    somePage.aspx.es.resx
    somePage.aspx.fr.resx
somePage.aspx
  • 添加您的翻译
  • 将它们插入到您的 aspx 文件中

按照建议调用您的文件,但使用查询字符串除外

/somePage.aspx?culture=fr

在 somepage.aspx 中,在 OnLoad() 或 OnInit() 集中:

var culture = System.Globalization.CultureInfo.CreateSpecificCulture(Request["culture"])

if (culture != null)
   System.Threading.Thread.CurrentThread.CurrentUICulture = culture;

瞧!

于 2012-11-09T15:48:07.547 回答
0

发送电子邮件按钮点击代码:

    protected void BtnSendIngMail_Click(object sender, EventArgs e)
    {

       StringBuilder stringBuilder = new StringBuilder();
        StringWriter writer = new StringWriter(stringBuilder);
        HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
        try
        {

            DivSendMail.RenderControl(htmlWriter);
        }
        catch (HttpException generatedExceptionName)
        {
            e.ToString();
        }

        try
        {

            string DefProductTemp_Html = stringBuilder.ToString();

            //send mail
            string smtpServer = Convert.ToString(DotNetNuke.Common.Globals.HostSettings["SMTPServer"]);
            string uid = Convert.ToString(DotNetNuke.Common.Globals.HostSettings["SMTPUsername"]);
            string upwd = Convert.ToString(DotNetNuke.Common.Globals.HostSettings["SMTPPassword"]);

          //  DotNetNuke.Services.Mail.Mail.SendMail(uid, txtMail.Text, uid, "Proforma Invoice", DefProductTemp_Html, "", DotNetNuke.Services.Mail.MailFormat.Html.ToString(), "", "", "", "");

            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            System.Net.Mail.SmtpClient client = new SmtpClient();
            mail.To.Add(txtMail.Text);
            mail.From = new System.Net.Mail.MailAddress(uid);
            mail.Subject = ddlSubList.SelectedItem.Text;
            mail.SubjectEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;


            mail.Body = DefProductTemp_Html;

            //mail.BodyEncoding = System.Text.Encoding.UTF8;

            //mail.Priority = System.Net.Mail.MailPriority.High;
            client.Credentials = new System.Net.NetworkCredential(uid, upwd);

            //client.Host = "localhost";
            client.Host = "smtp.gmail.com";
            client.Port = 587;
            client.EnableSsl = true;
            client.Send(mail);
        }
        catch (Exception ex)
        {
           // catch
        }
于 2013-12-10T10:24:58.287 回答