使用邮件发送用户选择的附件 [英] Sending an attachment that the user chose with mail

查看:103
本文介绍了使用邮件发送用户选择的附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我希望用户可以向我发送带有附件的邮件.他们可以使用html中的输入文件按钮选择文件.问题在于它找不到文件.无需附件即可正常运行.

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.

我收到此错误

文件C:程序文件(x 86)ExpressGIPENGLISH .pptx IIS找不到.

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

有人有什么主意吗?

我尝试过的事情:

尝试先将文件上传到该位置,但仍然无法正常工作.

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

输入文件按钮

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

C#代码

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

MailMessage mail = new MailMessage("d***t@gmail.com", "d***t@gmail.com");

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***t@gmail.com", "");

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

推荐答案

在下面的完整示例中,将文件作为附件添加到电子邮件中,而无需将其写入磁盘.

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@from.nl", "VDWWD");
    message.To.Add(new MailAddress("email@to.nl"));
    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
    }
}

这篇关于使用邮件发送用户选择的附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