2016-10-06 16 views
7

पर त्रुटि मैं एप्लिकेशन को किसी एसएमटीपी सर्वर के माध्यम से छवि अनुलग्नकों के साथ ईमेल भेजता है (sendgrid) हैएसएमटीपी मेल सर्वर (sendgrid) प्रस्तुत

जब अनुप्रयोग यह एक सॉकेट कनेक्शन intitilaizes और उपयोगकर्ता (आवेदन प्रमाणित करता है की शुरूआत। मैं किसी भी ईमेल 3 त्रुटियों

निम्नलिखित मैं भेजने पर निम्न संदेश Sendgrid

SG ESMTP service ready at<foo..sendgrid.net 

से लौटे देख सकते हैं और मैं भी एक सफल प्रमाणीकरण लौटे मिलता है।

हालांकि

error 1 550 Unauthenticated senders not allowed 
error 2 503 Must have sender before recipient 
error 3 503 Must have valid receiver and originator 

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

तो मैं निम्नलिखित कोड अंश कैसे बदलूं?

 writeUTFBytes ("MAIL FROM: <"+pFrom+">\r\n"); 
     writeUTFBytes ("RCPT TO: <"+pDest+">\r\n"); 
     writeUTFBytes ("DATA\r\n"); 
     writeUTFBytes ("From: "+pFrom+"\r\n"); 
     writeUTFBytes ("To: "+pDest+"\r\n"); 

यह पूरी कक्षा है जिसका मैं उपयोग कर रहा हूं। क्लास प्रारंभ होने के बाद ही मैं प्रमाणित करता हूं और फिर छवि संलग्नक के साथ ईमेल भेजते समय अटैचमेंटमेल भेजता हूं। और जब प्रमाणीकरण के बिना एक स्थानीय SMTP सर्वर का उपयोग परीक्षण, सब कुछ ठीक (ईमेल और चित्र अटैचमेंट भेजा)

package org.bytearray.smtp.mailer 
{ 
    import flash.events.ProgressEvent; 
    import flash.net.Socket; 
    import flash.utils.ByteArray; 
    import flash.utils.getTimer; 

    import org.bytearray.smtp.crypto.MD5; 
    import org.bytearray.smtp.encoding.Base64; 
    import org.bytearray.smtp.events.SMTPEvent; 
    import org.bytearray.smtp.infos.SMTPInfos; 

    public class SMTPMailer extends Socket 
    { 
     private var sHost:String; 
     private var buffer:Array = new Array(); 

     // regexp pattern 
     private var reg:RegExp = /^\d{3}/img; 

     // PNG, JPEG header values 
     private static const PNG:Number = 0x89504E47; 
     private static const JPEG:Number = 0xFFD8; 

     // common SMTP server response codes 
     // other codes could be added to add fonctionalities and more events 
     private static const ACTION_OK:Number = 0xFA; 
     private static const AUTHENTICATED:Number = 0xEB; 
     private static const DISCONNECTED:Number = 0xDD; 
     private static const READY:Number = 0xDC; 
     private static const DATA:Number = 0x162; 
     private static const BAD_SEQUENCE:Number = 0x1F7; 

     public function SMTPMailer (pHost:String, pPort:int) 
     { 
      super (pHost, pPort);   
      sHost = pHost; 
      addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler,false,0,true); 
     } 

     public function reset():void{ 
      removeEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler); 
     } 

     /* 
     * This method lets you authenticate, just pass a login and password 
     */ 
     public function authenticate (pLogin:String, pPass:String):void 
     { 
      writeUTFBytes ("EHLO "+sHost+"\r\n"); 
      writeUTFBytes ("AUTH LOGIN\r\n"); 
      writeUTFBytes (Base64.encode64String (pLogin)+"\r\n"); 
      writeUTFBytes (Base64.encode64String (pPass)+"\r\n"); 
      flush(); 
     } 

     /* 
     * This method is used to send emails with attached files and HTML 
     * takes an incoming Bytearray and convert it to base64 string 
     * for instance pass a JPEG ByteArray stream to get a picture attached in the mail ;) 
     */ 
     public function sendAttachedMail (pFrom:String, pDest:String, pSubject:String, pMess:String, pByteArray:ByteArray, pFileName:String) :void 
     { 
      try { 

       writeUTFBytes ("HELO "+sHost+"\r\n"); 
       writeUTFBytes ("MAIL FROM: <"+pFrom+">\r\n"); 
       writeUTFBytes ("RCPT TO: <"+pDest+">\r\n"); 
       writeUTFBytes ("DATA\r\n"); 
       writeUTFBytes ("From: "+pFrom+"\r\n"); 
       writeUTFBytes ("To: "+pDest+"\r\n"); 
       writeUTFBytes ("Date : "+new Date().toString()+"\r\n"); 
       writeUTFBytes ("Subject: "+pSubject+"\r\n"); 
       writeUTFBytes ("Mime-Version: 1.0\r\n"); 

       var md5Boundary:String = MD5.hash (String (getTimer())); 

       writeUTFBytes ("Content-Type: multipart/mixed; boundary=------------"+md5Boundary+"\r\n"); 
       writeUTFBytes("\r\n"); 
       writeUTFBytes ("This is a multi-part message in MIME format.\r\n"); 
       writeUTFBytes ("--------------"+md5Boundary+"\r\n"); 
       writeUTFBytes ("Content-Type: text/html; charset=UTF-8; format=flowed\r\n"); 
       writeUTFBytes("\r\n"); 
       writeUTFBytes (pMess+"\r\n"); 
       writeUTFBytes ("--------------"+md5Boundary+"\r\n"); 
       writeUTFBytes (readHeader (pByteArray, pFileName)); 
       writeUTFBytes ("Content-Transfer-Encoding: base64\r\n"); 
       writeUTFBytes ("\r\n"); 

       var base64String:String = Base64.encode64 (pByteArray, true); 

       writeUTFBytes (base64String+"\r\n"); 
       writeUTFBytes ("--------------"+md5Boundary+"-\r\n"); 
       writeUTFBytes (".\r\n"); 
       flush(); 

      } catch (pError:Error) 
      { 
       trace("Error : Socket error, please check the sendAttachedMail() method parameters"); 
       trace("Arguments : " + arguments);  
      } 
     } 

     /* 
     * This method is used to send HTML emails 
     * just pass the HTML string to pMess 
     */ 
     public function sendHTMLMail (pFrom:String, pDest:String, pSubject:String, pMess:String):void 
     { 
      try 
      {   
       writeUTFBytes ("HELO "+sHost+"\r\n"); 
       writeUTFBytes ("MAIL FROM: <"+pFrom+">\r\n"); 
       writeUTFBytes ("RCPT TO: <"+pDest+">\r\n"); 
       writeUTFBytes ("DATA\r\n"); 
       writeUTFBytes ("From: "+pFrom+"\r\n"); 
       writeUTFBytes ("To: "+pDest+"\r\n"); 
       writeUTFBytes ("Subject: "+pSubject+"\r\n"); 
       writeUTFBytes ("Mime-Version: 1.0\r\n"); 
       writeUTFBytes ("Content-Type: text/html; charset=UTF-8; format=flowed\r\n"); 
       writeUTFBytes("\r\n"); 
       writeUTFBytes (pMess+"\r\n"); 
       writeUTFBytes (".\r\n"); 
       flush(); 

      } catch (pError:Error) 
      { 
       trace("Error : Socket error, please check the sendHTMLMail() method parameters"); 
       trace("Arguments : " + arguments); 
      } 
     } 

     /* 
     * This method automatically detects the header of the binary stream and returns appropriate headers (jpg, png) 
     * classic application/octet-stream content type is added for different kind of files 
     */ 
     private function readHeader (pByteArray:ByteArray, pFileName:String):String 
     { 
      pByteArray.position = 0; 

      var sOutput:String = null; 

      if (pByteArray.readUnsignedInt() == SMTPMailer.PNG) 
      { 
       sOutput = "Content-Type: image/png; name="+pFileName+"\r\n"; 
       sOutput += "Content-Disposition: attachment filename="+pFileName+"\r\n"; 
       return sOutput; 
      } 

      pByteArray.position = 0; 

      if (pByteArray.readUnsignedShort() == SMTPMailer.JPEG) 
      { 
       sOutput = "Content-Type: image/jpeg; name="+pFileName+"\r\n"; 
       sOutput += "Content-Disposition: attachment filename="+pFileName+"\r\n"; 
       return sOutput; 
      } 

      sOutput = "Content-Type: application/octet-stream; name="+pFileName+"\r\n"; 
      sOutput += "Content-Disposition: attachment filename="+pFileName+"\r\n"; 

      return sOutput; 
     } 

     // check SMTP response and dispatch proper events 
     // Keep in mind SMTP servers can have different result messages the detection can be modified to match some specific SMTP servers 
     private function socketDataHandler (pEvt:ProgressEvent):void 
     { 
      var response:String = pEvt.target.readUTFBytes (pEvt.target.bytesAvailable); 
      buffer.length = 0; 
      var result:Array = reg.exec(response); 

      while (result != null) 
      { 
       buffer.push (result[0]); 
       result = reg.exec(response); 
      } 

      var smtpReturn:Number = buffer[buffer.length-1]; 
      var smtpInfos:SMTPInfos = new SMTPInfos (smtpReturn, response); 

      if (smtpReturn == SMTPMailer.READY) 
       dispatchEvent (new SMTPEvent (SMTPEvent.CONNECTED, smtpInfos)); 

      else if (smtpReturn == SMTPMailer.ACTION_OK && (response.toLowerCase().indexOf ("queued") != -1 || response.toLowerCase().indexOf ("accepted") != -1 || 
        response.toLowerCase().indexOf ("qp") != -1)) dispatchEvent (new SMTPEvent (SMTPEvent.MAIL_SENT, smtpInfos)); 
      else if (smtpReturn == SMTPMailer.AUTHENTICATED) 
       dispatchEvent (new SMTPEvent (SMTPEvent.AUTHENTICATED, smtpInfos)); 
      else if (smtpReturn == SMTPMailer.DISCONNECTED) 
       dispatchEvent (new SMTPEvent (SMTPEvent.DISCONNECTED, smtpInfos)); 
      else if (smtpReturn == SMTPMailer.BAD_SEQUENCE) 
       dispatchEvent (new SMTPEvent (SMTPEvent.BAD_SEQUENCE, smtpInfos)); 
      else if (smtpReturn != SMTPMailer.DATA) 
       dispatchEvent (new SMTPEvent (SMTPEvent.MAIL_ERROR, smtpInfos));  
     } 
    } 
} 
+0

