Link 使用 C# 通过电子邮件发送在 Visual studio 中有效,但在 IIS 中无效,尽管我发送了正确的 link 和 appsever 池以允许 32 位应用程序
Link sent via email using C# works in Visual studio but not in IIS though I send the correct link and appsever pool to allow 32bit application
这是我使用的代码,在 VS 2008 中运行良好,但在 IIS7 中运行不佳。
public void sendm (string ename,string leaveid, string empid,string tomail,string frommail)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(MServerName);
mail.From = new MailAddress(frommail);
mail.To.Add(tomail);
mail.Subject = "Leave Request: "+ename+" Has Applied for Leave";
mail.IsBodyHtml = true;
mail.Body = "Dear Manager </br> </br> Please approve leave of: "+ename+" : <a href='http://localhost:60457/blbtrc/LeaveApproval.aspx?leavetrnumber="+leaveid+"&UserID="+empid+"'>Click to Approve </a>";
SmtpServer.Port = Convert.ToInt32(MServerPort);
SmtpServer.EnableSsl = false;
SmtpServer.Send(mail);
}
我的站点配置在 IIS 的 82 端口上,如果我将 mail.body 更改为
mail.Body = "Dear Manager </br> </br> Please approve leave of: " + ename + " : <a href='http://localhost:82/blbtrc/LeaveApproval.aspx?leavetrnumber=" + leaveid + "&UserID=" + empid + "'>Click to Approve </a>";
当我使用 IIS 时,点击邮件中的 link 没有打开包含信息的网格。
我在页面中的网格视图如下:
<asp:GridView ID="LeaveDate" runat="server" AutoGenerateColumns="False"
DataKeyNames="Leave_ID" DataSourceID="LeaveApprovalSD1">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="Leave_ID" HeaderText="Leave_ID"
SortExpression="Leave_ID" ReadOnly="True" InsertVisible="False" />
<asp:BoundField DataField="LeaveTRNumber" HeaderText="LeaveTRNumber"
SortExpression="LeaveTRNumber"/>
<asp:BoundField DataField="Employee_Name" HeaderText="Employee_Name"
SortExpression="Employee_Name" />
<asp:BoundField DataField="Leave_StDate" HeaderText="Leave_StDate"
SortExpression="Leave_StDate"/>
<asp:BoundField DataField="Leave_EdDate" HeaderText="Leave_EdDate"
SortExpression="Leave_EdDate"/>
<asp:BoundField DataField="Numb_Of_days" HeaderText="Numb_Of_days"
SortExpression="Numb_Of_days" />
<asp:BoundField DataField="LeaveStatus" HeaderText="LeaveStatus"
SortExpression="LeaveStatus" />
<asp:BoundField DataField="Rejection_Summary"
HeaderText="Rejection_Summary" SortExpression="Rejection_Summary" />
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="RowEditing" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<ItemTemplate>
<asp:Button ID="ApproveBtn" runat="server" Text="Approve" OnClick="Approve_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="LeaveApprovalSD1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
DeleteCommand="DELETE FROM [LeaveTracking] WHERE [Leave_ID] = @original_Leave_ID AND (([LeaveTRNumber] = @original_LeaveTRNumber) OR ([LeaveTRNumber] IS NULL AND @original_LeaveTRNumber IS NULL)) AND (([Employee_Name] = @original_Employee_Name) OR ([Employee_Name] IS NULL AND @original_Employee_Name IS NULL)) AND (([Leave_StDate] = @original_Leave_StDate) OR ([Leave_StDate] IS NULL AND @original_Leave_StDate IS NULL)) AND (([Leave_EdDate] = @original_Leave_EdDate) OR ([Leave_EdDate] IS NULL AND @original_Leave_EdDate IS NULL)) AND (([Numb_Of_days] = @original_Numb_Of_days) OR ([Numb_Of_days] IS NULL AND @original_Numb_Of_days IS NULL)) AND (([LeaveStatus] = @original_LeaveStatus) OR ([LeaveStatus] IS NULL AND @original_LeaveStatus IS NULL)) AND (([Rejection_Summary] = @original_Rejection_Summary) OR ([Rejection_Summary] IS NULL AND @original_Rejection_Summary IS NULL))"
InsertCommand="INSERT INTO [LeaveTracking] ([LeaveTRNumber], [Employee_Name], [Leave_StDate], [Leave_EdDate], [Numb_Of_days], [LeaveStatus], [Rejection_Summary]) VALUES (@LeaveTRNumber, @Employee_Name, @Leave_StDate, @Leave_EdDate, @Numb_Of_days, @LeaveStatus, @Rejection_Summary)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT [Leave_ID], [LeaveTRNumber], [Employee_Name], [Leave_StDate], [Leave_EdDate], [Numb_Of_days], [LeaveStatus], [Rejection_Summary] FROM [LeaveTracking] WHERE ([LeaveTRNumber] = @LeaveTRNumber) and ([LeaveStatus] =@LeaveStatus)"
UpdateCommand="UPDATE [LeaveTracking] SET [Rejection_Summary] = @Rejection_Summary WHERE [Leave_ID] = @original_Leave_ID AND (([LeaveTRNumber] = @original_LeaveTRNumber) OR ([LeaveTRNumber] IS NULL AND @original_LeaveTRNumber IS NULL)) AND (([Employee_Name] = @original_Employee_Name) OR ([Employee_Name] IS NULL AND @original_Employee_Name IS NULL)) AND (([Leave_StDate] = @original_Leave_StDate) OR ([Leave_StDate] IS NULL AND @original_Leave_StDate IS NULL)) AND (([Leave_EdDate] = @original_Leave_EdDate) OR ([Leave_EdDate] IS NULL AND @original_Leave_EdDate IS NULL)) AND (([Numb_Of_days] = @original_Numb_Of_days) OR ([Numb_Of_days] IS NULL AND @original_Numb_Of_days IS NULL)) AND (([LeaveStatus] = @original_LeaveStatus) OR ([LeaveStatus] IS NULL AND @original_LeaveStatus IS NULL)) AND (([Rejection_Summary] = @original_Rejection_Summary) OR ([Rejection_Summary] IS NULL AND @original_Rejection_Summary IS NULL))">
<SelectParameters>
<asp:QueryStringParameter Name="LeaveTRNumber" QueryStringField="LtrNo"
Type="String" />
<asp:QueryStringParameter Name="LeaveStatus" QueryStringField="LeaveStatus"
Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="original_Leave_ID" Type="Int32" />
<asp:Parameter Name="original_LeaveTRNumber" Type="String" />
<asp:Parameter Name="original_Employee_Name" Type="String" />
<asp:Parameter DbType="Date" Name="original_Leave_StDate" />
<asp:Parameter DbType="Date" Name="original_Leave_EdDate" />
<asp:Parameter Name="original_Numb_Of_days" Type="String" />
<asp:Parameter Name="original_LeaveStatus" Type="String" />
<asp:Parameter Name="original_Rejection_Summary" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Rejection_Summary" Type="String" />
<asp:Parameter Name="original_Leave_ID" Type="Int32" />
<asp:Parameter Name="original_LeaveTRNumber" Type="String" />
<asp:Parameter Name="original_Employee_Name" Type="String" />
<asp:Parameter DbType="DateTime" Name="original_Leave_StDate" />
<asp:Parameter DbType="DateTime" Name="original_Leave_EdDate" />
<asp:Parameter Name="original_Numb_Of_days" Type="String" />
<asp:Parameter Name="original_LeaveStatus" Type="String" />
<asp:Parameter Name="original_Rejection_Summary" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="LeaveTRNumber" Type="String" />
<asp:Parameter Name="Employee_Name" Type="String" />
<asp:Parameter DbType="Date" Name="Leave_StDate" />
<asp:Parameter DbType="Date" Name="Leave_EdDate" />
<asp:Parameter Name="Numb_Of_days" Type="String" />
<asp:Parameter Name="LeaveStatus" Type="String" />
<asp:Parameter Name="Rejection_Summary" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
我在代码中捕获值如下:
protected void Page_Load(object sender, EventArgs e)
{
LTRno = Request.QueryString["leavetrnumber"].ToString()
if (!IsPostBack)
{
----------
Levaedatasource.SelectParameters["Leave_StDate"].DefaultValue = lstdate;
Levaedatasource.SelectParameters["Leave_StDate2"].DefaultValue = leddate;
LeaveApprovalSD1.SelectParameters["LeaveTRNumber"].DefaultValue = LTRno;
}
}
您的电子邮件生成代码使用查询字符串参数 leavetrnumber
和 UserID
生成 URL。但是您 SqlDataSource
的查询字符串参数正在寻找 LtrNo
和 LeaveStatus
。您可能需要这些来匹配您的 SqlDataSource
以检索结果。获取 Page_Load
函数中的值不会自动将它们传递给数据源中的参数。
查看您更新后的问题,您有两个选择。您可以像我上面描述的那样获取匹配的查询字符串。或者您可以确保在将值分配给参数后重新绑定 GridView。例如:
var LTRno = Request.QueryString["leavetrnumber"]
LeaveApprovalSD1.SelectParameters["LeaveTRNumber"].DefaultValue = LTRno;
LeaveDate.DataBind();
我不喜欢这种方法,因为在 SqlDataSource 中使用 QueryStringParameter 的全部意义在于您不需要让代码连接参数。你应该让参数匹配。
另一件需要修复的事情是电子邮件生成代码中的 URL 需要指向服务器。 Localhost 是环回地址 127.0.0.1,并且将始终转到本地机器。您需要提供服务器的 dns 名称(可能是主机名,或者您可能设置了别名)。
这是我使用的代码,在 VS 2008 中运行良好,但在 IIS7 中运行不佳。
public void sendm (string ename,string leaveid, string empid,string tomail,string frommail)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(MServerName);
mail.From = new MailAddress(frommail);
mail.To.Add(tomail);
mail.Subject = "Leave Request: "+ename+" Has Applied for Leave";
mail.IsBodyHtml = true;
mail.Body = "Dear Manager </br> </br> Please approve leave of: "+ename+" : <a href='http://localhost:60457/blbtrc/LeaveApproval.aspx?leavetrnumber="+leaveid+"&UserID="+empid+"'>Click to Approve </a>";
SmtpServer.Port = Convert.ToInt32(MServerPort);
SmtpServer.EnableSsl = false;
SmtpServer.Send(mail);
}
我的站点配置在 IIS 的 82 端口上,如果我将 mail.body 更改为
mail.Body = "Dear Manager </br> </br> Please approve leave of: " + ename + " : <a href='http://localhost:82/blbtrc/LeaveApproval.aspx?leavetrnumber=" + leaveid + "&UserID=" + empid + "'>Click to Approve </a>";
当我使用 IIS 时,点击邮件中的 link 没有打开包含信息的网格。
我在页面中的网格视图如下:
<asp:GridView ID="LeaveDate" runat="server" AutoGenerateColumns="False"
DataKeyNames="Leave_ID" DataSourceID="LeaveApprovalSD1">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="Leave_ID" HeaderText="Leave_ID"
SortExpression="Leave_ID" ReadOnly="True" InsertVisible="False" />
<asp:BoundField DataField="LeaveTRNumber" HeaderText="LeaveTRNumber"
SortExpression="LeaveTRNumber"/>
<asp:BoundField DataField="Employee_Name" HeaderText="Employee_Name"
SortExpression="Employee_Name" />
<asp:BoundField DataField="Leave_StDate" HeaderText="Leave_StDate"
SortExpression="Leave_StDate"/>
<asp:BoundField DataField="Leave_EdDate" HeaderText="Leave_EdDate"
SortExpression="Leave_EdDate"/>
<asp:BoundField DataField="Numb_Of_days" HeaderText="Numb_Of_days"
SortExpression="Numb_Of_days" />
<asp:BoundField DataField="LeaveStatus" HeaderText="LeaveStatus"
SortExpression="LeaveStatus" />
<asp:BoundField DataField="Rejection_Summary"
HeaderText="Rejection_Summary" SortExpression="Rejection_Summary" />
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="RowEditing" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<ItemTemplate>
<asp:Button ID="ApproveBtn" runat="server" Text="Approve" OnClick="Approve_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="LeaveApprovalSD1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
DeleteCommand="DELETE FROM [LeaveTracking] WHERE [Leave_ID] = @original_Leave_ID AND (([LeaveTRNumber] = @original_LeaveTRNumber) OR ([LeaveTRNumber] IS NULL AND @original_LeaveTRNumber IS NULL)) AND (([Employee_Name] = @original_Employee_Name) OR ([Employee_Name] IS NULL AND @original_Employee_Name IS NULL)) AND (([Leave_StDate] = @original_Leave_StDate) OR ([Leave_StDate] IS NULL AND @original_Leave_StDate IS NULL)) AND (([Leave_EdDate] = @original_Leave_EdDate) OR ([Leave_EdDate] IS NULL AND @original_Leave_EdDate IS NULL)) AND (([Numb_Of_days] = @original_Numb_Of_days) OR ([Numb_Of_days] IS NULL AND @original_Numb_Of_days IS NULL)) AND (([LeaveStatus] = @original_LeaveStatus) OR ([LeaveStatus] IS NULL AND @original_LeaveStatus IS NULL)) AND (([Rejection_Summary] = @original_Rejection_Summary) OR ([Rejection_Summary] IS NULL AND @original_Rejection_Summary IS NULL))"
InsertCommand="INSERT INTO [LeaveTracking] ([LeaveTRNumber], [Employee_Name], [Leave_StDate], [Leave_EdDate], [Numb_Of_days], [LeaveStatus], [Rejection_Summary]) VALUES (@LeaveTRNumber, @Employee_Name, @Leave_StDate, @Leave_EdDate, @Numb_Of_days, @LeaveStatus, @Rejection_Summary)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT [Leave_ID], [LeaveTRNumber], [Employee_Name], [Leave_StDate], [Leave_EdDate], [Numb_Of_days], [LeaveStatus], [Rejection_Summary] FROM [LeaveTracking] WHERE ([LeaveTRNumber] = @LeaveTRNumber) and ([LeaveStatus] =@LeaveStatus)"
UpdateCommand="UPDATE [LeaveTracking] SET [Rejection_Summary] = @Rejection_Summary WHERE [Leave_ID] = @original_Leave_ID AND (([LeaveTRNumber] = @original_LeaveTRNumber) OR ([LeaveTRNumber] IS NULL AND @original_LeaveTRNumber IS NULL)) AND (([Employee_Name] = @original_Employee_Name) OR ([Employee_Name] IS NULL AND @original_Employee_Name IS NULL)) AND (([Leave_StDate] = @original_Leave_StDate) OR ([Leave_StDate] IS NULL AND @original_Leave_StDate IS NULL)) AND (([Leave_EdDate] = @original_Leave_EdDate) OR ([Leave_EdDate] IS NULL AND @original_Leave_EdDate IS NULL)) AND (([Numb_Of_days] = @original_Numb_Of_days) OR ([Numb_Of_days] IS NULL AND @original_Numb_Of_days IS NULL)) AND (([LeaveStatus] = @original_LeaveStatus) OR ([LeaveStatus] IS NULL AND @original_LeaveStatus IS NULL)) AND (([Rejection_Summary] = @original_Rejection_Summary) OR ([Rejection_Summary] IS NULL AND @original_Rejection_Summary IS NULL))">
<SelectParameters>
<asp:QueryStringParameter Name="LeaveTRNumber" QueryStringField="LtrNo"
Type="String" />
<asp:QueryStringParameter Name="LeaveStatus" QueryStringField="LeaveStatus"
Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="original_Leave_ID" Type="Int32" />
<asp:Parameter Name="original_LeaveTRNumber" Type="String" />
<asp:Parameter Name="original_Employee_Name" Type="String" />
<asp:Parameter DbType="Date" Name="original_Leave_StDate" />
<asp:Parameter DbType="Date" Name="original_Leave_EdDate" />
<asp:Parameter Name="original_Numb_Of_days" Type="String" />
<asp:Parameter Name="original_LeaveStatus" Type="String" />
<asp:Parameter Name="original_Rejection_Summary" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Rejection_Summary" Type="String" />
<asp:Parameter Name="original_Leave_ID" Type="Int32" />
<asp:Parameter Name="original_LeaveTRNumber" Type="String" />
<asp:Parameter Name="original_Employee_Name" Type="String" />
<asp:Parameter DbType="DateTime" Name="original_Leave_StDate" />
<asp:Parameter DbType="DateTime" Name="original_Leave_EdDate" />
<asp:Parameter Name="original_Numb_Of_days" Type="String" />
<asp:Parameter Name="original_LeaveStatus" Type="String" />
<asp:Parameter Name="original_Rejection_Summary" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="LeaveTRNumber" Type="String" />
<asp:Parameter Name="Employee_Name" Type="String" />
<asp:Parameter DbType="Date" Name="Leave_StDate" />
<asp:Parameter DbType="Date" Name="Leave_EdDate" />
<asp:Parameter Name="Numb_Of_days" Type="String" />
<asp:Parameter Name="LeaveStatus" Type="String" />
<asp:Parameter Name="Rejection_Summary" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
我在代码中捕获值如下:
protected void Page_Load(object sender, EventArgs e)
{
LTRno = Request.QueryString["leavetrnumber"].ToString()
if (!IsPostBack)
{
----------
Levaedatasource.SelectParameters["Leave_StDate"].DefaultValue = lstdate;
Levaedatasource.SelectParameters["Leave_StDate2"].DefaultValue = leddate;
LeaveApprovalSD1.SelectParameters["LeaveTRNumber"].DefaultValue = LTRno;
}
}
您的电子邮件生成代码使用查询字符串参数 leavetrnumber
和 UserID
生成 URL。但是您 SqlDataSource
的查询字符串参数正在寻找 LtrNo
和 LeaveStatus
。您可能需要这些来匹配您的 SqlDataSource
以检索结果。获取 Page_Load
函数中的值不会自动将它们传递给数据源中的参数。
查看您更新后的问题,您有两个选择。您可以像我上面描述的那样获取匹配的查询字符串。或者您可以确保在将值分配给参数后重新绑定 GridView。例如:
var LTRno = Request.QueryString["leavetrnumber"]
LeaveApprovalSD1.SelectParameters["LeaveTRNumber"].DefaultValue = LTRno;
LeaveDate.DataBind();
我不喜欢这种方法,因为在 SqlDataSource 中使用 QueryStringParameter 的全部意义在于您不需要让代码连接参数。你应该让参数匹配。
另一件需要修复的事情是电子邮件生成代码中的 URL 需要指向服务器。 Localhost 是环回地址 127.0.0.1,并且将始终转到本地机器。您需要提供服务器的 dns 名称(可能是主机名,或者您可能设置了别名)。