创建带自动退出功能的邮件列表
开发了带退出功能的邮件列表集合
引言
我需要为客户创建一个简单的邮件列表,并具备退订功能,所以我想分享一下我的经验。最初的想法是通过一个网页表单收集用户的姓名,然后将其存储在数据库中供以后使用。客户使用Microsoft Word创建通讯,并会使用邮件合并来向用户发送电子邮件。此外,由于反垃圾邮件法规的要求,能够退订是该解决方案的关键组成部分。我们将逐步介绍交互和流程,然后快速回顾数据库所需的字段、创建Linq to SQL类、可用的代码以及最后的邮件合并。
这里有三种交互方式:
- 用户注册
- 向用户发送电子邮件
- 用户通过超链接退订
这是交互的一个例子

背景
本次练习使用的技术是:
- SQL Server
- Visual Studio 2008 用于代码开发
- LINQ to SQL
Using the Code
我首先创建了一个SQL数据库,或者如果您需要,可以在现有的SQL数据库中创建一个新表。

然后,我创建了一个LINQ to SQL类文件,并使用Visual Studio将SQL表拖放到类中。

Joinmailinglist.aspx 文件
这是一个基本表单,您在此输入信息,并且会进行验证以确保输入了正确的信息。这段代码几乎可以输入在任何地方。
<form id="form1" runat="server">
<div>
<!-- This is the code for the form. There is a Text Box to collect the first name,
last name and email address. All fields are required and I am validating that the
email address is a valid format. -->
<asp:Table ID="Table1" runat="server">
<asp:TableRow><asp:TableCell>First Name: <asp:TextBox ID="txtFirstName"
runat="server" Width="60" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ErrorMessage="Enter First Name" Text="*" ControlToValidate="txtFirstName"
runat="server" /> </asp:TableCell>
<asp:TableCell>Last Name: <asp:TextBox ID="txtLastName" runat="server" Width="60" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtLastName" ErrorMessage="Enter Last Name" Text="*" />
</asp:TableCell> </asp:TableRow>
<asp:TableRow><asp:TableCell>Email: <asp:TextBox ID="txtEmail"
runat="server" Width="80" /><asp:RequiredFieldValidator ID="emailRequired"
runat="server" ControlToValidate="txtEmail" ErrorMessage="Email Needs Information"
Text="*"/> <asp:RegularExpressionValidator ID="emailexpression"
runat="server" ControlToValidate="txtEmail" ValidationExpression=".*@.*\..*"
ErrorMessage="Invalide Email Address" Text="*" /></asp:TableCell></asp:TableRow>
<asp:TableRow><asp:TableCell>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowSummary="true"
ShowMessageBox="true" runat="server" />
</asp:TableCell></asp:TableRow>
<asp:TableRow>
<asp:TableCell><asp:Button ID="btnSubmit" runat="server" OnClick="addMember"
CausesValidation="true" Height="30" Width="100" Text="Add Me" />
</asp:TableCell></asp:TableRow>
<asp:TableRow><asp:TableCell><asp:Label ID="lblDuplicate" runat="server" Text="">
</asp:Label></asp:TableCell></asp:TableRow>
</asp:Table>
</div>
</form>
joinmailinglist.aspx.cs 文件
这是所有工作发生的地方,用于将正确的信息捕获到SQL Server中。在我的if
…else
…语句中,我最初尝试比较特定值是否相等;然而,我发现仅仅计算返回变量的数量,或者在我这里是未返回的数量,我就成功解决了我的问题。稍微跳出思维定势就能非常高效地解决这个问题,而无需添加更多代码或转换字符串或创建数组。这里重要的部分是确保您添加using System.Linq
。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class joinmailinglist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{}protected void addMember(object sender, EventArgs e)
{
// here you are defining the classes for the database and the linq
mailinglistClassDataContext Class = new mailinglistClassDataContext();
mailinglistMember member = new mailinglistMember();
// Now we are going to add the data to the member
// Here we are going to let the system define a GUID for the unique user ID
member.memberID = new Guid();
// Here we are going to capture the user inputs and we are going to set
// these to lower case especially the email so that we can do a proper comparison later.
member.fname = txtFirstName.Text;
member.lname = txtLastName.Text;
member.email = txtEmail.Text;
// Here we are going to create the URL so we can later remove the user
// if they decide to opt out.
member.removeurl = "http://removeuser.aspx?code=" + member.memberID.ToString();
// Here we are going to use a LINQ query to search the class of mailinglistmembers
// for any emails that contain equal values of the text field and select it.
var duplicatecheck = from emails in Class.mailinglistMembers
where emails.email.Contains(txtEmail.Text)
select emails;
// Here we are going to check that the count of duplicate is equal to zero.
// If so then we are going to insert the member information into the class
// and then submit the changes to the database.
if (duplicatecheck.Count() == 0)
{
Class.mailinglistMembers.InsertOnSubmit(member);
Class.SubmitChanges();
// If you want to add a confirmation page it’s as easy as this:
Response.Redirect(“confirmation.aspx”);
}
else
{
lblDuplicate.Text = "Hey you have already entered your information.";
}
}
}
Removeme.aspx
这是用户登陆的页面,告知他们信息已被删除。
<form id="form1" runat="server">
<div><h1>Request for Removal</h1>
</div>
<div><asp:Label runat="server" id="lblRemoved" Text="" /></div>
<div> If you have done this in error <a href="joinmailinglist.aspx">Click Here</a>!</div>
<div><asp:Label runat="server" ID="lblNotfound" Text=""></asp:Label></div>
</form>
Removeme.aspx.cs
在此代码中,我们将返回数据库,并使用电子邮件/退订超链接中提供的GUID来删除用户。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class removeme : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// here your are going to make sure that the url has a value for code
if (Request.QueryString["code"] != null)
{
// here we are going to define the class
mailinglistClassDataContext member = new mailinglistClassDataContext();
// here we are going to take the code provided in the URL and convert it to a string
string id = Request.QueryString["code"];
// here is our LINQ statement that is going to find the GUID of the user
// which was provided in the email
var removeme = from maillist in member.mailinglistMembers
where maillist.memberID.ToString() == id
select maillist;
//Checking to see if a value was returned
if (removeme.Count() > 0)
{
member.mailinglistMembers.DeleteOnSubmit(removeme.First());
member.SubmitChanges();
lblRemoved.Text = "You have been removed from our mailing list.";
}else
{
// if no value is returned then we let them know a user was already removed.
lblNotfound.Text = "This user has already been removed or does not exist";
}
}else
{
Response.Redirect("home.aspx");
}
}
}
邮件合并
这是我遇到的一个难点,也是我花时间解决的问题。似乎无法将数据字段添加到超链接中。因此,我无法预先格式化URL文本,然后只插入唯一的ID或我这里的GUID。这就是为什么我必须在表中创建removeurl
字段。这样,我就可以无缝地将整个URL导入,并将其插入到邮件合并中,然后进行超链接。在邮件合并中对数据字段进行超链接并非易事,因此我创建了这些步骤来帮助您完成整个过程。我使用的Word版本是2010,但这应该适用于早期版本。
- 像往常一样开始您的邮件合并。
- 像往常一样选择您的数据库。
- 创建您的信函或电子邮件。
- 选择您希望放置退订链接的位置。
- 插入字段,在我们这个例子中是
<<removeurl>>
- 选择此字段。
- 右键单击并选择更新字段。
- 然后,您将从字段列表中选择超链接。
- 单击字段代码。
- 单击“确定”。
- 当您回来时,它会显示“错误!超链接引用无效。”
- 选择句号之前的所有内容。
- 更改文本。
- 您现在已经创建了一个合并的超链接。
- 如果您按住CTRL键并单击链接,它将触发。
- 如果您的网页在正常工作的服务器上,它将正常触发。
- 如果您仍在开发中,并在本地主机上工作,您必须返回到joinmailinglist.aspx.cs文件,并更改
removeurl
文本以反映您正在工作的位置,例如:https://:#####/folder/removeme.aspx?code=
关注点
有两个问题是我需要解决的:
- 如何检测电子邮件地址是否已在系统中以消除重复
- 如何在消息中放置一个特定的退订超链接
- 因为我无法在邮件合并中将ID合并到超链接中,所以我不得不创建一个数据库字段来捕获所有信息,以便能够在邮件合并中将其转换为超链接。
历史
- 2010年6月25日:初次发布