none
relaying denied for 是什麼原因?? RRS feed

  • 問題

  • 各位達人好,關於在網站上自動回信的程式,以下是節錄的程式碼,使用時發生錯誤訊息:

    mx1.mail.tw.yahoo.com無法使用信箱。 伺服器回應為: relaying denied for

    請問是什麼原因?? 可以解嗎??

    呼叫:

    try
            {
                ArrayList dnsServers = new ArrayList();

                //name/ip of the dnsservers
                dnsServers.Add("x.x.x.x");

                DnsLite dl = new DnsLite();
                dl.setDnsServers(dnsServers);
                ArrayList results = null;
                results = dl.getMXRecords("yahoo.com.tw");
                for (int i = 0; i < results.Count; i++)
                {

                    MXRecord mx = (MXRecord)resultsIdea;
                    Response.Write(i + " : " + mx+"<br>");
                    Response.Write(mx.ToString());
                    bool isok = sendMail(mx.ToString(), "", "", true, "aaa.@msn.com", "eee@msn.com", "Subject", "Body");
                    if (isok.ToString() != "1")
                    {
                        return;
                    }
                }

                results = null;
            }
            catch (Exception ee)
            {

                Response.Write("Caught exception : " + ee);
            }

     

     

    摘入:

    public bool sendMail(string host, string userName, string pwd, bool ssl, string from, string to, string subject, string body)
        {
            System.Net.Mail.SmtpClient mail = new System.Net.Mail.SmtpClient();
            mail.Host = host;//smtp
            mail.Credentials = new System.Net.NetworkCredential(userName, pwd);
            //mail.EnableSsl = ssl;//发送连接套接层是否加密 例如用gmail发是加密的

            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);
            message.Body = body;
            message.Subject = subject;
            message.SubjectEncoding = System.Text.Encoding.GetEncoding("utf-8");
            message.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
            message.IsBodyHtml = true;
            try
            {
                mail.Send(message);
                Response.Write("Send");
                return true;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                return false;
            }
        }

     

    public class MXRecord
    {

        public int preference = -1;
        public string exchange = null;

        public override string ToString()
        {

            return exchange; //"Preference : " + preference + " Exchange : " + exchange;
        }
    }
    public class DnsLite
    {

        private byte[] data;
        private int position, id, length;
        private string name;
        private ArrayList dnsServers;

        private static int DNS_PORT = 53;

        Encoding ASCII = Encoding.ASCII;

        public DnsLite()
        {

            id = DateTime.Now.Millisecond * 60;
            dnsServers = new ArrayList();

        }

        public void setDnsServers(ArrayList dnsServers)
        {

            this.dnsServers = dnsServers;

        }
        public ArrayList getMXRecords(string host)
        {

            ArrayList mxRecords = null;

            for (int i = 0; i < dnsServers.Count; i++)
            {

                try
                {

                    mxRecords = getMXRecords(host, (string)dnsServersIdea);
                    Console.WriteLine(mxRecordsIdea.ToString());
                    //HttpContext.Current.Response.Write(mxRecordsIdea.ToString());
                    break;

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

            }

            return mxRecords;
        }

        private int getNewId()
        {

            //return a new id
            return ++id;
        }

        public ArrayList getMXRecords(string host, string serverAddress)
        {

            //opening the UDP socket at DNS server
            //use UDPClient, if you are still with Beta1
            UdpClient dnsClient = new UdpClient(serverAddress, DNS_PORT);

            //preparing the DNS query packet.
            makeQuery(getNewId(), host);

            //send the data packet
            dnsClient.Send(data, data.Length);

            IPEndPoint endpoint = null;
            //receive the data packet from DNS server
            data = dnsClient.Receive(ref endpoint);

            length = data.Length;

            //un pack the byte array & makes an array of MXRecord objects.
            return makeResponse();

        }

        //for packing the information to the format accepted by server
        public void makeQuery(int id, String name)
        {

            data = new byte[512];

            for (int i = 0; i < 512; ++i)
            {
                dataIdea = 0;
            }

            data[0] = (byte)(id >> 8);
            data[1] = (byte)(id & 0xFF);
            data[2] = (byte)1; data[3] = (byte)0;
            data[4] = (byte)0; data[5] = (byte)1;
            dataDevil = (byte)0; data[7] = (byte)0;
            dataMusic = (byte)0; data[9] = (byte)0;
            data[10] = (byte)0; data[11] = (byte)0;

            string[] tokens = name.Split(new char[] { '.' });
            string label;

            position = 12;

            for (int j = 0; j < tokens.Length; j++)
            {

                label = tokens[j];
                data[position++] = (byte)(label.Length & 0xFF);
                byte[] b = ASCII.GetBytes(label);

                for (int k = 0; k < b.Length; k++)
                {
                    data[position++] = b[k];
                }

            }

            data[position++] = (byte)0; data[position++] = (byte)0;
            data[position++] = (byte)15; data[position++] = (byte)0;
            data[position++] = (byte)1;

        }

        //for un packing the byte array
        public ArrayList makeResponse()
        {

            ArrayList mxRecords = new ArrayList();
            MXRecord mxRecord;

            //NOTE: we are ignoring the unnecessary fields.
            //  and takes only the data required to build
            //  MX records.

            int qCount = ((data[4] & 0xFF) << 8) | (data[5] & 0xFF);
            if (qCount < 0)
            {
                throw new IOException("invalid question count");
            }

            int aCount = ((dataDevil & 0xFF) << 8) | (data[7] & 0xFF);
            if (aCount < 0)
            {
                throw new IOException("invalid answer count");
            }

            position = 12;

            for (int i = 0; i < qCount; ++i)
            {
                name = "";
                position = proc(position);
                position += 4;
            }

            for (int i = 0; i < aCount; ++i)
            {

                name = "";
                position = proc(position);

                position += 10;

                int pref = (data[position++] << 8) | (data[position++] & 0xFF);

                name = "";
                position = proc(position);

                mxRecord = new MXRecord();

                mxRecord.preference = pref;
                mxRecord.exchange = name;

                mxRecords.Add(mxRecord);

            }

            return mxRecords;
        }

        private int proc(int position)
        {

            int len = (data[position++] & 0xFF);

            if (len == 0)
            {
                return position;
            }

            int offset;

            do
            {
                if ((len & 0xC0) == 0xC0)
                {
                    if (position >= length)
                    {
                        return -1;
                    }
                    offset = ((len & 0x3F) << 8) | (data[position++] & 0xFF);
                    proc(offset);
                    return position;
                }
                else
                {
                    if ((position + len) > length)
                    {
                        return -1;
                    }
                    name += ASCII.GetString(data, position, len);
                    position += len;
                }

                if (position > length)
                {
                    return -1;
                }

                len = data[position++] & 0xFF;

                if (len != 0)
                {
                    name += ".";
                }
            } while (len != 0);

            return position;
        }
    }

    2008年1月21日 上午 02:27

解答

  • 知道了!! 原因是一般smtp都有擋信,所以只有將目的信箱用相同的Domain才可以,如下,否則就變轉信了:

    bool isok = sendMail(mx.ToString(), "", "", true, "aaa.@msn.com", "eee@yahoo.com.tw", "Subject", "Body");

     

    2008年1月21日 上午 06:37

所有回覆

  • 你的 IP 應該被 Yahoo 的 Mail Server 給阻擋了,或其他原因。但你的程式應該沒寫錯才對!


    ----
    From: Will
    Blog: http://blog.miniasp.com/
    記載著 Will 在網路世界的學習心得與技術分享

     

    2008年1月21日 上午 03:41
  • 知道了!! 原因是一般smtp都有擋信,所以只有將目的信箱用相同的Domain才可以,如下,否則就變轉信了:

    bool isok = sendMail(mx.ToString(), "", "", true, "aaa.@msn.com", "eee@yahoo.com.tw", "Subject", "Body");

     

    2008年1月21日 上午 06:37