用Asp.net高效地发送Html格式的Email(二)
首先我创建了一个类,它只包含一些公共属性,作为数据容器。
public class UserCredentialsCS
{
public UserCredentialsCS()
{
//constructor here
}
private string _Username;
private string _Password;
private int _ExistingCredentials;
public string Username
{
get
{
return _Username;
}
set
{
_Username = value;
}
}
public string Password
{
get
{
return _Password;
}
set
{
_Password = value;
}
}
public int ExistingCredentials
{
get
{
return _ExistingCredentials;
}
set
{
_ExistingCredentials = value;
}
}
}
{
public UserCredentialsCS()
{
//constructor here
}
private string _Username;
private string _Password;
private int _ExistingCredentials;
public string Username
{
get
{
return _Username;
}
set
{
_Username = value;
}
}
public string Password
{
get
{
return _Password;
}
set
{
_Password = value;
}
}
public int ExistingCredentials
{
get
{
return _ExistingCredentials;
}
set
{
_ExistingCredentials = value;
}
}
}
接下来是EmaiDataCs ,它将提供一个GetUserCredentials方法,该方法包含在前文中提到的存储过程需要的参数,
public class EmailDataCS
{
public EmailDataCS()
{
//constructor logic here
}
public UserCredentialsCS GetUserCredentials(string emailAddress)
{
SqlConnection Connection = new
SqlConnection(ConfigurationSettings.AppSettings["Conn"]);
SqlCommand Command = new SqlCommand("GetLostCredentials", Connection);
Command.CommandType = CommandType.StoredProcedure;
SqlParameter ParameterEmailAddress = new SqlParameter("@EmailAddress",
SqlDbType.NVarChar, 50);
Command.Parameters.Add(ParameterEmailAddress);
ParameterEmailAddress.Direction = ParameterDirection.Input;
ParameterEmailAddress.Value = emailAddress;
SqlParameter ParameterUsername = new SqlParameter("@Username",
SqlDbType.NVarChar, 50);
Command.Parameters.Add(ParameterUsername);
ParameterUsername.Direction = ParameterDirection.Output;
SqlParameter ParameterPassword = new SqlParameter("@Password",
SqlDbType.NVarChar, 50);
Command.Parameters.Add(ParameterPassword);
ParameterPassword.Direction = ParameterDirection.Output;
SqlParameter ParameterExistingCredentials = new
SqlParameter("@ExistingCredentials", SqlDbType.Int, 4);
Command.Parameters.Add(ParameterExistingCredentials);
ParameterExistingCredentials.Direction = ParameterDirection.Output;
Connection.Open();
Command.ExecuteNonQuery();
Connection.Close();
UserCredentialsCS Credentials = new UserCredentialsCS();
Credentials.Username = Convert.ToString(ParameterUsername.Value);
Credentials.Password = Convert.ToString(ParameterPassword.Value);
Credentials.ExistingCredentials =
Convert.ToInt32(ParameterExistingCredentials.Value);
return Credentials;
}
}
{
public EmailDataCS()
{
//constructor logic here
}
public UserCredentialsCS GetUserCredentials(string emailAddress)
{
SqlConnection Connection = new
SqlConnection(ConfigurationSettings.AppSettings["Conn"]);
SqlCommand Command = new SqlCommand("GetLostCredentials", Connection);
Command.CommandType = CommandType.StoredProcedure;
SqlParameter ParameterEmailAddress = new SqlParameter("@EmailAddress",
SqlDbType.NVarChar, 50);
Command.Parameters.Add(ParameterEmailAddress);
ParameterEmailAddress.Direction = ParameterDirection.Input;
ParameterEmailAddress.Value = emailAddress;
SqlParameter ParameterUsername = new SqlParameter("@Username",
SqlDbType.NVarChar, 50);
Command.Parameters.Add(ParameterUsername);
ParameterUsername.Direction = ParameterDirection.Output;
SqlParameter ParameterPassword = new SqlParameter("@Password",
SqlDbType.NVarChar, 50);
Command.Parameters.Add(ParameterPassword);
ParameterPassword.Direction = ParameterDirection.Output;
SqlParameter ParameterExistingCredentials = new
SqlParameter("@ExistingCredentials", SqlDbType.Int, 4);
Command.Parameters.Add(ParameterExistingCredentials);
ParameterExistingCredentials.Direction = ParameterDirection.Output;
Connection.Open();
Command.ExecuteNonQuery();
Connection.Close();
UserCredentialsCS Credentials = new UserCredentialsCS();
Credentials.Username = Convert.ToString(ParameterUsername.Value);
Credentials.Password = Convert.ToString(ParameterPassword.Value);
Credentials.ExistingCredentials =
Convert.ToInt32(ParameterExistingCredentials.Value);
return Credentials;
}
}
EmailDataCs创建了SqlConnection和SqlCommand对象,然后定义Comand对象的CommandType(存储过程),我加了存储过程需要的参数,并指定了他们各自的ParameterDirection(input或者output)。存储过程GetLostCredential被执行,数据库连接被关闭,实例化一个UserCredentialCs类,公共属性通过SqlParameter被赋值,这些参数我在EmailDataCs类的GetUserCredentials方法中定义。这样,我的UserCredentialCs类(译注:应该是对象)存放了从存储过程中获取的数据。
创建一个具有输入框的aspx页面获取失去的密码
实际上,创建一个获取密码的“接口”是非常容易的。创建一个名为SimpleHtmlEmail.aspx的页面,该页面提供一个text输入框和一个butotn,以便获取一个Email地址并发送到服务器端。下面的截图显示了它的样子。
注意,我将在讨论各种不同的处理发送Html格式邮件方法的时候,多次利用这个页面。
通过StringBuilder类发送html格式的邮件
现在是真正用SimpleHtmlEmail.aspx页面发送html格式的邮件的时候了,把上面提到的东西真正利用起来。为了给你一个我正在讨论的问题的印象---下图显示了,如果你在aspx页面提供一个Email地址(它是有效的),将发送给你的html格式的邮件的样子。
创建一个名为SimpleHtmlEmail.aspx(译注:这个页面就是上文提到的页面)。为了在后台代码文件SimpleHtmlEmail.aspx.cs防止代码的混乱,并保持代码的隔离和清洁,我创建了一个新的名为SimpleHtmlEmailCs的文件.起初,这看起来没有必要,但当你看到包含html代码的最终代码的时候,你就会知道这是个不错的主意。
SimpleHtmlEmailCs的作用是发送html格式的E_mail,下面是代码片断。
public class SimpleHtmlEmailCS
{
public SimpleHtmlEmailCS()
{
//constructor
}
public void SendSimpleHtmlEmail(string EmailAddress)
{
HtmlEmailCS.EmailDataCS Credentials = new HtmlEmailCS.EmailDataCS();
HtmlEmailCS.UserCredentialsCS UserData =
Credentials.GetUserCredentials(EmailAddress);
if (UserData.ExistingCredentials > 0)
{
MailMessage MessageMail = new MailMessage();
MessageMail.From = "info@domain.com";
MessageMail.To = EmailAddress;
MessageMail.Subject = "Your credentials";
MessageMail.BodyFormat = MailFormat.Html;
StringBuilder MailBody = new StringBuilder();
MailBody.Append("<html><head></head><body> \n");
MailBody.Append("<span style=\"font-size: 11 px; font-family:Verdana,
Helvetica, sans-serif\">" + "Dear," + "<br><br>" + "Your account
information as it has been retrieved from the database:" +
"</span><br><br> \n");
MailBody.Append("<div style=\"font-size: 11 px; font-family:Verdana,
Helvetica, sans-serif\">" + "Username: " + UserData.Username +
"<br><br>" + "Password: " + UserData.Password + "</div> \n");
MailBody.Append("</body></html>");
MessageMail.Body = MailBody.ToString();
SmtpMail.SmtpServer = "mail-out.server.com";
SmtpMail.Send(MessageMail);
}
else
{
throw new Exception("E-mail address is invalid:
please specify the correct registered e-mail
address.");
}
}
}
{
public SimpleHtmlEmailCS()
{
//constructor
}
public void SendSimpleHtmlEmail(string EmailAddress)
{
HtmlEmailCS.EmailDataCS Credentials = new HtmlEmailCS.EmailDataCS();
HtmlEmailCS.UserCredentialsCS UserData =
Credentials.GetUserCredentials(EmailAddress);
if (UserData.ExistingCredentials > 0)
{
MailMessage MessageMail = new MailMessage();
MessageMail.From = "info@domain.com";
MessageMail.To = EmailAddress;
MessageMail.Subject = "Your credentials";
MessageMail.BodyFormat = MailFormat.Html;
StringBuilder MailBody = new StringBuilder();
MailBody.Append("<html><head></head><body> \n");
MailBody.Append("<span style=\"font-size: 11 px; font-family:Verdana,
Helvetica, sans-serif\">" + "Dear," + "<br><br>" + "Your account
information as it has been retrieved from the database:" +
"</span><br><br> \n");
MailBody.Append("<div style=\"font-size: 11 px; font-family:Verdana,
Helvetica, sans-serif\">" + "Username: " + UserData.Username +
"<br><br>" + "Password: " + UserData.Password + "</div> \n");
MailBody.Append("</body></html>");
MessageMail.Body = MailBody.ToString();
SmtpMail.SmtpServer = "mail-out.server.com";
SmtpMail.Send(MessageMail);
}
else
{
throw new Exception("E-mail address is invalid:
please specify the correct registered e-mail
address.");
}
}
}
SendSimpleHtmlMail有什么作用?首先我创建了一个EmailDataCs的实例,接下来我声明了一个UserCredentialCs类型的UserData数据,调用Credentials类的GetUserCredential方法,这个方法能够返回需要的数据。如果EmailDataCs的属性ExistingCredentials为1,则发送Mail.