Search Results

Search found 1 results on 1 pages for 'kgo'.

Page 1/1 | 1 

  • ASP.NET Send Image Attachment With Email Without Saving To Filesystem

    - by KGO
    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; } } }

    Read the article

1