email attachments not attaching

I am authoring a simple online application form which allows a user to upload cover letter and/or resume. The files are then stored in a database and also emailed to the correct person as attachments. The files are being stored in the database with no problems but they are not being attached to the email properly. Below is the snippet of the code used to attach the files and send the mail:

// Create the mail message
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("noreply@mydomain.com",
    OfficeEmailHiddenField.Value, "Application", message);
mail.IsBodyHtml = true;
            
// Prepare attachements
if (coverLetterFileUpload.HasFile)
{
    // Get the cover letter file
    HttpPostedFile coverLetter = coverLetterFileUpload.PostedFile;

    // Creat the attachment from the cover letter document
    System.Net.Mail.Attachment coverLetterAttachment = new System.Net.Mail.Attachment(coverLetter.InputStream,
        Path.GetFileName(coverLetter.FileName), coverLetter.ContentType);

    // Attach the file
    mail.Attachments.Add(coverLetterAttachment);
}

if (resumeFileUpload.HasFile)
{
    // Get the resume file
    HttpPostedFile resume = resumeFileUpload.PostedFile;

    // Create the attachment from the resume document
    System.Net.Mail.Attachment resumeAttachment = new System.Net.Mail.Attachment(resume.InputStream,
        Path.GetFileName(resume.FileName), resume.ContentType);

    // Attach the file
    mail.Attachments.Add(resumeAttachment);
}

// Send the message
System.Net.Mail.SmtpClient mailer = new System.Net.Mail.SmtpClient();
mailer.Send(mail);
 
0
kenniejaydavis
2/28/2007 9:02:20 PM
asp.net.web-forms 93655 articles. 3 followers. Follow

5 Replies
703 Views

Similar Articles

[PageSpeed] 22

You need to upload the attachment to the server to be able to send it. Anyways, that's the only way I ever was able to send an attachment. So you need a folder that accepts upload. And basically you just upload it, send the mail with the attachment, then just delete the file. (unless you want to keep it on the server. in which case, just make the database point to the uploaded file on the server?)

 


The best way to succeed is to take small steps. You can't take small steps if you're running, so take your time, take a few steps back, and everything will work out fine.
0
Krokador
2/28/2007 9:27:16 PM

Thank you for your suggestion. I will have to do that if there is no other way, but I thought that one of the advantages of ASP.NET 2.0 was the ability to create an email attachment directly from a stream--which is what I'm trying to do--eliminating the need to impliment the archaic .NET 1.1 way of saving, attaching and deleting?

I should have fully explained that a file(s) with the proper name and content type IS being attached to the email message, but the file(s) have no data. In my testing of the application, I am trying to upload and attach a 24kb word document. When debugging and stepping through the method line-by-line, after this line executes:

// Creat the attachment from the cover letter document
System.Net.Mail.Attachment coverLetterAttachment = new System.Net.Mail.Attachment(coverLetter.InputStream,
    Path.GetFileName(coverLetter.FileName), coverLetter.ContentType);

coverLetterAttachment.ContentStream does in fact equal/reference the full coverLetter.InputString with a total length of 24064 i.e. 24kb.

I also used the debugger to watch the value of mail.Attachments[0].ContentStream.Length and after the method has executed these lines:

// Send the message
System.Net.Mail.SmtpClient mailer = new System.Net.Mail.SmtpClient();
mailer.Send(mail);

mail.Attachments[0].ContentStream.Length still equals 24064.

I can't find at what point in the application the value is changing from 24064 to 185?

0
kenniejaydavis
2/28/2007 10:02:36 PM

I created a very simple page (source code below) to confirm that attachments can be created from a System.Web.UI.WebControls.FileUpload.PostedFile.InputStream and indeed the following page executes flawlessly and the attachment is complete. Thus, the error is somwhere in my page logic and independent from any limitations of ASP.NET.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AttachmentTest.aspx.cs" Inherits="AttachmentTest" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            <asp:FileUpload ID="SampleFileUpload" runat="server" />
        </div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>
 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Net.Mail;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class AttachmentTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        // Create email attachment from the FileUpload.PostedFile.InputStream
        Attachment sample = new Attachment(SampleFileUpload.PostedFile.InputStream, Path.GetFileName(SampleFileUpload.PostedFile.FileName),
            SampleFileUpload.PostedFile.ContentType);
        
        // Create the mail message with the attachment
        MailMessage mail = new MailMessage("noreply@mydomain.com", "test@mydomain.com", "Sample", "File Attached.");
        mail.Attachments.Add(sample);

        // Send the email
        SmtpClient mailer = new SmtpClient();
        mailer.Send(mail);
    }
}
 
0
kenniejaydavis
2/28/2007 11:27:38 PM

I am having a similar problem to this with an Excel spreadsheet that I am attempting to attach via a memorystream object.  Did you ever find a solution to this problem?

0
chroniccoder
1/11/2008 2:32:59 PM

Perhaps you should try converting the MemoryStream object to a Stream object.

