Asp.net 如何从.aspx页面发送电子邮件?
我在我的.aspx页面上使用了文本框和提交按钮,我想在单击按钮时通过电子邮件发送所有这些文本框的数据,因此请告诉我解决方案…您可以使用该类。解决此问题的方法只有代码;您依赖SMTP服务器来发送邮件。最佳方案:您已经在服务器上设置了一个默认端口。在这种情况下,您所需要的就是:Asp.net 如何从.aspx页面发送电子邮件?,asp.net,Asp.net,我在我的.aspx页面上使用了文本框和提交按钮,我想在单击按钮时通过电子邮件发送所有这些文本框的数据,因此请告诉我解决方案…您可以使用该类。解决此问题的方法只有代码;您依赖SMTP服务器来发送邮件。最佳方案:您已经在服务器上设置了一个默认端口。在这种情况下,您所需要的就是: SmtpClient client = new SmtpClient("localhost"); client.Send(new MailMessage("me@myserver.com", "someoneelse@foo
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
在按钮上单击事件调用此函数
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;
}
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
//创建邮件消息
MailMessage mail=新的MailMessage();
//设置地址
mail.From=新邮件地址(“info@infoA2Z.com");
mail.To.Add(“Support@infoA2Z.com");
//设置内容
mail.Subject=“这是一封电子邮件”;
mail.Body=“这是电子邮件的正文内容。”;
//发送消息
SmtpClient smtp=新SmtpClient();
smtp.发送(邮件);
}
<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>