在C#Windows窗体中向邮件添加多个附件 [英] Adding multiple attachments to a mail, in c# windows forms

查看:84
本文介绍了在C#Windows窗体中向邮件添加多个附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个c#和Windows Forms(经典Windows应用程序,例如记事本,绘画等)应用程序,该应用程序具有获取屏幕截图并通过邮件发送的功能。
但是,它现在只能拍摄6张图片(我可以添加更多,但我不想添加更多代码,我想通过编程方式使其生成),如何使其按设置发送更多或更少的图片用户在应用程序外部?

I'm making an c# and Windows Forms (Classic Windows app like notepad, paint etc.) app that has a feature to get screenshot and send it via mail. However, It can only take 6 pictures now (I can add more, but I don't want to add more code, I want to make it programmatically), how can I make it send more or less, as set by user, outside of app?

Timer1发送邮件。
Timer2需要截图。
resimoran是一个整数,它是调整大小后的图像比例,默认情况下为1。
计数器是一个整数,
现在正在工作...

Timer1 sends mail. Timer2 takes screenshot. resimoran is an int which is image ratio of resizing, it's 1 by default. counter is an int, It's working right now...

这是我的代码:

    private Bitmap Screenshot()
    {
        Bitmap Screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        Graphics GFX = Graphics.FromImage(Screenshot);
        GFX.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size);
        return Screenshot;
    }

    void SendReport()
    {
        MailMessage mail;
        var fromAddress = new MailAddress(frommail, fromname);
        var toAddress = new MailAddress(alici, aliciname);
        string fromPassword = mailpass;

        var smtp = new SmtpClient
        {
            Host = mailhostaddress,
            Port = mailport,
            EnableSsl = sslenabled,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = usedefaultcre,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
        };
        using (mail = new MailMessage(fromAddress, toAddress)
        {
            Subject = konu + DateTime.Now,
            Body = "None of your businness!"
        })
        {
            mail.Attachments.Add(attach1);
            mail.Attachments.Add(attach2);
            mail.Attachments.Add(attach3);
            mail.Attachments.Add(attach4);
            mail.Attachments.Add(attach5);
            mail.Attachments.Add(attach6);

                    smtp.Send(mail);
        }
    }

private void timer1_Tick(object sender, EventArgs e)
    {
        SendReport();
    }

    private void timer2_Tick(object sender, EventArgs e)
    {
        counter++;
        if (counter == 1)
        {
            Bitmap ekrangor = Screenshot();
            Bitmap imagee = resizeImage(ekrangor, new Size(ekrangor.Width / resimoran, ekrangor.Height / resimoran));
            imagee.Save(@"screen1.jpg");
            System.IO.Stream streamer = new System.IO.MemoryStream();
            imagee.Save(streamer, System.Drawing.Imaging.ImageFormat.Jpeg);
            streamer.Position = 0;
            attach1 = new Attachment(streamer, "screen1.jpg");
        }
        else if (counter == 2)
        {
            Bitmap ekrangor = Screenshot();
            Bitmap imagee = resizeImage(ekrangor, new Size(ekrangor.Width / resimoran, ekrangor.Height / resimoran));
            imagee.Save(@"screen2.jpg");
            System.IO.Stream streamer = new System.IO.MemoryStream();
            imagee.Save(streamer, System.Drawing.Imaging.ImageFormat.Jpeg);
            streamer.Position = 0;
            attach2 = new Attachment(streamer, "screen2.jpg");
        }
        else if (counter == 3)
        {
            Bitmap ekrangor = Screenshot();
            Bitmap imagee = resizeImage(ekrangor, new Size(ekrangor.Width / resimoran, ekrangor.Height / resimoran));
            imagee.Save(@"screen3.jpg");
            System.IO.Stream streamer = new System.IO.MemoryStream();
            imagee.Save(streamer, System.Drawing.Imaging.ImageFormat.Jpeg);
            streamer.Position = 0;
            attach3 = new Attachment(streamer, "screen3.jpg");
        }
        else if (counter == 4)
        {
            Bitmap ekrangor = Screenshot();
            Bitmap imagee = resizeImage(ekrangor, new Size(ekrangor.Width / resimoran, ekrangor.Height / resimoran));
            imagee.Save(@"screen4.jpg");
            System.IO.Stream streamer = new System.IO.MemoryStream();
            imagee.Save(streamer, System.Drawing.Imaging.ImageFormat.Jpeg);
            streamer.Position = 0;
            attach4 = new Attachment(streamer, "screen4.jpg");
        }
        else if (counter == 5)
        {
            Bitmap ekrangor = Screenshot();
            Bitmap imagee = resizeImage(ekrangor, new Size(ekrangor.Width / resimoran, ekrangor.Height / resimoran));
            imagee.Save(@"screen5.jpg");
            System.IO.Stream streamer = new System.IO.MemoryStream();
            imagee.Save(streamer, System.Drawing.Imaging.ImageFormat.Jpeg);
            streamer.Position = 0;
            attach5 = new Attachment(streamer, "screen5.jpg");
        }
        else if (counter == 6)
        {
            Bitmap ekrangor = Screenshot();
            Bitmap imagee = resizeImage(ekrangor, new Size(ekrangor.Width / resimoran, ekrangor.Height / resimoran));
            imagee.Save(@"screen6.jpg");
            System.IO.Stream streamer = new System.IO.MemoryStream();
            imagee.Save(streamer, System.Drawing.Imaging.ImageFormat.Jpeg);
            streamer.Position = 0;
            attach6 = new Attachment(streamer, "screen6.jpg");
            counter = 0;
        }
    }

public static Bitmap resizeImage(Bitmap imgToResize, Size size)
        {
            return (new Bitmap(imgToResize, size));
        }

而且,请用C#而不是英语给我答案! (不是这样做:MSDN bla bla,而是尝试使用此无效的lolnocodezhere(){}))

And, please, give me answers in C#, not in English! (not "do this: MSDN bla bla", but "try this void lolnocodezhere() {}")

推荐答案

List< T> 是您的朋友。

您在适当的位置将其声明为

you declare it at a proper place as

List<Attachment> attachments = new List<Attachment>();

然后,将6个块替换为执行

Then you replace your 6 blocks with a single one where you do a

attachments.Add(new Attachment(streamer, "screen.jpg");)

,然后在适当的时候来做

and when the right time has come you do a

foreach(Attachment a in attachments ) mail.Attachments.Add(a);

成功发送邮件后,您将像这样删除集合:

After successfully sending the mail you delete the collection like this:

attachments.Clear();

由您来控制计数器,屏幕图像等内容。

It is up to you to control things like counters, the screen images etc.

顺便说一句: mail.Attachments 就是这样一个集合,也许您想直接使用它。

Btw: mail.Attachmentsis just such a collection and maybe you want to use it directly..?

这篇关于在C#Windows窗体中向邮件添加多个附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