Tôi muốn thực hiện chức năng cơ bản với biểu mẫu liên hệ đơn giản và gửi biểu mẫu email cho ai đó. Điều này là khá dễ dàng để làm trong asp.net, tuy nhiên tôi gặp rắc rối khi tôi tải nó như là một điều khiển người dùng. Bạn có một ví dụ tốt tôi có thể xem? Cảm ơn bạn!Biểu mẫu liên hệ trong SiteFinity C#
Trả lời
Cũng giống như bạn có trong trang asp.net bình thường, mẫu giả sử bạn đang sử dụng phiên bản Sitefinity mới nhất và bạn có RadScriptManager hoặc ScriptManager trên trang chính của mình.
Thứ nhất đây là của tôi dưới hình thức ví dụ codebehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Text;
using System.ComponentModel;
public partial class UserControls_LandingPage_contactForm : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
bool bSent = false;
try
{
//create the email and add the settings
var email = new MailMessage();
email.From = new MailAddress(FromEmail);
email.To.Add(new MailAddress(FromEmail));
email.Subject = Subject;
email.IsBodyHtml = true;
//build the body
var sBody = new StringBuilder();
sBody.Append("<strong>Contact Details</strong><br /><br />");
sBody.AppendFormat("Needs: {0}<br />", cboConsultationType.SelectedValue);
sBody.AppendFormat("Name: {0}<br />", txtName.Text);
sBody.AppendFormat("Email: {0}<br />", txtEmail.Text);
sBody.AppendFormat("Number: {0}<br />", txtPhone.Text);
sBody.AppendFormat("Comment: {0}<br />", txtMsg.Text);
email.Body = sBody.ToString();
//send the email
var smtpServer = new SmtpClient();
smtpServer.Send(email);
//mark as sent ok
bSent = true;
}
catch (Exception ex)
{
//send any errors back
//add your own custom handling of errors;
}
//let the end user know if it was a success
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('" + (bSent ? SuccessText : FailureText) + "');", true);
}
//properties
public string FromEmail
{
get { return _fromEmail; }
set { _fromEmail = value; }
}
public string Subject
{
get { return _subject; }
set { _subject = value; }
}
public string SuccessText
{
get { return _successText; }
set { _successText = value; }
}
public string FailureText
{
get { return _failureText; }
set { _failureText = value; }
}
//fields
private string _fromEmail = "[email protected]";
private string _subject = "Website Enquiry";
private string _successText = "Thank you for submitting your details we will be in touch shortly.";
private string _failureText = "There was a problem submitting your details please try again shortly.";
}
Mã ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactForm.ascx.cs" Inherits="UserControls_LandingPage_contactForm" %>
<fieldset>
<div class="focus">
<label>
I need...</label>
<asp:DropDownList ID="cboConsultationType" runat="server" CssClass="select sub web">
<asp:ListItem Value="I Need A New Web Site">A completely new website</asp:ListItem>
<asp:ListItem Value="Web Site Upgrade">My website upgraded</asp:ListItem>
<asp:ListItem Value="Application Design">An application </asp:ListItem>
<asp:ListItem Value="An ecommerce website">New Ecommerce website</asp:ListItem>
<asp:ListItem Value="Other">Other</asp:ListItem>
</asp:DropDownList>
</div>
<ul>
<li>
<asp:Label EnableViewState="false" ID="lblErrorMessage" runat="server"></asp:Label>
</li>
<li>
<asp:Label EnableViewState="false" ID="lblName" AssociatedControlID="txtName" runat="server"
Text="Name"></asp:Label>
<asp:TextBox ID="txtName" runat="server" ValidationGroup="ContactValidation"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ValidationGroup="ContactValidation"
ControlToValidate="txtName" ErrorMessage="Name is required">*</asp:RequiredFieldValidator>
</li>
<li>
<asp:Label ID="lblPhone" runat="server" AssociatedControlID="txtPhone" EnableViewState="false"
Text="Phone"></asp:Label>
<asp:TextBox ID="txtPhone" runat="server" ValidationGroup="ContactValidation"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorPhone" runat="server" ValidationGroup="ContactValidation"
ControlToValidate="txtPhone" ErrorMessage="Phone is required">*</asp:RequiredFieldValidator>
</li>
<li>
<asp:Label ID="lblEmail" runat="server" AssociatedControlID="txtEmail" EnableViewState="false"
Text="Email"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server" ValidationGroup="ContactValidation"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ValidationGroup="ContactValidation"
ControlToValidate="txtEmail" ErrorMessage="Email is required">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorEmail" runat="server"
ControlToValidate="txtEmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ValidationGroup="ContactValidation" ErrorMessage="Email address is invalid">*</asp:RegularExpressionValidator>
</li>
<li>
<asp:Label ID="lblMsg" runat="server" AssociatedControlID="txtMsg" EnableViewState="false"
Text="How can we assist you?"></asp:Label>
<asp:TextBox ID="txtMsg" runat="server" TextMode="MultiLine" Rows="5" Wrap="true"></asp:TextBox>
</li>
<li>
<asp:Button ID="btnSubmit" runat="server" EnableViewState="false" CssClass="submit"
Text="Send" ValidationGroup="ContactValidation" OnClick="btnSubmit_Click" />
</li>
</ul>
</fieldset>
Sau đó, chỉ các thành phần khác mà bạn cần phải cảnh giác là trong web.config bạn cần phải sửa đổi cài đặt system.net cho email:
<system.net>
<mailSettings>
<smtp from="[email protected]">
<network host="smtp.yourdomain.com" userName="Your_Username" password="Your_Password" port="25" />
</smtp>
</mailSettings>
</system.net>
Sau đó tải lên kiểm soát hoặc sửa đổi web.config của bạn. Sau đó, cung cấp máy chủ SMTP của bạn là tất cả các thiết lập một cách chính xác các hình thức nên gửi không có vấn đề.
Tôi hy vọng điều này sẽ giúp bạn.
Tôi nghĩ rằng một cách tiếp cận tốt là sử dụng người gửi thư được kết hợp. Lợi nhuận là bạn có thể định cấu hình cài đặt bên trong Quản trị-> Cài đặt-> Nâng cao-> Hệ thống-> SMTP
var smtpSettings = Config.Get<SystemConfig>().SmtpSettings;
MailMessage message = new MailMessage();
message.From = new MailAddress(smtpSettings.UserName);
message.To.Add(new MailAddress(toEmail));
StringBuilder sb = new StringBuilder();
sb.AppendFormat(body);
message.Subject = subject;
message.Body = sb.ToString();
message.IsBodyHtml = true;
message.BodyEncoding = Encoding.Unicode;
message.SubjectEncoding = Encoding.Unicode;
EmailSender.Get().Send(message);
Bạn cũng có thể sử dụng thông báo.
- 1. địa chỉ trả lời trong biểu mẫu liên hệ php
- 2. Biểu mẫu liên hệ bằng ruby, sinatra và haml
- 3. Chống spam/Giảm thiểu - Biểu mẫu liên hệ?
- 4. Umbraco vs Sitefinity
- 5. Mẫu C++, liên kết lỗi
- 6. mẫu liên hệ Sanitize mà không mysql_real_escape_string
- 7. Mẫu liên hệ email không có PHP
- 8. thay đổi bố cục cột của biểu mẫu liên hệ trong Magento
- 9. Làm cách nào để gửi email thành công trong biểu mẫu liên hệ qua CodeIgniter?
- 10. Không thể gửi thư trong biểu mẫu liên hệ PHP bằng cách sử dụng 000webhost webmail
- 11. Biểu tượng khay hệ thống trong C++
- 12. C#, 'Biểu mẫu' không tồn tại trong hệ thống không gian tên.windows
- 13. Mẫu liên hệ Wordpress 7 mã ngắn tùy chỉnh
- 14. Làm thế nào để tạo mẫu hệ thống biểu mẫu bằng C#?
- 15. Mẫu biểu thức C++
- 16. C#: Biểu mẫu kính?
- 17. mã hóa biểu mẫu liên hệ php với khóa công khai của tôi
- 18. Biểu mẫu liên hệ Ajax/PHP đơn giản có xác thực
- 19. Sử dụng jQuery Fancybox hoặc Lightbox để hiển thị biểu mẫu liên hệ
- 20. PHPMailer v. Mail() cho một Biểu mẫu Liên hệ đơn giản
- 21. Cách đóng biểu mẫu trong C#
- 22. Tên khóa đẹp trong C# (Biểu mẫu)
- 23. mẫu biểu và C++ 11
- 24. Làm cách nào để vô hiệu hóa tùy chọn biểu mẫu liên hệ cá nhân trong biểu mẫu chỉnh sửa người dùng?
- 25. Bao gồm liên kết trong nhãn biểu mẫu?
- 26. Dự án mẫu để chọn nhiều liên hệ
- 27. Biểu mẫu Biểu mẫu Windows không hiển thị trong Thanh tác vụ C#
- 28. Khởi động Trình bảo vệ màn hình hệ thống từ C# Biểu mẫu Windows
- 29. HttpWebRequest và xác thực biểu mẫu trong C#
- 30. Biểu mẫu được làm tròn với hệ thống Shadow
Trả lời tuyệt vời, Sean! – Slavo
Cảm ơn Sean! Rất hữu ích :-) –
Không vấn đề gì tôi nghĩ bạn phải đánh dấu tôi là câu trả lời, miễn là bạn nghĩ nó đúng;) –