1

In my ASP.Net C# website, I got an order form which I want to be send via email by submitting it. The form elements are HTML and the calculations are done by JavaScript in client-side, and it has lots of form elements.

I want to be able to send the form and its filled contents by user, as PDF or JPG via email.

Maybe the form should be captured as a snapshot from client-side as an image or a print file, then maybe the file can be send from server-side by email.

I'd highly appreciate if you give me a practical described solution. Here is some parts of code as sample:

   <div id="cblDomain">
    <input id="cblDomain_1" value="10" onchange="subsumDomain()" type="checkbox" name="cblDomain" checked="checked"><label for="cblDomain_1">com - 10</label><br>
    <input id="cblDomain_2" value="10" onchange="subsumDomain()" type="checkbox" name="cblDomain"><label for="cblDomain_2">net - 10</label><br>
    <input id="cblDomain_3" value="5" onchange="subsumDomain()" type="checkbox" name="cblDomain"><label for="cblDomain_3">info - 5</label><br>
    <input id="cblDomain_4" value="10" onchange="subsumDomain()" type="checkbox" name="cblDomain"><label for="cblDomain_4">me - 10</label>
   </div>

<select name="ddlDomainPeriod" onchange="subsumDomain()" id="ddlDomainPeriod">
    <option value="1">1 yr</option>
    <option value="2">2 yrs</option>
    <option value="3">3 yrs</option>
    <option value="4">4 yrs</option>
    <option value="5">5 yrs</option>
</select>
 <div name="sum" id="sumDomain">10</div>

<script type="text/javascript">
    function subsumDomain() {
        var _sum = 0;
        var _cblDomain = document.getElementsByName('cblDomain');
        for (i = 0; i < _cblDomain.length; i++) {
            if (_cblDomain[i].checked == true)
                _sum += Number(_cblDomain[i].value);
        }
        var _domainPeriod = Number(document.getElementById('ddlDomainPeriod').options[document.getElementById('ddlDomainPeriod').selectedIndex].value);
        document.getElementById('sumDomain').innerHTML = moneyConvert(_sum * _domainPeriod);

        subTotal();
    }

    function subsumHost() {
        var _hostPrice = Number(document.getElementById('ddlHost').options[document.getElementById('ddlHost').selectedIndex].value);
        var _hostPeriod = Number(document.getElementById('ddlHostPeriod').options[document.getElementById('ddlHostPeriod').selectedIndex].value);
        _hostDiscount = 0;
        if (_hostPeriod > 1)
            _hostDiscount = (_hostPrice * _hostPeriod) * 0.2;
        document.getElementById('sumHost').innerHTML = moneyConvert((_hostPrice * _hostPeriod) - _hostDiscount);

        subTotal();
    }
</script>

Any kind help would be highly appreciated (^_^)

Kardo

4

2 回答 2

1

听起来是一个漫长的过程。提交、转换为 pdf、保存到硬盘、附加到电子邮件、发送电子邮件。你可以这样做,但我会考虑发送 HTML 电子邮件。您只需将数据(订单)存储在您的数据库中,然后在预先形成的电子邮件模板上发送电子邮件。让最终用户将其打印到 pdf 或纸张等...

如果你真的想做 pdf 的事情,你需要像已经建议的那样找到一个像样的库。

好吧,这就是您发送电子邮件的方式。我喜欢创建一个可重复使用的电子邮件类。如果这不符合您的需求,您不必这样做。但是,嘿,如果你要创建一个电子邮件,为什么不把它变成一个对象呢?

using System.Net.Mail;
public class Email
    {

        public MailAddress From { get; set; }
        public MailAddress To { get; set; }
        public string Password { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public string Host { get; set; }
        public int Port { get; set;}
        public List<Attachment> attachmentsList = new List<Attachment>();


        public void send()
        {
            //send email
            var smtp = new SmtpClient
            {
                Host = Host,
                Port = Port,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new NetworkCredential(From.Address, Password),
                Timeout = 120000 //2mins
            };

            MailMessage message = new MailMessage(From.Address, To.Address);
            message.Subject = Subject;
            message.Body = Body;
            message.IsBodyHtml = true;

            foreach(Attachment attachment in attachmentsList){
                if (attachment != null)
                {
                    message.Attachments.Add(attachment);
                }                
            }

            try
            {
                smtp.Send(message);
            }
            catch(Exception e){

                throw e;
            }
        }
    }

然后你需要创建你的 pdf 或任何你选择的东西。将它保存到您的硬盘驱动器的某个地方。创建您的电子邮件并将文件附加为电子邮件附件(或仅发送 html 正文。使用 stringReplace() 替换电子邮件模板中的值。(#price #address #companyname 等)。

Email newMail = new Email();

newMail.From = new MailAddress("someone@somewhere.com", "Your Name");
newMail.Password = "your outgoing mail password");
newMail.To = new MailAddress("someone@somewhere.com", "Recipient Name");
newMail.Subject = "Your Subject";
newMail.Body = "Your email body";
newMail.Host = smtp.example.com;
newMail.Port = 123;

//add the attachments (example)
 foreach (string fileLocation in AttachmentsList)
 {
 newMail.attachmentsList.Add(new Attachment(fileLocation));
 }

 newMail.send();

我希望在某种程度上有所帮助......

于 2013-03-07T08:11:04.960 回答
0

如果您想获取 html 表单并生成 PDF,我最喜欢的工具是wkhtmltopdf。不过,您必须在您的服务器上安装它(我很想知道其他人是否找到了 wkhtmltopdf 的 .net 库!:])。

安装后,它的命令行非常简单:

wkhtmltopdf www.myhomepage.com output.pdf

从 .net 调用它也很简单,例如: Calling wkhtmltopdf to generate PDF from HTML

还有像iTextSharp这样的 .net 库,可用于生成 pdf 和其他静态输出类型。

于 2013-03-07T06:31:46.220 回答