कृपया ध्यान दें कि मैं इस एसएमटीपी लाइब्रेरी का उपयोग कर रहा हूं ... http: //www.bytearray.org/? P = 27 https://code.google.com/archive/p/smtpmailer/downloads अगर आप इसे SendGrid के साथ काम करने के लिए प्राप्त कर सकते हैं और उत्तर प्रदान करेंगे I500 +12 –

उत्तर

0

So this suggests to me that I must change the order of sender and recipient in my SMTP module.

के लिए अपने आदेशों में से सही है काम करता है। इसके बजाय, आप समस्या का असली कारण के लिए पहली त्रुटि संदेश पर गौर करना चाहिए:

error 1 550 Unauthenticated senders not allowed 

यह आपको बताता है कि server requires authentication इस से, यानी उपयोगकर्ता पहचान के साथ एसएमटीपी AUTH आदेश के उपयोग से। एक अनधिकृत प्रेषक से मेल भेजने का प्रयास खारिज कर दिया जाएगा। ऐसे में, यदि MAIL FROM विफल रहता है, तो RCPT TO विफल रहता है। यदि RCPT TO विफल रहता है, तो DATA विफल रहता है। और इसी तरह।

उपयोग एसएमटीपी EHLO आदेश है कि सर्वर का समर्थन करता है AUTH योजनाओं (और अन्य क्षमताओं) को खोजने के लिए, और फिर MAIL FROM भेजने से पहले उचित AUTH आदेश भेज देते हैं।

