简体   繁体   English

发送带有附件C#的邮件

[英]Send mail with attachment C#

I am writing an application that is supposed to send an email, with up to 3 attachments. 我正在编写一个应发送电子邮件的应用程序,最多包含3个附件。

It is just a really simple web form, with 3 FileUpload controls to browse the possible attachments. 这只是一个非常简单的Web表单,带有3个FileUpload控件以浏览可能的附件。

The application is deployed in a webfarm and of course runs on server-side. 该应用程序部署在Webfarm中,并且当然在服务器端运行。

I managed to make it send the emails, but I am having problems with the attachments. 我设法使其发送电子邮件,但是附件出现问题。 Right now, I am using this procedure to attach the files: 现在,我正在使用以下过程来附加文件:

                if (fuAttatchment.HasFile)
                {                        
                    fuAttatchment.SaveAs(Server.MapPath(fuAttatchment.FileName));
                    MyMessage.Attachments.Add(new System.Net.Mail.Attachment(Server.MapPath(fuAttatchment.FileName))); 

                    filesize += fuAttatchment.PostedFile.ContentLength;
                }

The error I am getting once I submit, is the following: 提交后出现的错误如下:

Send failure: System.UnauthorizedAccessException: Access to the path 'E:\\Inetpub\\IS\\MSTicketRequest\\wallpaper-3010.jpg' is denied. 发送失败:System.UnauthorizedAccessException:拒绝访问路径“ E:\\ Inetpub \\ IS \\ MSTicketRequest \\ wallpaper-3010.jpg”。 at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode) at System.Web.HttpPostedFile.SaveAs(String filename) at System.Web.UI.WebControls.FileUpload.SaveAs(String filename) at MSTicketRequest.WebForm1.btnSubmit_Click(Object sender, EventArgs e) in C:\\Users\\ggruschka\\Desktop\\ggruschka\\MSTicketRequest\\MSTicketRequest\\Default.aspx.cs:line 54 在System.IO.FileStream.Init(字符串路径,FileMode模式,FileAccess访问,Int32权限,布尔useRights,FileShare共享,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs)处的System.IO .__ Error.WinIOError(Int32 errorCode,String mayFullPath) ,位于System.IO.FileStream..ctor处的字符串,msgPath,布尔bFromProxy,布尔useLongPath)(位于System.IO.FileStream处的字符串路径,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions选项,字符串msgPath,布尔bFromProxy) ..ctor(字符串路径,FileMode模式)位于System.Web.HttpPostedFile.SaveAs(字符串文件名),位于System.Web.UI.WebControls.FileUpload.SaveAs(字符串文件名),位于MSTicketRequest.WebForm1.btnSubmit_Click(对象发送者,EventArgs e )在C:\\ Users \\ ggruschka \\ Desktop \\ ggruschka \\ MSTicketRequest \\ MSTicketRequest \\ Default.aspx.cs:第54行

I have not been able to figure out why is this happen, probably I am missing something regardin security policies or something like that. 我无法弄清楚为什么会发生这种情况,可能我缺少有关安全策略的内容或类似内容。

Thank you very much in advance for your help ! 预先非常感谢您的帮助!

instead of this: 代替这个:

fuAttatchment.SaveAs(Server.MapPath(fuAttatchment.FileName));
                    MyMessage.Attachments.Add(new System.Net.Mail.Attachment(Server.MapPath(fuAttatchment.FileName))); 

do this: 做这个:

fuAttatchment.SaveAs("somewhere local"+fuAttatchment.FileName);
                    MyMessage.Attachments.Add(new System.Net.Mail.Attachment("somewhere local"+fuAttatchment.FileName)); 

you don't need to be saving the attachments on the server! 您无需将附件保存在服务器上!

Looks like the user that the site is running under doesn't have access to write to the target file path. 似乎该网站所运行的用户无权写入目标文件路径。 Check the directory's security permissions and make sure the IIS user has write access. 检查目录的安全权限,并确保IIS用户具有写访问权限。

Depends on what type ur application pool is. 取决于您的应用程序池的类型。 But if it is networkservice you gotta add networkservice. 但是,如果是networkservice,则必须添加networkservice。
IIS_Users for ApplicationPoolIdentity but I'm not sure on this one. IIS_Users用于ApplicationPoolIdentity,但我不确定这一点。 http://www.windowsecurity.com/articles/understanding-windows-ntfs-permissions.html http://www.windowsecurity.com/articles/understanding-windows-ntfs-permissions.html

If that doens't help you can try to remove the read only option. 如果那没有帮助,您可以尝试删除只读选项。

You an send email via your gmail account. 您通过您的Gmail帐户发送电子邮件。 Here is how to do it (I dunno if it helps). 这是操作方法(如果有帮助,我不知道)。 1. You need textbox where you are going to upload attachment. 1.您需要在其中上传附件的文本框。 2. Button 'Browse', and 'OpenFileDialog1'. 2.按钮“浏览”和“ OpenFileDialog1”。 In button 'Browse' you put this 在“浏览”按钮中,您输入了

private void btnBrowse_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            txt_attachment.Text = openFileDialog1.FileName;
        }
    }

You need button 'Send with an attachment' in which you place this: 您需要在其中放置“发送带有附件”按钮:

MailMessage mail = new MailMessage(txt_gmail.Text, txt_to.Text, txt_subject.Text, txt_body.Text);
        mail.Attachments.Add(new Attachment(txt_attachment.Text));
        SmtpClient client = new SmtpClient(txt_server.Text);
        client.Port = 587;
        client.Credentials = new System.Net.NetworkCredential(txt_gmail.Text, txt_password.Text);
        client.EnableSsl = true;
        client.Send(mail);
        MessageBox.Show("Mail sent", "Succes", MessageBoxButtons.OK);
        foreach (Control control in this.Controls)
        {
            TextBox box = control as TextBox;
            if (box != null)
            {
                box.Text = "";
            }
        }
    }

an the last thing (because when you do this it will show some errors) you need to create Gmail.dll file. 最后一件事(因为这样做会显示一些错误),您需要创建Gmail.dll文件。 Here is the link ho to do it: Here you can create Gmail.dll 这是执行此操作的链接: 在这里您可以创建Gmail.dll

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM