您可以在与 Web 服务器相同的服务器上安装/启用 SMTP 服务器。提供一个页面,用户可以在其中输入“发件人”和“收件人”地址。从您的服务器发送电子邮件,但“欺骗”“发件人”电子邮件 ID。这样用户就不必输入他们的电子邮件服务器详细信息。
public void SendEmailAsync(string id, string to, string from, string subject, string message, SendCompletedEventHandler callback)
{
// Command line argument must the the SMTP host.
var client = new SmtpClient(ConfigurationManager.AppSettings["SmtpServer"]);
if (bool.Parse(ConfigurationManager.AppSettings["PickupDirectoryFromIis"]))
{
client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
}
// Specify the message content.
var message = new MailMessage(from, to)
{
Subject = subject,
Body = message,
BodyEncoding = Encoding.UTF8,
SubjectEncoding = Encoding.UTF8
};
client.SendCompleted += callback;
try
{
client.SendAsync(message, id);
}
catch (SmtpException e)
{
this.EventLog.WriteEntry(e.ToString(), EventLogEntryType.Error);
}
}