使用c#通过电子邮件发送的链接在Visual studio中工作,但在IIS中不工作,尽管我发送了正确的链接和appse

本文关键字:链接 工作 管我 appse IIS 但在 电子邮件 Visual studio 使用 | 更新日期: 2023-09-27 18:04:24

这是我使用的代码,在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,如果我更改邮件。Body to

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时,点击邮件中的链接不会打开带有信息的网格。

我在页面中的网格视图如下:

     <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>
                    &nbsp;<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;

    }
}

使用c#通过电子邮件发送的链接在Visual studio中工作,但在IIS中不工作,尽管我发送了正确的链接和appse

您的电子邮件生成代码生成一个URL与查询字符串参数leavetrnumberUserID。但是您的查询字符串参数为您的SqlDataSource正在寻找LtrNoLeaveStatus。您可能需要这些匹配您的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名称(可能是主机名,或者您可能已经设置了别名)。