如何从 C# 发送带有附件的电子邮件?

发布于 2024-11-14 00:58:23 字数 1574 浏览 3 评论 0原文

我正在尝试编写一个代码,什么可以保存图片框的内容(有效)并通过电子邮件发送(无效)。

您认为可能是什么问题?是否还有其他内容 SmtpClient client = new SmtpClient("smtp.gmail.com"); ?

此外,在上传图像时,程序不应冻结,而是在必要时能够同时上传一些图像。

            System.Drawing.Image img = pictureBox1.Image;
            string name = "" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".jpg";
            img.Save(name, System.Drawing.Imaging.ImageFormat.Jpeg);

            if (chb_notif.Checked == true) ////////////// SEND EMAIL!
            {

                MailMessage message = new MailMessage(
                   "[email protected]",
                   tb_email.Text ,
                   "VIDEO FENCE",
                   "Your perimeter has been breeched! System name: " + Environment.MachineName + "." );

                Attachment data = new Attachment(name);

                ContentDisposition disposition = data.ContentDisposition;
                disposition.CreationDate = System.IO.File.GetCreationTime(name);
                disposition.ModificationDate = System.IO.File.GetLastWriteTime(name);
                disposition.ReadDate = System.IO.File.GetLastAccessTime(name);

                message.Attachments.Add(data);

                //Send the message.
                SmtpClient client = new SmtpClient("smtp.gmail.com");

                client.Credentials = CredentialCache.DefaultNetworkCredentials;

                client.Send(message);
            }

谢谢!

I'm trying to write a code, what would save the content of a picturebox ( works ) and email it ( doesn't work ).

What do you think might be the problem? Should there be anything more to the SmtpClient client = new SmtpClient("smtp.gmail.com"); ?

Also the program shouldn't freeze up while the image gets uploaded, rather then, if necessary, be able to simultaneously upload a few images.

            System.Drawing.Image img = pictureBox1.Image;
            string name = "" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".jpg";
            img.Save(name, System.Drawing.Imaging.ImageFormat.Jpeg);

            if (chb_notif.Checked == true) ////////////// SEND EMAIL!
            {

                MailMessage message = new MailMessage(
                   "[email protected]",
                   tb_email.Text ,
                   "VIDEO FENCE",
                   "Your perimeter has been breeched! System name: " + Environment.MachineName + "." );

                Attachment data = new Attachment(name);

                ContentDisposition disposition = data.ContentDisposition;
                disposition.CreationDate = System.IO.File.GetCreationTime(name);
                disposition.ModificationDate = System.IO.File.GetLastWriteTime(name);
                disposition.ReadDate = System.IO.File.GetLastAccessTime(name);

                message.Attachments.Add(data);

                //Send the message.
                SmtpClient client = new SmtpClient("smtp.gmail.com");

                client.Credentials = CredentialCache.DefaultNetworkCredentials;

                client.Send(message);
            }

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春夜浅 2024-11-21 00:58:23

对于:

“SMTP 服务器需要安全连接,或者客户端未经过身份验证。服务器响应为:5.5.1 需要身份验证”

尝试使用:

 var client = new SmtpClient("smtp.gmail.com", 587)
        {
            Credentials = new NetworkCredential("username", "password"),
            EnableSsl =true
        };

        client.Send(message);

for:

"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required"

Try use:

 var client = new SmtpClient("smtp.gmail.com", 587)
        {
            Credentials = new NetworkCredential("username", "password"),
            EnableSsl =true
        };

        client.Send(message);
等风也等你 2024-11-21 00:58:23

如果您不希望您的应用程序挂起,而这可能需要一些时间(如果图像很大,或者服务器没有响应,您需要将其放入单独的线程中。(许多示例已经存在)

因为我们中的一些人也有指出,您还需要发送电子邮件,上面的代码不会这样做,当然要注意,如果 gmail 认为您正在尝试通过它们转发,则邮件可能不会发送。

If you dont want your app to hang while this may take some time (if the image is big, or the servers are unresponsive, you need to put it into a separate thread. (Many examples already exist)

As a few of us have also pointed out, you need to also send the Email, your code above doesnt do that. Be aware of course that if gmail thinks you're trying to relay through them, the mail probably wont send.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文