我已经制作了一个网页,其中我正在尝试使用SMTP发送邮件。最初它没有给我一个错误,我在给硬编码。但是当我尝试从文本字段中获取值时(我也在使用multiview,因为我的页面上有标签),现在To
字段给了我一个错误。
我尝试从先前发布的查询中解决我的错误,但仍然没有。
我几乎尝试过所有事情,但我无法摆脱这个错误。
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();
}
}
答案 0 :(得分:2)
MailMessage.To
是只读属性。它返回您添加到邮件中的MailAdresses列表。
要添加MailAddress,您应该使用:
mm.To.Add(new MailAddress(txtTo.Text, txtReceiver.Text));
答案 1 :(得分:1)
试试这个:
mm.To.Add("testemail@domain.com");