如何通过asp.net发送电子邮件

时间:2012-04-28 23:08:13

标签: asp.net email

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

2 个答案:

答案 0 :(得分:0)

您可以创建关于消息的字符串(例如购买ID,金额,数量) 对于新行使用'\ 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。

答案 1 :(得分: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);
     }
 }