使用不同的附件发送多封电子邮件C#

时间:2015-08-18 23:16:33

标签: c# email

开始之前的快速说明,我知道如何发送附件的电子邮件,但在这种情况下,它只是无法正常工作。这个代码假设这样做会发送每封20张图像的电子邮件。因此,如果有一个包含62张图像的文件夹,它将总共发送4封电子邮件,最后一封包含2张图像。我发现它适合于.send位并崩溃。凭证也是正确的,虽然我删除了这篇文章。

Decimal EmailCount = Math.Ceiling(Convert.ToDecimal(Directory.GetFiles(@"C:\Users\William\Pictures\Documents\Apple iPhone\").Length) / 20);
        Int32 SentEmails = 0;
        Int32 AttachmentNumber = 1;
        Int32 NewEmail = 1;
        System.Net.Mail.Attachment attachment;

        do
        {
            MailMessage MyMailMessage = new MailMessage();
            MyMailMessage.Attachments.Clear();

            MyMailMessage.From = new MailAddress("");
            MyMailMessage.To.Add("");
            MyMailMessage.Subject = ("");

            do
            {
                attachment = new System.Net.Mail.Attachment(@"C:\Users\William\Pictures\Documents\Apple iPhone\" + AttachmentNumber + ".jpg");
                MyMailMessage.Attachments.Add(attachment);
                AttachmentNumber += 1;
                NewEmail += 1;

            } while (NewEmail <= 20);

            SmtpClient SMTPServer = new SmtpClient("smtp.gmail.com");
            SMTPServer.Port = 587;
            SMTPServer.Credentials = new System.Net.NetworkCredential("", "");
            SMTPServer.EnableSsl = true;
            SMTPServer.Send(MyMailMessage);

            SentEmails += 1;
            NewEmail = 1;

        } while (SentEmails <= EmailCount);

0 个答案:

没有答案