ASP.net从HTML表单发送电子邮件

时间:2016-01-02 06:57:18

标签: c# asp.net html5

下面是我的HTML表单。

<div class="form-area">  
<form role="form" method="post" action="" id="registerform">
<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Registration Form</h3>
<div class="form-inline">
<label class="radio">
<input value="property" type="radio" name="whatneed" checked="">&nbsp;&nbsp;&nbsp;Property Loan
</label>
&nbsp;&nbsp;&nbsp;<label class="radio">
<input value="automobile" type="radio" name="whatneed">&nbsp;&nbsp;&nbsp;Automobile Loan
</label>
</div>
<br>
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" placeholder="Your Budget" name="budget" required>
<span class="input-group-addon">.00</span>
</div>
</div>
<div class="form-group">
<input type="text" class="form-control" name="whenneed" placeholder="When are you planing to buy" required>
</div>
<div class="form-group">
<input type="text" class="form-control" name="location" placeholder="Location" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<textarea class="form-control" type="textarea" name="address" placeholder="Your Address" maxlength="140" rows="7"></textarea>                  
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Mobile Number" required>
</div>
<div class="form-group">
<input type="text" class="form-control" name="landline" placeholder="Landline Number" required>
</div>
<div class="form-group">
<input type="email" class="form-control" id="email" name="email" placeholder="Email" required>
</div>
   <button type="submit" id="submit" name="submit" class="btn btn-primary pull-right">Submit Form</button>
</form>
</div>

我需要在用户点击提交按钮时通过电子邮件发送此内容。我将此文件保存为.html。我是asp.net的新手。请帮我如何发送电子邮件。来自电子邮件ID将是固定的(例如:test@domainname.com)。

1 个答案:

答案 0 :(得分:2)

You can send email following way in ASP.NET.

 protected string SendEmail(string toAddress, string subject, string body)
   {
     string result = “Message Sent Successfully..!!”;
     string senderID = “SenderEmailID“;// use sender’s email id here..
     const string senderPassword = “Password“; // sender password here…
     try
     {
       SmtpClient smtp = new SmtpClient
       {
         Host = “smtp.gmail.com“, // smtp server address here…
         Port = 587,
         EnableSsl = true,
         DeliveryMethod = SmtpDeliveryMethod.Network,
         Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
          Timeout = 30000,
       };
       MailMessage message = new MailMessage(senderID, toAddress, subject, body);
       smtp.Send(message);
     }
     catch (Exception ex)
     {
       result = “Error sending email.!!!”;
     }
     return result;
    }