2011-12-13 16 views
6

मैं अपनी ashx फ़ाइल में अनुलग्नक के रूप में एक अपलोड की गई फ़ाइल को भेजने की कोशिश कर रहा हूं। यहां मैं जिस कोड का उपयोग कर रहा हूं वह है:अनुलग्नक के रूप में अपलोड की गई फ़ाइल भेजें

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("[email protected]"); 
message.Subject = "test"; 
message.From = new MailAddress("[email protected]"); 
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); 

मैं संलग्नक के बिना ईमेल भेजने में सक्षम हूं। क्या मुझे पहले फ़ाइल को सहेजने की आवश्यकता है और फिर अटैचमेंट में जोड़ें?

+0

आपको पहले फ़ाइल को सहेजने की आवश्यकता है; यहां एक मेल संदेश में एक छवि संलग्न करने के समान प्रश्न देखें: http://stackoverflow.com/questions/6105904/how-to-send-image-as-attachement-without-saving-it-in-file-system – dash

+0

हाँ बात मिल गई। मैं Server.MapPath() का उपयोग नहीं कर सका। समाधान HttpContext.Current.Server.MapPath() है। – nikhil

उत्तर

2

फ़ाइल नाम क्लाइंट पर फ़ाइल का नाम है, सर्वर पर नहीं। अनुलग्नक में कोई भी सामग्री प्राप्त करने के लिए आपको SaveAs या इनपुटस्ट्रीम का उपयोग करने की आवश्यकता होगी।

Here is a link एमएसडीएन दस्तावेज़ीकरण में।

+0

धन्यवाद प्रश्न में कोड को अपडेट किया गया। – nikhil

+2

आपको इसे पहले डिस्क पर सहेजने की आवश्यकता नहीं है ... यह खराब अभ्यास है। –

+0

@ सर्जसागन: मुझे खेद है, लेकिन आप गलत हैं। यह किसी भी प्रकार के कॉर्पोरेट पर्यावरण या किसी भी पर्यावरण में सुरक्षा का सबसे अच्छा अभ्यास है। डिस्क पर सहेजने से एंटी-वायरस चेकर्स वेब सर्वर से परे किसी भी स्थान पर जाने से पहले फ़ाइल को स्कैन करने की अनुमति देता है, इस प्रकार संभावित मैलवेयर के आंतरिक नेटवर्क में फैलता है। –

3

आप इस तरह कर सकते हैं:

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."; 
} 
+0

यह एक एएसपीएक्स फ़ाइल में काम करेगा। मैं एएसएक्स – nikhil

+0

@ निखिल हाँ का उपयोग कर रहा हूं, आप इसे बदल सकते हैं यह आपकी आवश्यकताओं पर निर्भर करता है ... –

12

आप की जरूरत नहीं है, और न ही आप, अनावश्यक रूप से सर्वर से अनुलग्नकों को सहेजने चाहिए। यहाँ कैसे ASP.NET WebForms http://www.aspsnippets.com/articles/Attach-files-to-email-without-storing-on-disk-using-ASP.Net-FileUpload-Control.aspx

सी # MVC में यह कर में यह करने के लिए पर एक लेख भी अच्छे है:

 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) { } 
      } 
     } 
3

सर्ज सगन की the footsteps में के बाद, यहाँ webforms का उपयोग कर एक हैंडलर है, लेकिन <input type="file" name="upload_your_file" /> साथ बजाय <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); 
} 

यह अगर आप अपने प्रपत्र टैग पर एक runat="server" की जरूरत नहीं है (या नहीं जोड़ सकते हैं) उपयोगी है।

संबंधित मुद्दे