我做了一個網頁,其中我試圖發送郵件使用SMTP。最初,在給出硬編碼的時候,它並沒有給我一個錯誤。但是,當我嘗試從文本字段中獲取值(同時我使用多視圖,因爲我的頁面上有選項卡),現在To
字段給我一個錯誤。發送電子郵件的SMTP錯誤
我試圖解決我以前發佈的查詢錯誤,但仍然沒有。
- c# asp .net Convert to MailAddress
- http://forums.asp.net/t/1258190.aspx/1
- http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx
我已經試過幾乎所有的東西,但我無法擺脫這種錯誤的。
Property or indexer 'System.Net.Mail.MailMessage.To' cannot be assigned to -- it is read only.
我的前端代碼:
<tr>
<td class="style15"> RECEIVER </td>
<td> <asp:TextBox ID="txtReceiver" runat="server" CssClass="Textbox1" Width="414px"></asp:TextBox>
<asp:LinkButton ID="lbEdit5" runat="server" OnClick="lbEdit5_Click"> Edit </asp:LinkButton>
</td>
</tr>
<tr>
<td class="style15">
TO MAIL
</td>
<td>
<asp:TextBox ID="txtTo" runat="server" CssClass="Textbox1" Width="414px"></asp:TextBox>
<asp:LinkButton ID="lbEdit6" runat="server" OnClick="lbEdit6_Click"> Edit
</asp:LinkButton>
<asp:RegularExpressionValidator ID="regexTo" runat="server"
ControlToValidate="txtTo" Display="Dynamic" ErrorMessage="Enter an E-Mail Address"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"> </asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="reqMailTo" runat="server"
ControlToValidate="txtTo" ErrorMessage="Enter a Mailing Address"></asp:RequiredFieldValidator>
</td>
</tr>
我的後端代碼是:
protected void btnSMail_Click(object sender, EventArgs e)
{
string smtpadd = txtSMTP.Text;
try
{
if (smtpadd != "" && smtpadd != null)
{
MailMessage mm = new MailMessage();
SmtpClient sc = new SmtpClient(txtSMTP.Text);
if (!fupAttach.HasFile)
{
FileStream fs = new FileStream("D:\\DEV\\New.xml", FileMode.Open, FileAccess.Read);
Attachment attch = new Attachment(fs, "License Generation in XML", MediaTypeNames.Application.Octet);
mm.Attachments.Add(attch);
}
//else
//{
// FileStream fd = new FileStream();
//}
mm.From = new MailAddress(txtMailAdd.Text, txtFrom.Text);
mm.Subject = txtSub.Text;
mm.To = new MailAddress(txtTo.Text,txtReceiver.Text);
//mm.To= new MailAddress(txtTo.Text);
mm.Body = txtBody.Text;
lblMailFail.Text = "Mail Successfully Sent";
}
else
{
lblMailFail.Text = "Enter an SMTP IP";
}
}
catch (Exception blah)
{
lblMailFail.Text = blah.ToString();
}
}
重複的[asp.net郵件添加的ReplyTo](http://stackoverflow.com/questions/3667006/asp-net-mail-add-回覆)。您提供的所有三個鏈接都會給出答案,您收到的錯誤也是如此。你正在分配'mm.To =新的MailAddress(...)',這是不允許的;你必須做'mm.To.Add(新的MailAddress(...))'。嘗試了 – CodeCaster
。不工作。 :/ – Esha
「不工作」不是錯誤。請顯示確切的例外情況及其發生的路線。 – CodeCaster