Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 如何使用C代码发送邮件通知_C# 4.0 - Fatal编程技术网

C# 4.0 如何使用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

我已经使用下面的代码使用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添加