ASP.NET邮件功能只向gmail发送邮件

本文关键字:gmail 功能 NET ASP | 更新日期: 2023-09-27 18:28:48

我正在点击按钮尝试此代码这在发送邮件的服务器上运行良好,但仅适用于Gmail帐户。有什么建议、更正或替换吗

如果代码的任何其他方面需要检查,请告诉我

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"  CellPadding="4" ForeColor="#333333" GridLines="None">
    <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
            <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            <asp:BoundField DataField="subject" HeaderText="subject" SortExpression="subject" />
            <asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
            <asp:BoundField DataField="pages" HeaderText="pages" SortExpression="pages" />
            <asp:BoundField DataField="time" HeaderText="time" SortExpression="time" />
            <asp:BoundField DataField="words" HeaderText="words" SortExpression="words" />
            <%--<asp:BoundField DataField="file_path" HeaderText="file_path" SortExpression="file_path" />--%>
            <asp:TemplateField>
                <ItemTemplate>
                        <asp:LinkButton ID="lnkDownload" runat="server" CommandArgument='<%# Eval("file_path")%>' CommandName="Download" OnClick="DownloadFile" Text='<%#
     Eval("file_path") %>'></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>


try
{           
    MailMessage mailMsg = new MailMessage("ahsan.sykov@gmail.com",GridView1.SelectedRow.Cells[3].Text);
    mailMsg.Subject = TextBox1.Text;
    mailMsg.Body = TextBox2.Text + "    " + DateTime.Now.ToString();

    SmtpClient smtp = new SmtpClient();
    smtp.Host = "relay-hosting.secureserver.net";
    smtp.Credentials = new System.Net.NetworkCredential("ahsan.sykov@gmail.com", "mypassword");
    smtp.Send(mailMsg);
    Label5.Text="Email Sent!";
}
catch (Exception ex)
{
    Label5.Text=ex.Message;
}

<mailSettings>
   <smtp deliveryMethod="Network">
     <network host="smtp.gmail.com" port="25" defaultCredentials="false"/>
   </smtp>
</mailSettings>

ASP.NET邮件功能只向gmail发送邮件

将此工作代码放入

public void SendToMail()
    {
        try
        {
            const string vMailFm = "admin@example.com";
            var vMailTo = ((Session["EmailId"].ToString() == "") ? "admin@example.com" : Session["EmailId"].ToString());
            MailMessage vMail = new MailMessage(vMailFm, vMailTo);
            var vDetails = "";
            vDetails += "Result Information";

            vMail.Subject = subject;
            vMail.Body = vDetails;
            vMail.IsBodyHtml = true;
            SmtpClient vSmpt = new SmtpClient();
            System.Net.NetworkCredential smtpUser = new System.Net.NetworkCredential("admin@example.com", "1234567");
            vSmpt.Host = "example.com";
           // vSmpt.Port = 587;//for local
            vSmpt.Port = 25;//for online
            vSmpt.EnableSsl = false;
            vSmpt.DeliveryMethod = SmtpDeliveryMethod.Network;
            vSmpt.UseDefaultCredentials = false;
            vSmpt.Credentials = smtpUser;
            vSmpt.Send(vMail);
        }
        catch (Exception)
        {
            // ignored
        }
    }