+0

हां मुझे पता होगा, लेकिन जब मेरा एप्लिकेशन लॉन्च होता है तो मैं सफल प्रमाणीकरण देख सकता हूं। यह एक एक्सएमएल सॉकेट कनेक्शन है। चूंकि प्रमाणीकरण के लिए एक एकल उपयोगकर्ता (एप्लिकेशन) है, तो मुझे हर बार जब मैं एक ईमेल भेजूं, तो मुझे फिर से प्रमाणित करना होगा? मेरी धारणा यह है कि पहला त्रुटि संदेश सीधे दूसरी और तीसरी त्रुटियों से संबंधित था, यानी यदि मैं प्राप्तकर्ता को बदलता हूं, प्रेषक आदेश सही ढंग से त्रुटि 1 गायब हो जाना चाहिए। –

+0

@eco_bach: एसएमटीपी प्रमाणीकरण प्रत्येक एसएमटीपी कनेक्शन (यानी टीसीपी कनेक्शन) के लिए किया जाता है। आपके द्वारा दिखाए गए कोड से मुझे कोई भी प्रमाणीकरण दिखाई नहीं दे रहा है। और मुझे यह भी नहीं पता कि "एक्सएमएल सॉकेट" के साथ आपका क्या मतलब है, एसएमटीपी को एक टीसीपी सॉकेट की आवश्यकता है। शायद आपको कुछ और कोड दिखाना चाहिए, खासकर एसएमटीपी सर्वर के प्रारंभिक कनेक्शन और आप SMTP सर्वर के विरुद्ध सफलतापूर्वक प्रमाणीकरण कैसे करें। –

