如何从 C# 发送带有附件的电子邮件?
我正在尝试编写一个代码,什么可以保存图片框的内容(有效)并通过电子邮件发送(无效)。
您认为可能是什么问题?是否还有其他内容 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于:
“SMTP 服务器需要安全连接,或者客户端未经过身份验证。服务器响应为:5.5.1 需要身份验证”
尝试使用:
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:
如果您不希望您的应用程序挂起,而这可能需要一些时间(如果图像很大,或者服务器没有响应,您需要将其放入单独的线程中。(许多示例已经存在)
因为我们中的一些人也有指出,您还需要发送电子邮件,上面的代码不会这样做,当然要注意,如果 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.