1

I am trying to mail with attachments using asp.net. To do this I have done the following code:

Default.aspx.cs:

using (MailMessage mailMessage = new MailMessage())
        {
            mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["UserName"]);
            mailMessage.Subject = "Test Subject";
            mailMessage.Body = myString.ToString();
            if (fuAttachment.HasFile)
            {
                string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
                mailMessage.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
            }
            mailMessage.IsBodyHtml = true;
            mailMessage.To.Add(new MailAddress(txtem.Text.ToString()));
            mailMessage.CC.Add(new MailAddress("sample@test.com"));
            mailMessage.Bcc.Add(new MailAddress("sample1@test.com"));

            SmtpClient smtp = new SmtpClient();
            smtp.Host = ConfigurationManager.AppSettings["Host"];
            smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
            NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
            NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
            smtp.Send(mailMessage);
        }

Default.aspx:

<tr>
                                        <td align="left">Passport Scan Copy</td>
                                        <td align="left">
                                            <asp:FileUpload ID="fuAttachment" runat="server" />
                                        </td>
                                        </tr>

Now with this code I can send email properly but there is no attachment! Is it because I have to upload the file separately in the server. Am actually clueless on this point can anyone please help me.

user3305327
  • 897
  • 4
  • 18
  • 34
  • If that is server side code, then the files being attached must be accessible as files or in the form of a stream. If a web server this means the content must have been uploaded. – Richard May 20 '15 at 09:42
  • http://stackoverflow.com/questions/15017132/how-to-send-email-with-attachment-in-asp-net – Massimiliano Peluso May 20 '15 at 09:43
  • @Richard...to upload it in the server I did the following code:if (fuAttachment.HasFile) { try { string filename = Path.GetFileName(fuAttachment.FileName); fuAttachment.SaveAs(Server.MapPath("~/") + filename); } catch (Exception ex) { uploaded. The following error occured: " + ex.Message; Response.Write(ex.ToString()); } } But it didn't uploaded – user3305327 May 20 '15 at 10:36

1 Answers1

0

Set your form enctype to multipart/form-data. Otherwise the file does not get posted to the server and your attachment will not be sent.

<form runat="server" enctype="multipart/form-data">
Clark Kent
  • 305
  • 1
  • 4
  • 16
  • I don't have form tag in the page...its designed by site.master – user3305327 May 20 '15 at 10:17
  • You can safely add the enctype attribute to the form tag in site.master. It should not affect the behaviour of other forms. – Clark Kent May 20 '15 at 10:49
  • ...I have added it but still the attachment is not going – user3305327 May 20 '15 at 10:51
  • can you put a breakpoint on this line "if (fuAttachment.HasFile)"? Is the value of HasFile true? – Clark Kent May 20 '15 at 10:54
  • ...while trying to do this from a separate page the file got uploaded in the server but from this page its not uploading and surprisingly there is no error also showing – user3305327 May 20 '15 at 11:29
  • So the problem is not the code that sends the mail. The problem must be in the webform. Can you debug the page, use your browser to view the html and post the
    tag that is rendered into the page please?
    – Clark Kent May 20 '15 at 11:45
  • Surprisingly I just found from this code if I click on any button that is not triggering any functions...whereas there is no error its showing...shall I share those files with you here...can you please check it once – user3305327 May 20 '15 at 11:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78318/discussion-between-clark-kent-and-user3305327). – Clark Kent May 20 '15 at 11:55