Send the downloaded file as an attachment

I am trying to upload the downloaded file as an attachment to my ashx file. Here is the code I'm using:

 HttpPostedFile fileupload = context.Request.Files[0]; //filename w/o the path string file = Path.GetFileName(fileupload.FileName); MailMessage message = new MailMessage(); //*****useless stuff******** message.To.Add(" abc@xxx.com "); message.Subject = "test"; message.From = new MailAddress(" test@aaa.com "); message.IsBodyHtml = true; message.Body = "testing"; //*****useless stuff******** //Fault line message.Attachments.Add(new Attachment(file, MediaTypeNames.Application.Octet)) //Send mail SmtpClient smtp = new System.Net.Mail.SmtpClient("xxxx", 25); smtp.UseDefaultCredentials = false; smtp.Credentials = new NetworkCredential("xxx", "xxxx"); smtp.Send(message); 

I can send an email without attachment. Do I need to save the file first and then add the attachment?

+10
source share
4 answers

FileName is the name of the file on the client, not on the server. You will need to use SaveAs or InputStream to get the content in the application.

Here is a link to the MSDN documentation.

+2
source

You should NOT and should not save attachments to the server unnecessarily. ASP Snippets has an article on how to do this in ASP.NET WebForms.

Doing this in C # MVC is even nicer:

 public IEnumerable<HttpPostedFileBase> UploadedFiles { get; set; } var mailMessage = new MailMessage(); // ... To, Subject, Body, etc foreach (var file in UploadedFiles) { if (file != null && file.ContentLength > 0) { try { string fileName = Path.GetFileName(file.FileName); var attachment = new Attachment(file.InputStream, fileName); mailMessage.Attachments.Add(attachment); } catch(Exception) { } } } 
+18
source

Following the steps of Serge Sagan, here is a handler using web forms, but with <input type="file" name="upload_your_file" /> instead of the <asp:FileUpload> :

 HttpPostedFile file = Request.Files["upload_your_file"]; if (file != null && file.ContentLength > 0) { string fileName = Path.GetFileName(file.FileName); var attachment = new Attachment(file.InputStream, fileName); mailMessage.Attachments.Add(attachment); } 

This is useful if you do not need (or cannot add) runat="server" in your form tag.

+5
source

You can do the following:

 private void btnSend_Click(object sender,EventArgs e) { MailMessage myMail = new MailMessage(); myMail.To = this.txtTo.Text; myMail.From = "<" + this.txtFromEmail.Text + ">" + this.txtFromName.Text; myMail.Subject = this.txtSubject.Text; myMail.BodyFormat = MailFormat.Html; myMail.Body = this.txtDescription.Text.Replace("\n","<br>"); //*** Files 1 ***// if(this.fiUpload1.HasFile) { this.fiUpload1.SaveAs(Server.MapPath("MyAttach/"+fiUpload1.FileName)); myMail.Attachments.Add(new MailAttachment(Server.MapPath("MyAttach/"+fiUpload1.FileName))); } //*** Files 2 ***// if(this.fiUpload2.HasFile) { this.fiUpload2.SaveAs(Server.MapPath("MyAttach/"+fiUpload2.FileName)); myMail.Attachments.Add(new MailAttachment(Server.MapPath("MyAttach/"+fiUpload2.FileName))); } SmtpMail.Send(myMail); myMail = null; this.pnlForm.Visible = false; this.lblText.Text = "Mail Sending."; } 
+4
source

Source: https://habr.com/ru/post/903719/


All Articles