Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dipa 88 posts 370 karma points
    Dec 22, 2015 @ 19:07
    Dipa
    0

    Get file upload for email attachment

    Hi

    I am creating one simple page which has one form. Its code is like below :

        @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using System.Net;
    @using System.Net.Mail;
    @{
        if(IsPost)
        {
           //Way 1: to get attachment
            var fileSavePath = "";
            var uploadedFile = Request.Files[0];//Here not getting file name
            var fileName = Path.GetFileName(uploadedFile.FileName);
            fileSavePath = Server.MapPath("~/media/" + fileName);
            uploadedFile.SaveAs(fileSavePath);
            FileInfo info = new FileInfo(fileSavePath);
            string[] ext = fileName.Split('.');
    
            //Way 2 :
            var a = Request["fluld"];//Getting file name only
            var b = Request.Files;//Getting null here
    
            string d = Path.GetFullPath(Request["fluld"]);
            string c = string.Empty;
    
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    
                mail.From = new MailAddress("[email protected]");
                mail.To.Add("[email protected]");
                mail.Subject = "Test Mail";
                mail.Body = "This is for testing SMTP mail from GMAIL";
    
                System.Net.Mail.Attachment attachment;
                attachment = new System.Net.Mail.Attachment("filepath");
                mail.Attachments.Add(attachment);
    
                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "******");
                SmtpServer.EnableSsl = true;
    
                //SmtpServer.Send(mail);
                //MessageBox.Show("mail Send");
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
    }
    <form method="post">
        <input type="file" name="fluld" id="fluld" />
        <input type="submit" value="Sub"/>
    </form>
    

    I am not able to get file for email attachment with this Request.Files. Help me with this. Any thing I need to add?

    Thanks

    Dipa

  • Yasir Butt 161 posts 371 karma points
    Dec 22, 2015 @ 20:15
    Yasir Butt
    100

    Atleast your form should have enctype = "multipart/form-data". As <form method="post" enctype="multipart/form-data">

  • Dipa 88 posts 370 karma points
    Dec 23, 2015 @ 04:17
    Dipa
    0

    Hi,

    Thanks, I forgot that. It works.

  • Dipa 88 posts 370 karma points
    Dec 23, 2015 @ 04:25
    Dipa
    0

    Hi Yasir,

    I have one more doubt. When mail sent successfully it should get redirect to another page. How to do that? Can you help me with this? I am not getting it.

    Thanks,

    Dipa

Please Sign in or register to post replies

Write your reply to:

Draft