我想写一个算法来发送电子邮件。
该算法需要用 C#.Net 编写。
任何人请给我有关此算法或与此算法相关的任何链接的建议。
一个简单的解决方案是使用SmtpClient
类。
void SendEmail(string fromAddress, string toLine, string subject, string messageBody)
{
const string host = "smtp.server.com";
const int port = 1234;
const string userName = "(user)";
const string password = "password";
using (var smtpClient = new SmtpClient(host, port))
{
smtpClient.Credentials = new NetworkCredential(userName, password);
var mailMessage = new MailMessage(fromAddress, toLine, subject, messageBody);
smtpClient.Send(mailMessage);
}
}
private void SendEmailToAdmin(string message)
{
SmtpSection smtpSection = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
string host = smtpSection.Network.Host;
if (string.IsNullOrEmpty(host))
{
host = "127.0.0.1";
}
using (SmtpClient smtpClient = new SmtpClient(host, smtpSection.Network.Port))
{
MailMessage mail = new MailMessage(smtpSection.From, Properties.Settings.Default.SupportEmailAddress);
mail.Subject = Environment.MachineName + ": Error";
mail.IsBodyHtml = false;
mail.Body = message;
smtpClient.Send(mail);
}
}