Gur*_*Rao 0 asp.net smtp c#-4.0
我想在用户点击发送按钮时向用户的帐户发送电子邮件.但我得到了上述错误.下面是我的sendClick代码.
protected void btnsendCode_Click(object sender, EventArgs e)
{
try
{
if (txtemail.Text != "")
{
Random rd = new Random();
veri = rd.Next(1000, 10000);
MailMessage mm = new MailMessage();
mm.To.Add(new MailAddress(txtemail.Text.ToString()));
mm.From = new MailAddress("xxx@yyy.in", "Verification Mail");
mm.Body = "Your Verification Code is - " + veri.ToString();
mm.IsBodyHtml = true;
mm.Subject = "Verification mail";
SmtpClient smcl = new SmtpClient();
smcl.Host = "smtp.gmail.com";
smcl.Port = 587;
smcl.Credentials = new NetworkCredential("xxx@yyy.in", "xxx");
//smcl.EnableSsl = true;
smcl.Send(mm);
Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Verification Code sent to your Email ID! Please Check your Email!!');", true);
txtverify.Enabled = true;
btnsendCode.Text = "Send Code Again";
lblmsg.Visible = false;
}
else
{
lblmsg.Visible = true;
lblmsg.Text = "Please enter Email ID!!";
lblmsg.ForeColor = System.Drawing.Color.Yellow;
lblmsg.BorderColor = System.Drawing.Color.Red;
lblmsg.BorderStyle = BorderStyle.Ridge;
lblmsg.BorderWidth = new Unit("2");
lblmsg.Focus();
}
}
catch (WebException we)
{
lblmsg.Visible = true;
lblmsg.Text = we.Message.ToString();
lblmsg.ForeColor = System.Drawing.Color.Yellow;
lblmsg.BorderColor = System.Drawing.Color.Red;
lblmsg.BorderStyle = BorderStyle.Ridge;
lblmsg.BorderWidth = new Unit("2");
}
}
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪
[FormatException:指定的字符串不是电子邮件地址所需的格式.]
System.Net.Mail.MailAddressParser.ReadCfwsAndThrowIfIncomplete(String data,Int32 index)+1475945
System.Net.Mail.MailAddressParser.ParseDomain(String data ,Int32&index)+135 System.Net.Mail.MailAddressParser.ParseAddress(String data,Boolean expectMultipleAddresses,Int32&index)+99
System.Net.Mail.MailAddressParser.ParseAddress(String data)+23
System.Net.Mail.MailAddress. .ctor(String address,String displayName,Encoding displayNameEncoding)+220
System.Net.Mail.MailMessage..ctor()+130
events.btnsendCode_Click(Object sender,EventArgs e)在d:\ inetpub\vhosts\marpallichande.in\httpdocs\Test\events.aspx.cs:101
System.Web.UI.WebControls.Button.OnClick(EventArgs e)+9552874
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)+103
System.Web.UI .WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)+10
System.Web.UI.Page.RaisePostBac kEvent(IPostBackEventHandler sourceControl,String eventArgument)+13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)+35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+1724
哪个部分我犯了错误,需要纠正它?
rec*_*ace 10
它失败的原因是使用没有邮件设置的空构造函数来匹配:
MailMessage mm = new MailMessage();
Run Code Online (Sandbox Code Playgroud)
空构造函数依赖于:
<system.net>
<mailSettings>
<smtp from="report@company.com" />
</mailSettings>
</system.net>
Run Code Online (Sandbox Code Playgroud)
在您的应用程序或web.config文件中.因此要么使用期望from和to的地址的构造函数,要么将该节点添加到app/web.config文件中.
我坚信这是.Net框架中的一个错误,因为我们能够创建新的MailMessage()对象,然后在以后分配,但在.Net 4.0中,在某些条件下,我仍然不完全理解这一点失败.我当然欢迎纠正,但就目前而言,这似乎是一种疏忽.
所以我们的一些客户从未遇到过这个问题,但为了解决这个问题,我们不得不将这个虚拟设置添加到web.config文件中.
| 归档时间: |
|
| 查看次数: |
8625 次 |
| 最近记录: |