You must Sign In to post a response.
  • Category: ASP.NET

    What is Attachment class?

    Hi everyone
    Please explain about Attachment Class
    Try to give code and where can we use it
    Thank you for your time and consideration

  • #648515
    In what context you are talking about.
    I have seen Attachment Property in System.Net.Mail class.
    While sending mail you have to create an object of MailMessage where if you want then you can add attachment using this property.
    here is the code for the same

    MailMessage msg = new MailMessage(FromAddress,ToAddress);
    msg.Attachment.Add(new Attachment("FileName"));

    So the attachment class mainly attach files to the MailMessage class.
    you can give the file path for attachment of directly assign stream to it.
    For more information please google it.

    Dharmendra Kumar

  • #648524
    The Attachment class is used for creating and managing individual attachments of the MailMessage object. Attachments can be created from streams or file paths. The stream or file path must be set in the ctor of the Attachment.

    Below is an example demonstrating the Attachment class:(C#)

    static void AttachmentFromFile()
    //create the mail message
    MailMessage mail = new MailMessage();

    //set the addresses
    mail.From = new MailAddress("");

    //set the content
    mail.Subject = "This is an email";
    mail.Body = "this content is in the body";

    //add an attachment from the filesystem
    mail.Attachments.Add(new Attachment("c:\\temp\\example.txt"));

    //to add additional attachments, simply call .Add(...) again
    mail.Attachments.Add(new Attachment("c:\\temp\\example2.txt"));
    mail.Attachments.Add(new Attachment("c:\\temp\\example3.txt"));

    //send the message
    SmtpClient smtp = new SmtpClient("");


    SQL Server Programmers and Consultants

  • #648575
    Attachment Class:Provides properties and methods for constructing a single attachment to the mail message.

    using MailBee;
    using MailBee.Mime;

    MailMessage mesg = new MailMessage();

    foreach (Attachment atch in mesg.Attachments)
    Console.WriteLine("Attachment name is " + atch.Filename);

    Software Engg.

  • #648576

    below link will work for you.

    /* Beginning of Attachment1 process &
    Check the first open file dialog for a attachment */
    if (inpAttachment1.PostedFile != null)
    /* Get a reference to PostedFile object */
    HttpPostedFile attFile = inpAttachment1.PostedFile;
    /* Get size of the file */
    int attachFileLength = attFile.ContentLength;
    /* Make sure the size of the file is > 0 */
    if (attachFileLength > 0)
    /* Get the file name */
    strFileName = Path.GetFileName(inpAttachment1.PostedFile.FileName);
    /* Save the file on the server */
    /* Create the email attachment with the uploaded file */
    MailAttachment attach = new MailAttachment(Server.MapPath(strFileName));
    /* Attach the newly created email attachment */
    /* Store the attach filename so we can delete it later */
    attach1 = strFileName;


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.