+0

इस लाइब्रेरी का उपयोग http://www.bytearray.org/?p=27 https://code.google.com/archive/p/smtpmailer/downloads मेरी प्रारंभिक पोस्ट –

0

अद्यतन: क्षमा करें, मैंने आपकी कक्षा की सावधानी से जांच नहीं की थी। आपके पास पहले से ही एक प्रमाणीकृत विधि है और यह वही करता है जो इसे करना चाहिए। तो जाहिर है, जबकि आपको लगता है कि आप इसे बुला रहे हैं, या तो इसे बुलाया नहीं जा रहा है, या यह विफल हो जाता है।

तो ध्यान से जाँच करें कि कनेक्शन आप और में के सत्यापन कर रहे हैं कनेक्शन आप में मेल भेज रहे हैं वास्तव में एक ही कनेक्शन हैं, और आप शायद अनजाने नहीं हैं फिर से बनाने के लिए एक नया, अप्रमाणित कनेक्शन ।

इसके अलावा, आपको वास्तव में आपके द्वारा संचालित संचालन के परिणामों की जांच करने की आवश्यकता है - इस मामले में, प्रमाणीकरण। क्या यह वास्तव में सफल है?

पुराना जवाब

अपने उदाहरण और क्या छोटे प्रलेखन मैंने पाया, आप की जरूरत से - या कम से कम दिखाई जरूरत के लिए - दो प्रमाणीकरणों। शायद with the same username and password

पहला जो आप सही कर रहे हैं, यह 'एक्सएमएल सॉकेट कनेक्शन' है जो आपको एसएमटीपी परत पर ले जाता है।

अब आपको दूसरे प्रमाणीकरण की आवश्यकता है। अन्यथा सभी आदेश विफल हो जाएंगे और त्रुटियां एक दूसरे में पैनकेक होंगी, जबकि 'वास्तविक' त्रुटि, जैसे स्टीफन उलरिक ने देखा, पहला है।

error 1 550 Unauthenticated senders not allowed 

यह हो सकता है आप sendgrid पहले अपनी इस ईमेल पते को पंजीकृत करने के लिए है कि:

writeUTFBytes ("HELO "+sHost+"\r\n"); 

// Inner authentication 
writeUTFBytes ("AUTH LOGIN\r\n"); 
writeUTFBytes (Base64.encode64 (username) + "\r\n"); 
writeUTFBytes (Base64.encode64 (password) + "\r\n"); 
// HERE you really should read the stream and ensure it says 
// "Authentication OK" -- or something to that effect. 

writeUTFBytes ("MAIL FROM: <"+pFrom+">\r\n"); 
संबंधित मुद्दे