1    MemoryStream oMemoryStream = new MemoryStream();
2    byte[] oTemp = oMemoryStream.ToArray();
3    Stream oStream = new MemoryStream(oTemp);
Give that a roll and see how it treats you.
greg johnson
0
GregDDC
1/11/2008 4:06:40 PM
Reply:

Similar Artilces:

Attaching emails as email attachments
Hi, Is this possible? I want to be able to attach a number of emails to another email and send it. A bit like you can do in Outlook where you can just drag the emails you want to send into the new email - they then appear as attachments with the envelope icon. I can attach other kinds of file without any problems, that all works fine. It's just attaching emails that's proving to be a problem. I'd like to do something like this: Dim oldEmail1 As New MailMessage(oldMailFrom1, oldMailTo1, oldSubject1, oldBody1) Dim oldEmail2 As New MailMessage(oldMailFrom2, oldMa...

Emailing a web form with an attachment
I have the following code, understood well enough. But.. How would I go about processing the form to allow the site visitor to email an attachemtn from their pc to me via the form? <%@page language="VB" %> <%@Import Namespace="System.Web.Util" %> <HTML><BODY> <SCRIPT LANGUAGE="VB" RUNAT="server"> ' This method is called on the server when the submit ' button is clicked on the client and when the page ' posts back to itself Sub SendMail (Obj As Object, E As EventArgs) ' Instantiate a MailM...

Could not attach any attachment
Hi, I'm using bugzilla 3.1.3, perl 5.10, IIS6.0, Mysql5.0 and able to add new bugs or search bugs. But not able to attach any file. It's give some error in attachment.cgi file. Pls help me if any one know the solution. Sandesh ...

Multiple Email Attachments
I'm fairly clear on how to get multiple attachments into an email (using an arraylist), but does anyone have any examples of a web form design to cleanly allow the user to select multiple files (which I would then dump into my arraylist)? I guess I could use a standard file input control and do a postback to upload the files and add it to the array list one at a time (displaying a list on screen for the filenames as sent), but that seems kind of clunky to me. Any help would be appreciated! Have multiple file input controls on the same page, like Yahoo! mail does. All files will be...

Print all email attachments (including sub emails and their attachments)
--____OBYWTHBCXVXOFGPGVPTL____ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: base64 Content-Disposition: inline; modification-date="Wed, 31 Aug 2010 07:20:44 -0400" QW55IHdheSB0byBwcmludCBhbGwgZW1haWwgYXR0YWNobWVudCBOQU1FUyBvbmx5IChpbmNsdWRp bmcgYW55IHN1YiBlbWFpbHMgYW5kIHRoZWlyIGF0dGFjaG1lbnRzKSBhbGwgYXQgb25jZSA/Pz8= --____OBYWTHBCXVXOFGPGVPTL____ Content-Type: multipart/related; boundary="____UDFRXZIVSCHQTEJGXAIR____" --____UDFRXZIVSCHQTEJGXAIR____ Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: base64 Content-Dis...

How the Attachments are attached to Email Message
Hello Mate, Could you please give me a link to study about 1.Email Attachments and how they are added to the Email message & how to make custom Email classes.2.we are just providing the path of the file to be attached.But how it is uploded to the server and how it is transmitted through the internet to reach the client.3.How to code anti virus for attachments that had been added to the Email4.How to validate attachments for specific type like .txt,.doc etc..Much Obliged & Thanks a Lot,Myself Hi, take a look at these articles: http://www.systemnetmail.com/faq/3.4.aspx. Grz, Kr...

How do i make my web method to finish executing a 'send email with attachment' method and then delete the attachment in C#?
 I have a webmethod that calls an email sender class to send emails.I have to delete the attachments after the email is sent. I am getting an error as the  webmethod i have written below is causing an error. Please look at the last two linesin the web method (attached after the error) where i call the MailDetails() method which sends the email and DeleteAttachments() mehtod which deletes the attachments line 31 and 32.How do i make the DeleteAttachments() method wait till the emails are sent with the attachments first. i.e the mailDetails() method has sent the emails.Error:...

Can't ready the email attachments which are inside email attachments.
Please tell me this can be fixed. In Thunderbird, when I get an email attachment and open it, it works fine. The email attachment opens and I can read it. However, if that email attachment has an email attachment inside it, it doesn't work. Thunderbird will not allow me to open the email attachment inside the email attachment. If I go over to Outlook and get it there everything works fine. I switched over to Thunderbird, because Outlook was pulling two and three copies of every email in my POP accounts. But if I have to choose between Outlook downloading multiple c...

superreview requested: [Bug 410500] Add "Web form password" description to web form keychain items : [Attachment 295140] fix
Stuart Morgan <stuart.morgan@alumni.case.edu> has asked Mark Mentovai <mark@moxienet.com> for superreview: Bug 410500: Add "Web form password" description to web form keychain items https://bugzilla.mozilla.org/show_bug.cgi?id=410500 Attachment 295140: fix https://bugzilla.mozilla.org/attachment.cgi?id=295140&action=edit ...

