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

    Regards
    Kamlesh
  • #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
    dharmendra.hbti@gmail.com
    +919911308352
    http://blog.dkumar.co.in

  • #648524
    Hi,
    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("me@mycompany.com");
    mail.To.Add("you@yourcompany.com");

    //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("127.0.0.1");
    smtp.Send(mail);
    }

    Thanks,
    sql-programmers

    http://www.sql-programmers.com

    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();
    mesg.LoadMessage(@"C:\Mail.eml");

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

    Regards
    Mr.Rajesh
    Software Engg.

  • #648576
    Hi,

    below link will work for you.

    http://www.codeproject.com/KB/aspnet/SivaEMailSample.aspx

    /* 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 */
    inpAttachment1.PostedFile.SaveAs(Server.MapPath(strFileName));
    /* Create the email attachment with the uploaded file */
    MailAttachment attach = new MailAttachment(Server.MapPath(strFileName));
    /* Attach the newly created email attachment */
    mailMessage.Attachments.Add(attach);
    /* Store the attach filename so we can delete it later */
    attach1 = strFileName;
    }
    }

    Regards,
    Naveen


  • 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.