2012-11-22 224 views
0

我做了一个网页,其中我试图发送邮件使用SMTP。最初,在给出硬编码的时候,它并没有给我一个错误。但是,当我尝试从文本字段中获取值(同时我使用多视图,因为我的页面上有选项卡),现在To字段给我一个错误。发送电子邮件的SMTP错误

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

  1. c# asp .net Convert to 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(); 
    } 
} 
+0

重复的[asp.net邮件添加的ReplyTo](http://stackoverflow.com/questions/3667006/asp-net-mail-add-回复)。您提供的所有三个链接都会给出答案,您收到的错误也是如此。你正在分配'mm.To =新的MailAddress(...)',这是不允许的;你必须做'mm.To.Add(新的MailAddress(...))'。尝试了 – CodeCaster

+0

。不工作。 :/ – Esha

+0

“不工作”不是错误。请显示确切的例外情况及其发生的路线。 – CodeCaster

回答

1

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

要添加一个MailAddress,将您应该使用:

mm.To.Add(new MailAddress(txtTo.Text, txtReceiver.Text)); 
1

试试这个:

mm.To.Add("[email protected]"); 
+0

我将添加在文本框中输入的值。 – Esha

+0

它的工作与否? –