C# 4.0 如何使用C代码发送邮件通知
我已经使用下面的代码使用C代码发送邮件通知C# 4.0 如何使用C代码发送邮件通知,c#-4.0,C# 4.0,我已经使用下面的代码使用C代码发送邮件通知 public static void SendNotification(string filepath) { try { SmtpClient mailServer = new SmtpClient(ConfigurationManager.AppSettings["host"], int.Parse(ConfigurationManager.AppSett
public static void SendNotification(string filepath)
{
try
{
SmtpClient mailServer = new SmtpClient(ConfigurationManager.AppSettings["host"], int.Parse(ConfigurationManager.AppSettings["portnumber"]));
mailServer.EnableSsl = true;
mailServer.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["sender_username"], ConfigurationManager.AppSettings["sender_password"]);
string from = ConfigurationManager.AppSettings["sender"];
string to = ConfigurationManager.AppSettings["receipients"];
string cc = ConfigurationManager.AppSettings["receipientCC"];
MailMessage msg = new MailMessage(from, to);
msg.Subject = "Branch API Export Results";
msg.Body = "Test Mail. Please Find Attached for the Results from Branch API Export";
msg.CC.Add(cc);
msg.Attachments.Add(new Attachment(filepath));
mailServer.Send(msg);
}
catch (Exception ex)
{
//Log
}
}
在App.Config中包含配置值。除此之外还有什么更好的办法。这里是
private static void SendMail(string subject, string content)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("YOURMAİL");
mail.To.Add("MAİLTO");
mail.Subject = subject;
mail.Body = content;
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("YOURMAİL", "YOURMAİLPASSWORD");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
catch (Exception ex)
{
}
}
这是发送邮件的最简单方式。不要忘记使用System.Net.Mail添加