0

我知道如何在 asp.net 中发送电子邮件,但我不知道如何从网站中提取详细信息并将其放入电子邮件正文。我正在那里建立一个购物车网站,我想通过电子邮件将发票发送给用户。请帮忙。

4

2 回答 2

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);
     }
 }
于 2013-11-26T07:08:24.120 回答
0

您可以为新行使用 '\n' 并放置 4 个空格(制表符)使用 '\t'

然后你在邮件函数中发送字符串,如:

try
    {

        MailMessage mail = new MailMessage();

        //set the addresses
        mail.From = new MailAddress("username@domain_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 添加。

于 2012-04-28T23:20:02.333 回答