I've made a web page wherein I'm trying to send a mail using SMTP. Initially it wasn't giving me an error where I was the giving the hard code. But when I tried taking the values from the text field (Also I'm using multiview because I have tabs on my page), now the To
field is giving me an error.
I tried to resolve my error from the previously posted queries, but still nothing.
I've tried almost everything but I'm not able to get rid of this error.
Property or indexer 'System.Net.Mail.MailMessage.To' cannot be assigned to -- it is read only.
My frontend code is:
<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>
My backend Code is:
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();
}
}
MailMessage.To
is a read-only property. It returns a list of MailAdresses you added to the message.
To add a MailAddress, you should use:
mm.To.Add(new MailAddress(txtTo.Text, txtReceiver.Text));
尝试这个:
mm.To.Add("testemail@domain.com");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.