Sending an attachment that the user chose with mail
Asked Answered
B

2

1

The problem:

I want that users can send me mails with attachments. They can choose the file with an input file button in html. The problem is that it can't find the file. It works fine without attachments.

I get this error

File C: Program Files (x 86) ExpressGIPENGLISH .pptx IIS cannot be found.

Does anybody have any ideas?

What I tried:

Tried first uploading the file to that location but still doesn't work.

Input file button

<INPUT type=file id=File1 name=File1 runat="server" >&nbsp; </asp:Content>

C# Code

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(System.IO.Path.GetFileName(File1.PostedFile.FileName));

MailMessage mail = new MailMessage("d***[email protected]", "d***[email protected]");

SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Port = 587;              
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("d***[email protected]", "");

mail.BodyEncoding = Encoding.UTF8;
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mail.Subject = TxtOnderwerp.Text;
mail.Body = TxtMail.Text;
mail.Body += Environment.NewLine + "Van  " + TxtNaam.Text;
mail.Body += Environment.NewLine + " Deze persoon is te bereiken op het mail adres " + TxtEmail.Text + " of op het nummer " + TxtTel.Text;
mail.Attachments.Add(attachment);

client.Send(mail);

Result: I want that a user can send me a mail with an attachment that he chose himself that is on his computer. And that I can receive the mail and open the attachment. Thank you in advance

Brokendown answered 4/4, 2019 at 14:32 Comment(0)
A
2

Below a complete example to add files to an email message as attachment without writing them to the disk.

using (SmtpClient client = new SmtpClient())
using (MailMessage message = new MailMessage())
{
    client.Host = "host.com";
    client.Port = 25;
    client.Timeout = 10000;
    client.EnableSsl = false;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Credentials = new NetworkCredential("user", "pass");

    message.From = new MailAddress("[email protected]", "VDWWD");
    message.To.Add(new MailAddress("[email protected]"));
    message.Subject = "Your uploaded files";
    message.IsBodyHtml = true;
    message.Body = "<html><head></head><body><font face=\"arial\" size=\"2\"><b>The files you uploaded.</b></font></body></html>";

    //loop all the uploaded files
    foreach (var file in FileUpload1.PostedFiles)
    {
        //add the file from the fileupload as an attachment
        message.Attachments.Add(new Attachment(file.InputStream, file.FileName, MediaTypeNames.Application.Octet));
    }

    //send mail
    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        //handle error
    }
}
Acquittance answered 4/4, 2019 at 17:16 Comment(0)
L
0

You will need to save the posted file to your server before you can add it as an attachment. You can also add the attachment from a MemoryStream, so this could be another way without having to save the file itself.

Launcher answered 4/4, 2019 at 14:34 Comment(3)
You will need to save the posted file to your server before you can add it as an attachment. You can also add the attachment from a MemoryStream, so this could be another way without having to save the file itself. ... or the simplest way: use HttpPostedFile.InputStream directly to create Attachment ... no saving, no unnecessary MemoryStreamWoodberry
@Woodberry do u some more info about that or an example? I looked it up and it's verry complicated for me. Started programming only a few months ago .Thank you in advanceBrokendown
No, as @Woodberry says you do not need to store it on the server first to add it as an attachment.Acquittance

© 2022 - 2024 — McMap. All rights reserved.