0

我制作了一个网页,在其中尝试使用 SMTP 发送邮件。最初,在我给出硬代码的地方并没有给我一个错误。但是当我尝试从文本字段中获取值时(我也在使用多视图,因为我的页面上有标签),现在该To字段给了我一个错误。

我试图从以前发布的查询中解决我的错误,但仍然没有。

  1. 时间:2019-05-10 标签:c#asp.net转换为MailAddress
  2. http://forums.asp.net/t/1258190.aspx/1
  3. 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();
    }
}
4

2 回答 2

2

MailMessage.To是一个只读属性。它返回您添加到邮件中的 MailAdresses 列表。

要添加 MailAddress,您应该使用:

mm.To.Add(new MailAddress(txtTo.Text, txtReceiver.Text));
于 2012-11-22T19:14:35.700 回答
1

试试这个:

mm.To.Add("testemail@domain.com");
于 2012-11-22T09:52:54.240 回答