Beispiel #1
0
        void _AddInstances( ref SmtpClient[] arSmtp,
			ref SmtpClientAsyncResult[] arResult,
			int index )
        {
            int count = arSmtp.Length;
            for( int i = 0; i < count; i++ )
            {
                SmtpClient oSmtp = arSmtp[i];
                if( oSmtp == null )
                {
                    //idle instance found.

                    oSmtp = new SmtpClient();
                    //store current list item index to object instance
                    //and we can retrieve it in EASendMail events.
                    oSmtp.Tag = index;

                    //For evaluation usage, please use "TryIt" as the license code, otherwise the
                    //"invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then
                    //"trial version expired" exception will be thrown.

                    //For licensed uasage, please use your license code instead of "TryIt", then the object
                    //will never expire
                    SmtpMail oMail = new SmtpMail("TryIt");

                    //If you want to specify a reply address
                    //oMail.Headers.ReplaceHeader( "Reply-To: <reply@mydomain>" );

                    //From is a MailAddress object, in c#, it supports implicit converting from string.
                    //The syntax is like this: "*****@*****.**" or "Tester<*****@*****.**>"

                    //The example code without implicit converting
                    // oMail.From = new MailAddress( "Tester", "*****@*****.**" )
                    // oMail.From = new MailAddress( "Tester<*****@*****.**>" )
                    // oMail.From = new MailAddress( "*****@*****.**" )
                    oMail.From = textFrom.Text;

                    string name, address;
                    ListViewItem item = lstTo.Items[index];
                    name = item.Text;
                    address = item.SubItems[1].Text;

                    oMail.To.Add( new MailAddress( name, address ));

                    oMail.Subject = textSubject.Text;
                    oMail.Charset = m_arCharset[lstCharset.SelectedIndex,1];

                    //replace keywords in body text.
                    string body = textBody.Text;
                    body = body.Replace( "[$subject]", oMail.Subject );
                    body = body.Replace( "[$from]", oMail.From.ToString());
                    body = body.Replace( "[$name]", name );
                    body = body.Replace( "[$address]", address );

                    oMail.TextBody = body;

                    int y = m_arAttachment.Count;
                    for( int x = 0; x < y; x++ )
                    {
                        //add attachment
                        oMail.AddAttachment( m_arAttachment[x] as string );
                    }

                    SmtpServer oServer = new SmtpServer( textServer.Text );
                    oServer.Protocol = (ServerProtocol)lstProtocol.SelectedIndex;
                    if( oServer.Server.Length != 0 )
                    {
                        if( chkAuth.Checked )
                        {
                            oServer.User = textUser.Text;
                            oServer.Password = textPassword.Text;
                        }

                        if( chkSSL.Checked )
                            oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

                    }
                    else
                    {
                        //To send email to the recipient directly(simulating the smtp server),
                        //please add a Received header,
                        //otherwise, many anti-spam filter will make it as junk email.
                        System.Globalization.CultureInfo cur = new System.Globalization.CultureInfo("en-US");
                        string gmtdate = System.DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss zzz", cur);
                        gmtdate.Remove( gmtdate.Length - 3, 1 );
                        string recvheader = String.Format( "from {0} ([127.0.0.1]) by {0} ([127.0.0.1]) with SMTPSVC;\r\n\t {1}",
                            oServer.HeloDomain,
                            gmtdate );

                        oMail.Headers.Insert( 0, new HeaderItem( "Received", recvheader ));
                    }

                     _CrossThreadSetItemText( "Connecting ...", index );

                    //Catching the following events is not necessary,
                    //just make the application more user friendly.
                    //If you use the object in asp.net/windows service or non-gui application,
                    //You need not to catch the following events.
                    //To learn more detail, please refer to the code in EASendMail EventHandler region
                    oSmtp.OnIdle += new SmtpClient.OnIdleEventHandler( OnIdle );
                    oSmtp.OnAuthorized += new SmtpClient.OnAuthorizedEventHandler( OnAuthorized );
                    oSmtp.OnConnected += new SmtpClient.OnConnectedEventHandler( OnConnected );
                    oSmtp.OnSecuring += new SmtpClient.OnSecuringEventHandler( OnSecuring );
                    oSmtp.OnSendingDataStream += new SmtpClient.OnSendingDataStreamEventHandler( OnSendingDataStream );

                    SmtpClientAsyncResult oResult = null;
                    if( !chkTestRecipients.Checked )
                    {
                        oResult = oSmtp.BeginSendMail(
                            oServer, oMail,  null, null );
                    }
                    else
                    {
                        //Just test the email address without sending email data.
                        oResult = oSmtp.BeginTestRecipients(
                            null, oMail, null, null );
                    }

                    //Add the object instance to the array.
                    arSmtp[i] = oSmtp;
                    arResult[i] = oResult;
                    break;
                }
            }
        }