2011-05-03 52 views

回答

1

我把以下代码放在这个网站的文章http://authors.aspalliance.com/stevesmith/articles/dotnetemailwebsite.asp,我发现这里有一个问题在这里问“用asp.net创建复杂的html电子邮件的最佳方法,怎么样?”,以及从这篇文章如何打印在ASP.NET 2.0 http://www.dotnetcurry.com/ShowArticle.aspx?ID=92。 我使用的是一个asp.net面板,这样我就可以只获取部分页面而不是整个页面,这样您就可以发送一个表格,而不必发送激活发送电子邮件或任何其他部分的按钮我不想发送的页面。 注意:发送的控件的任何样式属性必须直接设置到控件,而不是通过cssclass。 这是代码:

//Here I extract html of the control to be sent 
StringWriter stringWrite = new StringWriter(); 
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite); 
//pnlRpt is an asp.net panel containing the controls to be sent 
pnlRpt.RenderControl(htmlWrite); 
string htmlStr = stringWrite.ToString(); 

//Here I send the message whith the html of the table 
MailMessage msg = new MailMessage(); 
msg.From = new MailAddress("EmailOfSender"); 
msg.To.Add("emailOfReceiver"); 
msg.Subject = "your subject"; 
msg.Body = htmlStr; 
msg.IsBodyHtml = true; 
SmtpClient smtp = new SmtpClient(mailServer); 
smtp.Credentials = new System.Net.NetworkCredential(userName, usePass); 
smtp.Send(msg); 
msg.Dispose(); 

有了这个代码,我发送生成并在后台代码填充一个asp.net表,并将其完美地工作。

0

然后你可以把http请求发送到asp.net页面,你可以在邮件正文中嵌入响应。 您应该将邮件正文格式设置为html。

+0

你能给HTTP请求和嵌入在邮件正文中的反应的例子吗?谢谢。 – 2011-09-13 08:45:44

0

您可以在.NET中使用System.Mail.Net

发送HTML电子邮件

喜欢的东西

//create the mail message 
MailMessage mail = new MailMessage(); 

//set the addresses 
mail.From = new MailAddress("[email protected]"); 
mail.To.Add("[email protected]"); 

//set the content 
mail.Subject = "This is an email"; 
mail.Body = "this is a sample body with html in it. <b>This is bold</b> <font color=#336699>This is blue</font>"; 
mail.IsBodyHtml = true; 

//send the message 
SmtpClient smtp = new SmtpClient("address of email server possibly localhost"); 
smtp.Send(mail); 

注意身体HTML,所以你可以包含一个表。尽管不同的电子邮件客户端支持不同数量的HTML,但请注意格式,最好让事情简单些。

+0

是不是一个常规的HTML表格被认为保持简单的事情?谢谢 – 2011-05-04 12:32:03

0

试试下面的代码片段

protected void SendEmail(object sender, EventArgs e) 
    { 
     try 
     { 
      using (StringWriter sw = new StringWriter()) 
      { 
       using (HtmlTextWriter hw = new HtmlTextWriter(sw)) 
       { 
        // GridView5.DataBind(); 
        GridView5.RenderControl(hw);//this is my gridview name 
        StringReader sr = new StringReader(sw.ToString()); 
        MailMessage mm = new MailMessage("[email protected]",     "[email protected]");//From and To Mail id's 
        mm.Subject = "Quotation from INDIAN Departmental store,Udumalpet"; 
        mm.Body = "INDIAN Departmental store,Udumalpet:<hr />" + sw.ToString(); 
        mm.IsBodyHtml = true; 
        SmtpClient smtp = new SmtpClient(); 
        smtp.Host = "smtp.gmail.com"; 
        smtp.EnableSsl = true; 
        System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential(); 
        NetworkCred.UserName = "[email protected]"; 
        NetworkCred.Password = "perumalpudur"; 
        smtp.UseDefaultCredentials = true; 
        smtp.Credentials = NetworkCred; 
        smtp.Port = 587; 
        smtp.Send(mm); 
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Alert message", "alert('mail sent');", true); 
       } 
      } 
     } 
     catch (Exception) 
     { 
      ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Mail not sent!!!');", true); 
     } 
    } 

     public override void VerifyRenderingInServerForm(Control control) 
    { 
     /* Verifies that the control is rendered */ 
    } 
+1

请解释你的代码 – muzaffar 2015-09-04 10:10:00

相关问题