使用asp.net中的system.web.Mail发送电子邮件而不保存服务器上的附件

时间:2015-11-25 13:06:25

标签: c# asp.net email smtp

当我尝试使用system.web.mail发送电子邮件时,我可以成功发送邮件而无需附件。  但是当我尝试使用附件发送邮件时,我收到错误:访问无效(拒绝访问特定目录)。因为我将文件保存在服务器上以发送邮件。

现在我的解决方案是使用附件发送邮件而不将文件存储在服务器上

请在这里帮助我使用带附件的system.web发送邮件,而不将文件存储在服务器上

Below is my previous old code



 public void SendMailtoRecruitment()
        {
            string filePath = FileUpLoad1.PostedFile.FileName;
            string filename = System.IO.Path.GetFileName(filePath);
            string ext = System.IO.Path.GetExtension(filename);
            var path = Server.MapPath("~/App_Data");
            StringBuilder sbEmailBody = new StringBuilder();

            string strFileName = "";


            sbEmailBody.Append("Dear Recruitment Team" + Environment.NewLine + Environment.NewLine);
            sbEmailBody.Append("The application is received from the following candidate for the job mentioned in the subject.");
            sbEmailBody.Append("\n");

            sbEmailBody.Append("Position Id:" + JobId);
            sbEmailBody.Append("\n");

            sbEmailBody.Append("Position Description:" + jobrole);
            sbEmailBody.Append("\n");

            sbEmailBody.Append("Candidate First Name :" + First.Text);
            sbEmailBody.Append("\n");
            sbEmailBody.Append("Candidate Last  Name :" + Last.Text);
            sbEmailBody.Append("\n");
            sbEmailBody.Append("Candidate Email :" + TxtEmailAddress.Text);
            sbEmailBody.Append("\n");

            sbEmailBody.Append("Candidate Contact No :" + Contact.Text);
            sbEmailBody.Append("\n");
            sbEmailBody.Append("Reply the Status of the Application as early as posible");


            sbEmailBody.Append("\n");
            sbEmailBody.Append("\n");
            sbEmailBody.Append("\n");


            sbEmailBody.Append("\n" + "Sri Tech Consulting" + "\n");
            MailMessage mail = new MailMessage();
            String fileName = FileUpLoad1.FileName;
            if (FileUpLoad1.PostedFile != null)
            {
                /* Get a reference to PostedFile object */
                HttpPostedFile attFile = FileUpLoad1.PostedFile;
                /* Get size of the file */
                int attachFileLength = attFile.ContentLength;
                /* Make sure the size of the file is > 0  */
                if (attachFileLength > 0)
                {
                    strFileName = Path.GetFileName(FileUpLoad1.PostedFile.FileName);
                  //  FileUpLoad1.PostedFile.SaveAs(Server.MapPath(strFileName));
                    MailAttachment attach = new MailAttachment(Server.MapPath(strFileName),MailEncoding.Base64);
                    mail.Attachments.Add(attach);




                }


                // mail.Attachments.Add(attach);
            //  mail.To = "Recruitment@btc-india.in";
             mail.To = "rajesh@btc-india.in";

                mail.From = "noreply@btc-india.in";

                mail.Subject = "Job Application : "+ jobrole;

                mail.Body = sbEmailBody.ToString();

                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");   //basic authentication

                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "noreply@btc-india.in"); //set your username here

                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "Anuhya@1999");       //set your password here

                SmtpMail.SmtpServer = "208.91.198.171";

                SmtpMail.Send(mail);

            }
        }

0 个答案:

没有答案