C#调用邮箱smtp
static string accountName = System.Configuration.ConfigurationManager.AppSettings['mailAccountName'];
static string password = System.Configuration.ConfigurationManager.AppSettings['mailPassword'];
static string smtpServer = System.Configuration.ConfigurationManager.AppSettings['mailSmtpServer'];
static int smtpPort = int.Parse(System.Configuration.ConfigurationManager.AppSettings['mailSmtpPort']);
static string displayName= System.Configuration.ConfigurationManager.AppSettings['mailDisplayName'];
static string userState= System.Configuration.ConfigurationManager.AppSettings['mailUserState'];
static public void SendMail(string sendTo, string subject, string body)
{
try
{
// Command line argument must the the SMTP host.
SmtpClient client = new SmtpClient(smtpServer, smtpPort);
client.UseDefaultCredentials = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential(accountName, password);
// Specify the e-mail sender.
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress(accountName,
displayName,
System.Text.Encoding.UTF8);
// Set destinations for the e-mail message.
MailAddress to = new MailAddress(sendTo);
// Specify the message content.
MailMessage message = new MailMessage(from, to);
message.Body = body;
// Include some non-ASCII characters in body and subject.
string someArrows = '(请勿回复这封邮件)';
message.Body += Environment.NewLine + someArrows;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = subject;
message.SubjectEncoding = System.Text.Encoding.UTF8;
client.Send(message);
message.Dispose();
client.Dispose();
}
catch { }
}
赞0
踩0