ASP.NET 通过电子邮件发送图像附件而不保存到文件系统
我正在尝试创建一个表单来发送带有附加图像的电子邮件,但遇到了一些问题。我正在创建的表单相当大,因此我为此问题创建了一个小型测试表单。电子邮件将发送,附件将存在于电子邮件中,但图像已损坏或无法查看。
另外..
我不想将图像保存到文件系统。您可能认为将图像文件从文件上传到流很复杂,但这是因为我正在处理的真实表单将允许通过单个文件上传添加多个文件并将其保存在会话中,因此图像不会在提交时直接来自文件上传控件。
文件:TestAttachSend.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestAttachSend.aspx.cs" Inherits="TestAttachSend" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Send Email with Image Attachment</h1>
Email Address TO: <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />
Attach JPEG Image: <asp:FileUpload ID="fuImage" runat="server" /><br />
<br />
<asp:Button ID="btnSend" runat="server" Text="Send" onclick="btnSend_Click" /><br />
<br />
<asp:label ID="lblSent" runat="server" text="Image Sent!" Visible="false" EnableViewState="false"></asp:label>
</div>
</form>
</body>
</html>
文件:TestAttachSend.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.IO;
public partial class TestAttachSend : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
if (fuImage.HasFile && fuImage.PostedFile.ContentType == System.Net.Mime.MediaTypeNames.Image.Jpeg)
{
SmtpClient emailClient = new SmtpClient();
MailMessage EmailMsg = new MailMessage();
EmailMsg.To.Add(txtEmail.Text.Trim());
EmailMsg.From = new MailAddress(txtEmail.Text.Trim());
EmailMsg.Subject = "Attached Image";
EmailMsg.Body = "Image is attached!";
MemoryStream imgStream = new MemoryStream();
System.Drawing.Image img = System.Drawing.Image.FromStream(fuImage.PostedFile.InputStream);
string filename = fuImage.PostedFile.FileName;
img.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
EmailMsg..Attachments.Add(new Attachment(imgStream, filename, System.Net.Mime.MediaTypeNames.Image.Jpeg));
emailClient.Send(EmailMsg);
lblSent.Visible = true;
}
}
}
I'm trying to create a form that will send an email with an attached image and am running into some problems. The form I am creating is rather large so I have created a small test form for the purpose of this question. The email will send and the attachment will exist on the email, but the image is corrupt or something as it is not viewable.
Also..
I do not want to save the image to the filesystem. You may think it is convoluted to take the image file from the fileupload to a stream, but this is due to the fact that the real form I am working on will allow multiple files to be added through a single fileupload and will be saved in session, thus the images will not be coming from the fileupload control directly on submit.
File: TestAttachSend.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestAttachSend.aspx.cs" Inherits="TestAttachSend" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Send Email with Image Attachment</h1>
Email Address TO: <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />
Attach JPEG Image: <asp:FileUpload ID="fuImage" runat="server" /><br />
<br />
<asp:Button ID="btnSend" runat="server" Text="Send" onclick="btnSend_Click" /><br />
<br />
<asp:label ID="lblSent" runat="server" text="Image Sent!" Visible="false" EnableViewState="false"></asp:label>
</div>
</form>
</body>
</html>
File: TestAttachSend.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.IO;
public partial class TestAttachSend : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
if (fuImage.HasFile && fuImage.PostedFile.ContentType == System.Net.Mime.MediaTypeNames.Image.Jpeg)
{
SmtpClient emailClient = new SmtpClient();
MailMessage EmailMsg = new MailMessage();
EmailMsg.To.Add(txtEmail.Text.Trim());
EmailMsg.From = new MailAddress(txtEmail.Text.Trim());
EmailMsg.Subject = "Attached Image";
EmailMsg.Body = "Image is attached!";
MemoryStream imgStream = new MemoryStream();
System.Drawing.Image img = System.Drawing.Image.FromStream(fuImage.PostedFile.InputStream);
string filename = fuImage.PostedFile.FileName;
img.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
EmailMsg..Attachments.Add(new Attachment(imgStream, filename, System.Net.Mime.MediaTypeNames.Image.Jpeg));
emailClient.Send(EmailMsg);
lblSent.Visible = true;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,您不需要创建 Image 对象。
更改:
为
另外,这假设您上传的图像是 JPEG。
Off the top of my head, you do not need to create an Image object.
Change:
to
Also, this assumes the images you are uploading are JPEGs.