我如何发送电子邮件评论? [英] How can i send a comment in email?
问题描述
我有一些文本框,如姓名,电子邮件地址和电话号码。和我的网页上发表评论。
我要的值发送给我的电子邮件地址。
我应该怎么办呢??
我做的:
保护无效btnSubmit_Click(对象发件人,EventArgs的发送)
{
SmtpClient客户端=新SmtpClient();
消息MAILMESSAGE新= MAILMESSAGE(); 尝试
{
MailAddress FROMADDRESS =新的MailAddress(txtEmail.Text,txtName.Text); SmtpClient.Host =localhost的;
SmtpClient.Port = 25; message.From = FROMADDRESS;
message.To.Add(xyz@gmail.com);
message.Subject =反馈;
message.IsBodyHtml = FALSE;
message.Body = txtComment.Text;
SmtpClient.Send(消息); 的Response.Write(邮件发送成功);
}
赶上(异常前)
{
的Response.Write(发送电子邮件失败。+ ex.Message);
}
}
和我收到以下错误:
的对象引用是必需的非静态字段,方法或属性System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMessage)'
SmtpClient.Host =localhost的;
SmtpClient.Port = 25;
~~~~~~~~~~~~~~~~~~~~
SmtpClient.Send(消息);
这些行正试图使用类 SmtpClient
的成员。然而,由于这些成员不定义为静态
,你需要参考您的实例的类,你叫<$ C $的C>客户端。
尝试
client.Host =localhost的;
client.Port = 25;
~~~~~~~~~~~~~~~~~~~~
client.Send(消息);
此外,有这篇文章读对类和实例成员之间的差异。
最后,SmtpClient实现IDisposable,我会改变你的code把它包起来了的使用块,因为这将确保你用它完成后,你是正确清理您的SMTP会话。
使用(SmtpClient客户端=新SmtpClient())
{
//你的code
}
I have some text boxes like name,email address,phone no. and comment on my page.
I have to send the values to my email address..
How should I do this?? I am doing:
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("xyz@gmail.com");
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);
}
}
and I am getting the following error:
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);
These lines are attempting to use members of the class SmtpClient
. However, as these members are not defined as static
, you need to refer to your instance of that class, which you have called client
.
Try
client.Host = "localhost";
client.Port = 25;
~~~~~~~~~~~~~~~~~~~~
client.Send(message);
Also, have a read of this article on the differences between class and instance members.
Finally, as SmtpClient implements IDisposable, I would change your code to wrap it in a using block, as this will ensure you are correctly cleaning up your SMTP session after you are finished with it.
using (SmtpClient client = new SmtpClient())
{
// YOUR CODE
}
这篇关于我如何发送电子邮件评论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!