使用{System.Net.Mail}发送邮件

时间:2016-04-07 17:26:45

标签: c# .net

        SmtpClient client = new SmtpClient();
        client.Port = 587;
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true;
        client.Timeout = 10000;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential("*********@gmail.com", "***********");

        MailMessage mm = new MailMessage("*********@gmail.com", "******@gmail.com", "delivery.", "tttt");
        mm.BodyEncoding = UTF8Encoding.UTF8;
        mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

            Attachment fMailAttachment;
            fMailAttachment = new Attachment(FilePath);
            mm.Attachments.Add(fMailAttachment);

            client.Send(mm);
            mm.Attachments.Dispose();

我正在尝试使用此代码发送电子邮件。它工作得很好,但只有当我登录我的邮件并让它打开时,当我关闭邮件时它不会发送任何邮件。

该代码有什么问题?

1 个答案:

答案 0 :(得分:0)

我编辑后效果很好         client.Timeout = 10000; 至         client.Timeout = 100000;