superreview denied: [Bug 410500] Add "Web form password" description to web form keychain items : [Attachment 295140] fix
Mark Mentovai <mark@moxienet.com> has denied Stuart Morgan <stuart.morgan@alumni.case.edu>'s request for superreview: Bug 410500: Add "Web form password" description to web form keychain items https://bugzilla.mozilla.org/show_bug.cgi?id=410500 Attachment 295140: fix https://bugzilla.mozilla.org/attachment.cgi?id=295140&action=edit ------- Additional Comments from Mark Mentovai <mark@moxienet.com> Not localizable :( ...

Attachment icon shows on email with no attachment
Name: Matthew Burton McFaul Email: matthewatburtonmcfauldotcom Product: Thunderbird Summary: Attachment icon shows on email with no attachment Comments: I wrote an email that had the word "attachment" in it which triggered the keyword alert. I did not however, add an attachment. Later, I noticed that the attachment 'paper clip' icon was listed next to this mail in a Thunderbird email search results list. Browser Details: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9 From URL: http://hendrix.mozilla.org/ ...

Forwarded emails with attachments do not print attachments.
Hello. Mozilla Thunderbird ver 1.5.0.7. When user forwards emails with attachments, the email will print, the attachment can be viewed and even print-previewed, but is does not print. Some attachments are just other emails, nothing fancy. Have tried uninstalling, rebooting, and reinstalling Thunderbird. Other users with same version do not have this problem when they forward emails with attachments. Thank you. P Boulton wrote: > Hello. > Mozilla Thunderbird ver 1.5.0.7. > When user forwards emails with attachments, the email will print, the > attachment can b...

Problem with Web Content Form in a Web Application when trying to attach masterpage
Hi,When I try to create a web content form and select to use the masterpage below Visual Studio 2008 freezes. It works with any other masterpage and everything else works fine in Visual Studio. Everything compiles and runs. To replicate this problem put the below files in a new web application and then try and create a web content form and use the master page. I have tried this on a number of different machines and still have the same problem. I have used the masterpage in other projects and still falls over so I know its not the sln or proj files, and when its been deployed using our CI ser...

superreview requested: [Bug 2920] Delete attachment from mail message in folder (remove/strip attached files from email messages) : [Attachment 173637] mime changes
David Bienvenu <bienvenu@nventure.com> has asked Scott MacGregor <mscott@mozilla.org> for superreview: Bug 2920: Delete attachment from mail message in folder (remove/strip attached files from email messages) https://bugzilla.mozilla.org/show_bug.cgi?id=2920 Attachment 173637: mime changes https://bugzilla.mozilla.org/attachment.cgi?id=173637&action=edit ------- Additional Comments from David Bienvenu <bienvenu@nventure.com> this is just the libmime changes - there are lots more to follow :-) ...

Web resources about - email attachments not attaching - asp.net.web-forms

Attachment theory - Wikipedia, the free encyclopedia
Attachment theory describes the dynamics of long-term relationships between humans . Its most important tenet is that an infant needs to develop ...

Facebook Allowing Page Admins To ‘Refresh Share Attachment?’
Facebook appears to have given page administrators the option of refreshing links that they share in posts on their pages . continued... New ...

Chronik-Fotos - Mama Bean - Unconditional Attachment - Facebook
Graphic design students at the University of North Texas asked this inspiring mama, Monica, to be the face of their ad campaign. The campaign ...

New Facebook Messages Only Provides an Online Attachment Viewing Feature for Microsoft Docs
In another move highlighting Facebook’s strategic relationship with investor Microsoft, the company has given the established office software ...

App Store - Attachments.me- Gmail inbox software to efficiently manage emails, automatically send/upload ...
Get Attachments.me- Gmail inbox software to efficiently manage emails, automatically send/upload files to cloud storage(Dropbox, Box, and G Drive), ...

Team Talk: Attachments, Beliefs and Fears - YouTube
Hi, I'm John Fitzgerald. Welcome to this week's edition of Team WealthBuilders. Since I touched on Attachments, Beliefs and Fears, I've had a ...

Invention of the Day: New ‘Alternative’ Gun Attachment May Prevent Shooting Deaths
The Ferguson Police Department is testing out a new device which they think might help prevent unnecessary shooting deaths. “The Alternative” ...

100th anniversary of Anzac landings brings home strong attachments
The commemoration of the 100th anniversary of the Anzac landings draws our attention to events that so many of us have a strong attachment to. ...

iPhones - Is Our Attachment To Smartphones A Bad Thing?
They cater to our basic human hunger for connection, so is our attachment to mobiles such a bad thing?

Apple not encrypting email attachments in iOS 7, security researcher claims
Apple's Mail app in iOS 7 is failing to encrypt email attachments, leaving user data vulnerable to hackers, a security researcher claims.

Resources last updated: 1/22/2016 4:23:45 PM