简体   繁体   English

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

[英]cannot send email through button asp.net

Im planning to send an email with an image attached to a receiver. 我打算发送带有图像的电子邮件,该图像附加到收件人。 however when i click on the button, nothing happens. 但是,当我单击按钮时,什么也没有发生。 an error or success note is not visible. 错误或成功注释不可见。 can someone help me out on this? 有人可以帮我吗? thank you. 谢谢。 here are my codes: aspx 这是我的代码:aspx

<%@ 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>

and code behind: 和后面的代码:

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);
        }

Is your main div wrapped in a Form? 您的主要div是否包裹在表格中?

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

It looks like you are missing <form runat="server">... </form> around all your content. 您似乎在所有内容中都缺少<form runat="server">... </form>

Try adding that tag around your form. 尝试在表单周围添加该标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM