如何使用C#代码发送邮件通知
How to send a mail notification using C# code
我使用以下代码通过 C# 代码发送邮件通知
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;
添加
我使用以下代码通过 C# 代码发送邮件通知
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;
添加