我有一个自定义类:
using System;
using System.Configuration;
using System.Net;
using System.Net.Configuration;
using System.Net.Mail;
namespace MyNameSpace
{
internal static class SMTPMailer
{
public static void SendMail(string to, string subject, string body)
{
Configuration oConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var mailSettings = oConfig.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
if (mailSettings != null)
{
int port = mailSettings.Smtp.Network.Port;
string from = mailSettings.Smtp.From;
string host = mailSettings.Smtp.Network.Host;
string pwd = mailSettings.Smtp.Network.Password;
string uid = mailSettings.Smtp.Network.UserName;
var message = new MailMessage
{
From = new MailAddress(@from)
};
message.To.Add(new MailAddress(to));
message.CC.Add(new MailAddress(from));
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
var client = new SmtpClient
{
Host = host,
Port = port,
Credentials = new NetworkCredential(uid, pwd),
EnableSsl = true
};
try
{
client.Send(message);
}
catch (Exception ex)
{
}
}
}
}
}
这可以从我的 app.conf 文件中提取。