[英]cannot send email through button asp.net
我打算發送帶有圖像的電子郵件,該圖像附加到收件人。 但是,當我單擊按鈕時,什么也沒有發生。 錯誤或成功注釋不可見。 有人可以幫我嗎? 謝謝。 這是我的代碼: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>
和后面的代碼:
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);
}
您的主要div是否包裹在表格中?
<form runat="server" id="frm1">
....
</form>
您似乎在所有內容中都缺少<form runat="server">... </form>
。
嘗試在表單周圍添加該標簽。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.