无法通过按钮asp.net发送电子邮件

本文关键字:net 电子邮件 asp 按钮 | 更新日期: 2023-09-27 18:02:50

我打算发送一封带有图片的电子邮件给收件人。然而,当我点击按钮时,什么也没有发生。错误或成功记录不可见。有人能帮我一下吗?谢谢你!以下是我的代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/Site1.Master" 
AutoEventWireup="true" CodeBehind="SubmitPurchaseOrder.aspx.cs" Inherits="IslandGas.Admin.SubmitPurchaseOrder" %>
<asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="content" runat="server">
<form id="Form1" runat="server">
<div>
<table style=" border:1px solid" align="center">
<tr>
<td colspan="2" align="center">
<b>Purchase Order to Supplier</b>
</td>
</tr>
<tr>
<td>
Gmail Username:
</td>
<td>
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
                                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                                        ErrorMessage="User name is required." Display="Dynamic" ControlToValidate="txtUsername" 
                                        ForeColor="Red"></asp:RequiredFieldValidator>
                                        <asp:RegularExpressionValidator ID="RegularExpressionValidator7" runat="server" 
                                        ErrorMessage="Invalid format." Display="Dynamic" 
                                        ValidationExpression="'w+([-+.']'w+)*@'w+([-.]'w+)*'.'w+([-.]'w+)*" 
                                        ControlToValidate="txtUsername" ForeColor="Red"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
Gmail Password:
</td>
<td>
<asp:TextBox ID="txtpwd" runat="server" TextMode="Password"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                                            ErrorMessage="No special characters." ControlToValidate="txtpwd" ForeColor="Red" 
                                            ValidationExpression="^[a-zA-Z0-9]+$" Display="Dynamic"></asp:RegularExpressionValidator>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                                        ErrorMessage="Password required." Display="Dynamic" ControlToValidate="txtpwd" 
                                        ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" 
                                            ErrorMessage="No special characters." ControlToValidate="txtSubject" ForeColor="Red" 
                                            ValidationExpression="^[a-zA-Z0-9 ]+$" Display="Dynamic"></asp:RegularExpressionValidator>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                                        ErrorMessage="Subject required." Display="Dynamic" ControlToValidate="txtSubject" 
                                        ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
To:
</td>
<td>
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" 
                                        ErrorMessage="Invalid format." Display="Dynamic" 
                                        ValidationExpression="'w+([-+.']'w+)*@'w+([-.]'w+)*'.'w+([-.]'w+)*" 
                                        ControlToValidate="txtTo" ForeColor="Red"></asp:RegularExpressionValidator>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
                                        ErrorMessage="Recipient required." Display="Dynamic" ControlToValidate="txtTo" 
                                        ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Attach a file:
</td>
<td>
<asp:FileUpload ID="fileUpload1" runat="server" />
<asp:RequiredFieldValidator
                ID="RequiredFieldValidator5" runat="server" 
                ErrorMessage="Please Upload Image" ControlToValidate="fileUpload1" 
                ForeColor="Red"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server" 
                    ErrorMessage="Invalid file type" 
                    ControlToValidate="fileUpload1" ForeColor="Red" 
                    ValidationExpression="^.*'.(jpg|JPG|gif|GIF|doc|DOC|pdf|PDF|PNG|png)$"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td valign="top">
Body:
</td>
<td>
<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10" ></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator6" runat="server" 
                                            ErrorMessage="No special characters." ControlToValidate="txtBody" ForeColor="Red" 
                                            ValidationExpression="^[a-zA-Z0-9]+$" Display="Dynamic"></asp:RegularExpressionValidator>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" 
                                        ErrorMessage="Message body required." Display="Dynamic" ControlToValidate="txtBody" 
                                        ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" Text="Send" runat="server" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</form>
</asp:Content>

和后面的代码:

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage Msg = new MailMessage();
            // Sender e-mail address.
            Msg.From = new MailAddress(txtUsername.Text);
            // Recipient e-mail address.
            Msg.To.Add(txtTo.Text);
            Msg.Subject = txtSubject.Text;
            // File Upload path
            String FileName = fileUpload1.PostedFile.FileName;
            string mailbody = txtBody.Text + "<br/><img          src=cid:companylogo>";
            string fileName = Path.GetFileName(FileName);
            Msg.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileName));
            //LinkedResource myimage = new LinkedResource(FileName);
            // Create HTML view
            AlternateView htmlMail = AlternateView.CreateAlternateViewFromString(mailbody, null, "text/html");
            // Set ContentId property. Value of ContentId property must be the same as
            // the src attribute of image tag in email body. 
            //myimage.ContentId = "companylogo";
            // htmlMail.LinkedResources.Add(myimage);
            Msg.AlternateViews.Add(htmlMail);
            // your remote SMTP server IP.
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtpwd.Text);
            smtp.EnableSsl = true;
            smtp.Send(Msg);
            Msg = null;
            //ClientScript.RegisterStartupScript( "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
            Response.Write("<script>alert('Email Sent');</script>");
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Unable To Send Email');</script>");
            Console.WriteLine("{0} Exception caught.", ex);
        }

无法通过按钮asp.net发送电子邮件

主div是否在表单中包装?

<form runat="server" id="frm1">
....
</form>

看起来你的所有内容都缺少<form runat="server">... </form>