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.
我已经制作了一个网页,其中我正在尝试使用SMTP发送邮件。最初它没有给我一个错误,我在给硬编码。但是当我尝试从文本字段中取值时(我也使用多视图,因为我的页面上有标签),现在To字段给了我一个错误。
I tried to resolve my error from the previously posted queries, but still nothing.
我试图从以前发布的查询中解决我的错误,但仍然没有。
- c# asp .net Convert to MailAddress
- c#asp .net转换为MailAddress
- http://forums.asp.net/t/1258190.aspx/1
- http://forums.asp.net/t/1258190.aspx/1
- http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx
- http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx
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.
无法将属性或索引器“System.Net.Mail.MailMessage.To”分配给 - 它是只读的。
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();
}
}
2 个解决方案
#1
1
MailMessage.To
is a read-only property. It returns a list of MailAdresses you added to the message.
MailMessage.To是一个只读属性。它返回您添加到邮件中的MailAdresses列表。
To add a MailAddress, you should use:
要添加MailAddress,您应该使用:
mm.To.Add(new MailAddress(txtTo.Text, txtReceiver.Text));
#2
1
Try this:
尝试这个:
mm.To.Add("testemail@domain.com");
#1
1
MailMessage.To
is a read-only property. It returns a list of MailAdresses you added to the message.
MailMessage.To是一个只读属性。它返回您添加到邮件中的MailAdresses列表。
To add a MailAddress, you should use:
要添加MailAddress,您应该使用:
mm.To.Add(new MailAddress(txtTo.Text, txtReceiver.Text));
#2
1
Try this:
尝试这个:
mm.To.Add("testemail@domain.com");