2009-07-11 15 views
15

मेल एम्बेडेड छवि asp.netमेल भेजने asp.net

मैं पहले से ही इस्तेमाल किया है का उपयोग कर निम्नलिखित के साथ भेज रहा है, लेकिन यह

Dim EM As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage(txtFrom.Text, txtTo.Text) 
     Dim A As System.Net.Mail.Attachment = New System.Net.Mail.Attachment(txtImagePath.Text) 
     Dim RGen As Random = New Random() 
     A.ContentId = RGen.Next(100000, 9999999).ToString() 
     EM.Attachments.Add(A) 
     EM.Subject = txtSubject.Text 
     EM.Body = "<body>" + txtBody.Text + "<br><img src='cid:" + A.ContentId +"'></body>" 
     EM.IsBodyHtml = True 
     Dim SC As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient(txtSMTPServer.Text) 
     SC.Send(EM) 

उत्तर

29

काम नहीं कर सकता आप उपयोग कर रहे हैं का उपयोग कर एम्बेडेड छवि के साथ। नेट 2 या इसके बाद के संस्करण आप AlternateView और इस तरह LinkedResource कक्षाओं का उपयोग कर सकते हैं:

string html = @"<html><body><img src=""cid:YourPictureId""></body></html>"; 
AlternateView altView = AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html); 

LinkedResource yourPictureRes = new LinkedResource("yourPicture.jpg", MediaTypeNames.Image.Jpeg); 
yourPictureRes.ContentId = "YourPictureId"; 
altView.LinkedResources.Add(yourPictureRes); 

MailMessage mail = new MailMessage(); 
mail.AlternateViews.Add(altView); 

उम्मीद है कि आप वीबी बराबर यह मान सकते हैं।

+2

उसी नाम वाले व्यक्ति द्वारा बीटन। –

+1

कभी-कभी ऐसा होता है;) – Alex

+6

पीटा जाना आपकी आंखों को पाने से बेहतर है, मुझे लगता है कि मैं अनुमान लगाऊंगा –

8

खोज और प्रयास करने के बाद चार या पांच 'उत्तर' होना चाहिए, मुझे लगा कि मुझे वास्तव में काम करने के लिए जो मिला वह साझा करना था क्योंकि इतने सारे लोग यह नहीं जानते कि यह कैसे करें या कुछ विस्तृत जवाब दें कि इतने सारे अन्य इसके साथ समस्याएं हैं, साथ ही कुछ करते हैं और केवल एक स्निपेट उत्तर देते हैं जिसे तब व्याख्या किया जाना चाहिए। चूंकि मेरे पास ब्लॉग नहीं है लेकिन मैं दूसरों की मदद करना चाहता हूं, यह सब कुछ करने के लिए कुछ पूर्ण कोड है। एलेक्स पेक के लिए बड़ा धन्यवाद, क्योंकि इसका जवाब विस्तारित है।

inMy.aspx asp.net फ़ाइल

<div> 
    <asp:LinkButton ID="emailTestLnkBtn" runat="server" OnClick="sendHTMLEmail">testemail</asp:LinkButton> 
</div> 

C# फ़ाइल के पीछे inMy.aspx.cs कोड

protected void sendHTMLEmail(object s, EventArgs e) 
{ 
    /* adapted from http://stackoverflow.com/questions/1113345/sending-mail-along-with-embedded-image-using-asp-net 
     and http://stackoverflow.com/questions/886728/generating-html-email-body-in-c-sharp */ 

    string myTestReceivingEmail = "[email protected]"; // your Email address for testing or the person who you are sending the text to. 
    string subject = "This is the subject line"; 
    string firstName = "John"; 
    string mobileNo = "07711 111111"; 

    // Create the message. 
    var from = new MailAddress("[email protected]ddress.co.uk", "displayed from Name"); 
    var to = new MailAddress(myTestReceivingEmail, "person emailing to's displayed Name"); 
    var mail = new MailMessage(from, to); 
    mail.Subject = subject; 

    // Perform replacements on the HTML file (which you're using as a template). 
    var reader = new StreamReader(@"c:\Temp\HTMLfile.htm"); 
    string body = reader.ReadToEnd().Replace("%TEMPLATE_TOKEN1%", firstName).Replace("%TEMPLATE_TOKEN2%", mobileNo); // and so on as needed... 

    // replaced this line with imported reader so can use a templete .... 
    //string html = body; //"<html><body>Text here <br/>- picture here <br /><br /><img src=""cid:SACP_logo_sml.jpg""></body></html>"; 

    // Create an alternate view and add it to the email. Can implement an if statement to decide which view to add // 
    AlternateView altView = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html); 

    // Logo 1 // 
    string imageSource = (Server.MapPath("") + "\\logo_sml.jpg"); 
    LinkedResource PictureRes = new LinkedResource(imageSource, MediaTypeNames.Image.Jpeg); 
    PictureRes.ContentId = "logo_sml.jpg"; 
    altView.LinkedResources.Add(PictureRes); 

    // Logo 2 // 
    string imageSource2 = (Server.MapPath("") + "\\booking_btn.jpg"); 
    LinkedResource PictureRes2 = new LinkedResource(imageSource2, MediaTypeNames.Image.Jpeg); 
    PictureRes2.ContentId = "booking_btn.jpg"; 
    altView.LinkedResources.Add(PictureRes2); 

    mail.AlternateViews.Add(altView); 

    // Send the email (using Web.Config file to store email Network link, etc.) 
    SmtpClient mySmtpClient = new SmtpClient(); 
    mySmtpClient.Send(mail); 
} 

HTMLfile.htm

<html> 
<body> 
    <img src="cid:logo_sml.jpg"> 
    <br /> 
    Hi %TEMPLATE_TOKEN1% . 
    <br /> 
    <br/> 
    Your mobile no is %TEMPLATE_TOKEN2% 
    <br /> 
    <br /> 
    <img src="cid:booking_btn.jpg"> 
</body> 
</html> 
अपने Web.config फ़ाइल में

, आपके < कॉन्फ़िगरेशन> ब्लॉक के अंदर आपको अपने c: \ dri पर TempMail फ़ोल्डर में परीक्षण की अनुमति देने के लिए निम्न की आवश्यकता है

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="SpecifiedPickupDirectory" from="[email protected]"> 
      <specifiedPickupDirectory pickupDirectoryLocation="C:\TempMail"/> 
     </smtp> 
    </mailSettings> 
</system.net> 

केवल अन्य बातों ve आप अपने aspx.cs के शीर्ष पर की आवश्यकता होगी फ़ाइल के पीछे कोड का उपयोग कर प्रणाली भी शामिल है (अगर मैं अज्ञात वर्ग पर तुम बाहर एक बस ठीक क्लिक याद किया और चयन किया है 'हल' विकल्प)

using System.Net.Mail; 
using System.Text; 
using System.Reflection; 
using System.Net.Mime; // need for mail message and text encoding 
using System.IO; 

आशा इस मदद करता है जवाब काम करवाने के लिए की जरूरत है देने के लिए ऊपर पोस्टर के लिए किसी और बड़े धन्यवाद (और साथ ही मेरी कोड में अन्य लिंक)।

यह काम करता है लेकिन सुधार के लिए खुला हूं।

चीयर्स।

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