我想编写一个发送电子邮件的算法。
算法需要用C#.Net编写。
任何人请给我关于此算法的建议或与此算法相关的任何链接。
答案 0 :(得分:4)
一个简单的解决方案是使用SmtpClient
class。
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);
}
}
答案 1 :(得分:0)
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);
}
}