简体   繁体   English

如何通过电子邮件在asp.net c#中发送HTML表单?

[英]How to send an HTML form in asp.net c# by email?

In my ASP.Net C# website, I got an order form which I want to be send via email by submitting it. 在我的ASP.Net C#网站上,我收到了一份定单,希望通过提交该定单通过电子邮件发送。 The form elements are HTML and the calculations are done by JavaScript in client-side, and it has lots of form elements. 表单元素是HTML,并且计算是由JavaScript在客户端完成的,并且具有很多表单元素。

I want to be able to send the form and its filled contents by user, as PDF or JPG via email. 我希望能够通过电子邮件将格式及其填充内容以PDF或JPG格式发送给用户。

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 卡多

If you are looking to grab your html form and generate a PDF, my favorite tool is wkhtmltopdf . 如果您希望获取HTML表单并生成PDF,我最喜欢的工具是wkhtmltopdf You would have to have this installed on your server though (I'd love to know if others found a .net library for wkhtmltopdf! :]). 不过,您将不得不在服务器上安装此程序(我想知道其他人是否为wkhtmltopdf!.net库找到了.net库!)。

Once installed its commandline is very simple: 安装后,其命令行非常简单:

wkhtmltopdf www.myhomepage.com output.pdf

Calling it from .net is simple too, example: Calling wkhtmltopdf to generate PDF from HTML 从.net调用它也很简单,例如: 调用wkhtmltopdf从HTML生成PDF

There are also .net libraries like iTextSharp that you can use to generate pdfs and other static output types. 还有.net库,例如iTextSharp ,可用于生成pdf和其他静态输出类型。

Sounds like a long process. 听起来像一个漫长的过程。 Submit, convert to pdf, save to HDD, attache to email, send email. 提交,转换为pdf,保存为HDD,附加到电子邮件,发送电子邮件。 You can do it but I would consider sending a HTML email. 您可以做到,但我会考虑发送HTML电子邮件。 You just store the data (order) in your DB and send the email on a preformed email template. 您只需将数据(订单)存储在数据库中,然后通过预设的电子邮件模板发送电子邮件即可。 Let the end user print it to pdf or paper etc... 让最终用户将其打印为pdf或纸张等。

If you really want to do the pdf thing you will need to find a decent library as already suggested. 如果您真的想做pdf事情,您将需要找到一个已经建议的不错的库。

Well this is how you send the email. 好吧,这就是您发送电子邮件的方式。 I like to create a re-useable email class. 我喜欢创建一个可重复使用的电子邮件类。 You don't have to though if that doesn't suit your needs. 如果这不符合您的需求,则不必。 But hey, if your gonna create an email, Why not make it an object? 但是,嘿,如果您要创建电子邮件,为什么不使其成为对象?

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;
            }
        }
    }

Then you need to create your pdf or whatever you choose. 然后,您需要创建pdf或任何您选择的文件。 Save it to your HDD somewhere. 将其保存到您的硬盘中。 create your email and attach the file(s) as an email attachment (or just send the html body. Use stringReplace() to replace values in an email template. (#price #address #companyname etc). 创建您的电子邮件,并将文件作为电子邮件附件附加(或仅发送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();

I Hope that helps in some way... 我希望这会有所帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM