2012-04-28 236 views
0

我知道如何在asp.net中發送電子郵件,但我不知道如何從網站中提取詳細信息並將其放入電子郵件正文中。我正在那裏建立一個購物車網站,以便將發票通過電子郵件發送給用戶。請幫助。如何在asp.net中通過電子郵件發送郵件

+0

您是否使用窗體或MVC?您是否研究過如何在ASP.NET中處理「表單」數據? – 2012-04-28 23:09:23

+4

你確定你應該建立一個電子商務網站,如果訪問表單數據和字符串連接高於你的技能水平...? – 2012-04-28 23:09:47

回答

0

您可以對消息的字符串(如採購編號,金額,數量) 新線使用「\ n」和把4個空格(標籤)使用「\ t」

然後你寄的字符串中的郵件功能,如:

try 
    { 

     MailMessage mail = new MailMessage(); 

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

     //set the content 
     mail.Subject = "This is an subject"; 
     mail.Body = mail_content; 

     //send the message 
     SmtpClient smtp = new SmtpClient("127.0.0.1"); 

     //to authenticate we set the username and password properites on the SmtpClient 
     smtp.Credentials = new System.Net.NetworkCredential("username", "password"); 
     smtp.Send(mail); 
    } 
    catch (Exception ex) 
    { 
     //MessageBox.Show(ex.ToString()); 
    } 

不要忘記添加使用System.Net.Mail作爲它的需要。

0

您可以使用下面的代碼來生成發票:

public string GenerateInvoice(Invoice invoiceToGenerate) 
    { 
     StringBuilder invoiceHtml = new StringBuilder(); 
     invoiceHtml.Append("<b>INVOICE : ").Append(invoiceToGenerate.InvoiceId.ToString()).Append("</b><br />"); 
     invoiceHtml.Append("<b>DATE : </b>").Append(DateTime.Now.ToShortDateString()).Append("<br />"); 
     invoiceHtml.Append("<b>Invoice Amt :</b> $").Append(invoiceToGenerate.Total.ToString("#.00")).Append("<br />"); 
     invoiceHtml.Append("<br /><b>CUSTOMER CONTACT INFO:</b><br />"); 
     invoiceHtml.Append("<b>Name : </b>").Append(invoiceToGenerate.ContactName).Append("<br />"); 
     invoiceHtml.Append("<b>Phone : </b>").Append(invoiceToGenerate.ContactPhone).Append("<br />"); 
     invoiceHtml.Append("<b>Email : </b>").Append(invoiceToGenerate.ContactEmail).Append("<br />"); 
     invoiceHtml.Append("<b>Address : </b><br />").Append(invoiceToGenerate.ContactAddress1).Append("<br />"); 
     invoiceHtml.Append(invoiceToGenerate.ContactAddress2).Append("<br />"); 
     invoiceHtml.Append(invoiceToGenerate.ContactCity).Append(", ").Append(this.ContactStateProvince).Append(" ").Append(invoiceToGenerate.ContactZip).Append("<br />"); 
     invoiceHtml.Append("<br /><b>SHIP TO:</b><br />"); 
     invoiceHtml.Append("<b>Name : </b>").Append(invoiceToGenerate.ShipToName).Append("<br />"); 
     invoiceHtml.Append("<b>Address : </b><br />").Append(invoiceToGenerate.ShipToAddress1).Append("<br />"); 
     invoiceHtml.Append(invoiceToGenerate.ShipToAddress2).Append("<br />"); 
     invoiceHtml.Append(invoiceToGenerate.ShipToCity).Append(", ").Append(this.ShipToStateProvince).Append(" ").Append(invoiceToGenerate.ShipToZip).Append("<br />"); 
     invoiceHtml.Append("<br /><b>PRODUCTS:</b><br /><table><tr><th>Qty</th><th>Product</th></tr>"); 
     // InvoiceItem should be a collection property which contains list of invoice lines 
     foreach (InvoiceItem invoiceLine in invoiceToGenerate.InvoiceItems) 
     { 
      invoiceHtml.Append("<tr><td>").Append(invoiceLine.Quantity.ToString()).Append("</td><td>").Append(invoiceLine.ProductName).Append("</td></tr>"); 
     } 
     invoiceHtml.Append("</table>"); 
     return invoiceHtml.ToString(); 
    } 

,那麼你可以用這個方法把它列入你的電子郵件的身體像:

var mailBody = GenerateInvoice(invoiceObject); 

using (MailMessage mailMessage = new MailMessage(fromEmail, toEmail, subject, mailBody)) 
{ 
    mailMessage.IsBodyHtml = true; 
    using (SmtpClient smtpClient = new SmtpClient()) 
    { 
      smtpClient.Host = "127.0.0.1"; 
      smtp.Credentials = new System.Net.NetworkCredential("username", "password"); 
      smtpClient.Send(mailMessage); 
    } 
}