当有人在我的网站上注册时,他/她还需要填写他/她的电子邮件地址。
单击“注册”按钮后,系统需要向文本框中写入的电子邮件地址发送一封自动电子邮件。
谁能帮我解决这个问题?
提前致谢!
要在 asp.net 中发送电子邮件,您需要查看 System.Net.Mail。从asp.net内部发送邮件有两个步骤
1) 电子邮件帐户设置 - 这可以在您的 web.config 文件中全局设置
<system.net>
<mailSettings>
<smtp from="test@foo.com">
<network host="host" port="25" userName="username" password="password" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
2)设置您的消息 - 在您的注册页面代码后面
MailMessage message = new MailMessage();
message.From = new MailAddress("sender@sender.com");
message.To.Add(new MailAddress("email@email.com"));
message.Subject = "subject";
message.Body = "content";
SmtpClient client = new SmtpClient();
client.Send(message);
http://weblogs.asp.net/scottgu/archive/2005/12/10/432854.aspx
以下是关于如何从 .NET 代码发送电子邮件的简单说明:http ://weblogs.asp.net/scottgu/archive/2005/12/10/432854.aspx
您要查找的主要关键字是System.Net.Mail
.
以下脚本是一个很好的起点。
using (System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage()) {
System.Text.StringBuilder body = new System.Text.StringBuilder("Your message");
System.Net.Mail.SmtpClient() smtp = new System.Net.Mail.SmtpClient();
mail.To.Add("yourUser@email.com");
mail.From = new System.Net.Mail.MailAddress("yourFrom@email.com");
mail.Subject = "a Subject";
mail.Body = body.ToString();
try {
smtp.Send(mail);
} catch (Exception ex) {
// handle your exception here..
}
}
这假设您的配置中有一些关于 SmtpClient 的详细信息。像这样:http: //msdn.microsoft.com/en-us/library/ms164240.aspx
如果没有,那么您可以在SmtpClient()
构造函数中向客户端提供详细信息。更多信息在这里:http: //msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx
这是一个简单的函数来实现。如果不是匿名的,您必须获取网络凭据和关于您的 SMTP 中继服务器的 SMTP 信息。否则它应该是直截了当的。
using System.Net.Mail; //goes on top
//goes in your class
public void sendEmail(string emailMessage,
string emailSubject,
string emailAddress, string from,
string fromAddress, string emailCC,
string emailBCC)
{
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress(fromAddress, from);
msg.To.Add(emailAddress);
if (emailCC != null && emailCC.ToString().Length > 1)
msg.CC.Add(emailCC);
if (emailBCC != null && emailBCC.ToString().Length > 1)
msg.Bcc.Add(emailBCC);
msg.Priority = MailPriority.High;
msg.Subject = emailSubject;
msg.Body = emailMessage;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Host = info.SMTPServer;
client.Port = 25;
client.EnableSsl = true;
// client.UseDefaultCredentials = some System.Net.NetworkCredential var;
client.Credentials = info.networkCredentials;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
// Use SendAsync to send the message asynchronously
client.Send(msg);
}
catch
{
//handle exception
}
}
使用与实施:
this.sendEmail("test message", "your subject", "to@to.com","from person", "from@from.com","cc@cc.com","bcc@bcc.com");