我有一些文本框,如姓名、电子邮件地址、电话号码。并在我的页面上发表评论。
我必须将这些值发送到我的电子邮件地址..
我该怎么做?
我在做:
protected void btnSubmit_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);
SmtpClient.Host = "localhost";
SmtpClient.Port = 25;
message.From = fromAddress;
message.To.Add("[email protected] /cdn-cgi/l/email-protection");
message.Subject = "Feedback";
message.IsBodyHtml = false;
message.Body = txtComment.Text;
SmtpClient.Send(message);
Response.Write("Email successfully sent.");
}
catch (Exception ex)
{
Response.Write("Send Email Failed." + ex.Message);
}
}
我收到以下错误:
An object reference is required for the nonstatic field, method, or property 'System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMessage)'
SmtpClient.Host = "localhost";
SmtpClient.Port = 25;
~~~~~~~~~~~~~~~~~~~~
SmtpClient.Send(message);
这些行试图使用类的成员SmtpClient
。然而,由于这些成员没有被定义为static
,您需要参考您的instance你所说的那个类的client
.
Try
client.Host = "localhost";
client.Port = 25;
~~~~~~~~~~~~~~~~~~~~
client.Send(message);
另外,请阅读本文 http://weblogs.asp.net/wallen/archive/2004/11/23/268574.aspx关于类成员和实例成员之间的差异。
最后,由于 SmtpClient 实现了 IDisposable,我将更改您的代码以将其包装在using http://msdn.microsoft.com/en-us/library/yh598w02%28v=vs.80%29.aspx阻止,因为这将确保您在完成 SMTP 会话后正确清理它。
using (SmtpClient client = new SmtpClient())
{
// YOUR CODE
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)