【发布时间】:2012-06-27 10:34:01
【问题描述】:
我在我的 .aspx 页面上使用了文本框和提交按钮,我想通过电子邮件发送按钮单击时所有这些文本框的数据,所以请告诉我解决方案...
【问题讨论】:
标签: asp.net
我在我的 .aspx 页面上使用了文本框和提交按钮,我想通过电子邮件发送按钮单击时所有这些文本框的数据,所以请告诉我解决方案...
【问题讨论】:
标签: asp.net
在按钮点击事件上调用该函数
public bool SendOnlyToEmail(string sToMailAddr, string sSubject, string sMessage,
string sFromMailAddr)
{
try
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
if (string.IsNullOrEmpty(sFromMailAddr))
{
// fetching from address from web config key
msg.From = new System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings["MailFrom"]);
}
else
{
msg.From = new System.Net.Mail.MailAddress(sFromMailAddr);
}
foreach (string address in sToMailAddr)
{
if (address.Length > 0)
{
msg.To.Add(address);
}
}
msg.Subject = sSubject;
msg.Body = sMessage;
msg.IsBodyHtml = true;
//fetching smtp address from web config key
System.Net.Mail.SmtpClient objSMTPClient = new System.Net.Mail.SmtpClient(System.Configuration.ConfigurationManager.AppSettings["MailServer"]);
//SmtpMail.SmtpServer = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["MailServer"]);
if (sToMailAddr.Length > 0)
{
objSMTPClient.Send(msg);
return true;
}
else
{
return false;
}
}
catch (Exception objException)
{
ErrorLog.InsertException(objException);
return false;
}
}
【讨论】:
您可以使用SmtpClient 类。
【讨论】:
没有解决此问题的纯代码方法;您依赖 SMTP 服务器来发送您的邮件。最佳情况:您已经在服务器上设置了一个默认端口。在这种情况下,您只需要这样:
SmtpClient client = new SmtpClient("localhost");
client.Send(new MailMessage("me@myserver.com", "someoneelse@foo.com"));
如果做不到这一点,您可以考虑设置一个免费的 SMTP 帐户,或者(如果您打算发送批量电子邮件,绝对有必要),在 Amazon SES 等电子邮件服务提供商处获得一个帐户。
【讨论】:
您可以使用以下代码发送电子邮件:
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("senderEmail");
message.From = fromAddress;
message.Subject = "your subject";
message.Body = txtBox.Text;//Here put the textbox text
message.To.Add("to");
smtpClient.Send(message);//returns the boolean value ie. success:true
【讨论】:
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("info@infoA2Z.com");
mail.To.Add("Support@infoA2Z.com");
//set the content
mail.Subject = "This is an email";
mail.Body = "this is the body content of the email.";
//send the message
SmtpClient smtp = new SmtpClient();
smtp.Send(mail);
}
</script>
【讨论】: