تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
طريقة إرسال بريد إلكتروني من برنامجك
#1
كاتب الموضوع : محمد أسامة جندية


عليك بداية تعريف Class ونسخ الكود التالي ضمنه:


كود :
[color=#000000][COLOR=#0000bb]using System[/color][color=#007700].[/color][color=#0000bb]ServiceProcess[/color][COLOR=#007700];
[/COLOR][color=#0000bb]using System[/color][color=#007700].[/color][color=#0000bb]Net[/color][color=#007700].[/color][color=#0000bb]Mail[/color][COLOR=#007700];
[/COLOR][color=#0000bb]using System[/color][color=#007700].[/color][color=#0000bb]Text[/color][COLOR=#007700];
[/COLOR][color=#0000bb]using System[/color][color=#007700].[/color][color=#0000bb]Collections[/color][COLOR=#007700];
[/COLOR][color=#0000bb]using System[/color][COLOR=#007700];

[/COLOR][COLOR=#0000bb]namespace MOJ
[/COLOR][COLOR=#007700]{
    class [/COLOR][COLOR=#0000bb]Email
    [/COLOR][COLOR=#007700]{
        public [/COLOR][color=#0000bb]bool SendEmail[/color][color=#007700]([/color][color=#0000bb]MailAddress FromEmail[/color][color=#007700], [/color][color=#0000bb]MailAddress ToEmail[/color][color=#007700], [/color][color=#0000bb]string Subject[/color][color=#007700], [/color][color=#0000bb]MailAddressCollection ToCC[/color][color=#007700], [/color][color=#0000bb]MailAddressCollection ToBCC[/color][color=#007700], [/color][color=#0000bb]string Body[/color][color=#007700], [/color][color=#0000bb]string SMTP[/color][color=#007700], [/color][color=#0000bb]bool IsHTML[/color][color=#007700], [/color][color=#0000bb]ArrayList Attachments[/color][COLOR=#007700])
        {
            [/COLOR][color=#0000bb]CheckService[/color][color=#007700]([/color][color=#dd0000]"smtpsvc"[/color][COLOR=#007700]);

            [/COLOR][color=#0000bb]MailMessage mailMsg [/color][color=#007700]= new [/color][color=#0000bb]MailMessage[/color][color=#007700]([/color][color=#0000bb]FromEmail[/color][color=#007700], [/color][color=#0000bb]ToEmail[/color][COLOR=#007700]);
            [/COLOR][color=#0000bb]mailMsg[/color][color=#007700].[/color][color=#0000bb]Subject [/color][color=#007700]= [/color][color=#0000bb]Subject[/color][color=#007700].[/color][color=#0000bb]Trim[/color][COLOR=#007700]();
            if ([/COLOR][color=#0000bb]ToCC[/color][color=#007700].[/color][color=#0000bb]Count [/color][color=#007700]!= [/color][color=#0000bb]0[/color][color=#007700]) [/color][color=#0000bb]mailMsg[/color][color=#007700].[/color][color=#0000bb]CC[/color][color=#007700].[/color][color=#0000bb]Add[/color][color=#007700]([/color][color=#0000bb]ToCC[/color][color=#007700].[/color][color=#0000bb]ToString[/color][COLOR=#007700]());
            [/COLOR][color=#0000bb]mailMsg[/color][color=#007700].[/color][color=#0000bb]Bcc[/color][color=#007700].[/color][color=#0000bb]Add[/color][color=#007700]([/color][color=#0000bb]ToBCC[/color][color=#007700].[/color][color=#0000bb]ToString[/color][COLOR=#007700]());
            [/COLOR][color=#0000bb]mailMsg[/color][color=#007700].[/color][color=#0000bb]IsBodyHtml [/color][color=#007700]= [/color][color=#0000bb]IsHTML[/color][COLOR=#007700];
            [/COLOR][color=#0000bb]mailMsg[/color][color=#007700].[/color][color=#0000bb]BodyEncoding [/color][color=#007700]= [/color][color=#0000bb]Encoding[/color][color=#007700].[/color][color=#0000bb]UTF8[/color][COLOR=#007700];
            [/COLOR][color=#0000bb]mailMsg[/color][color=#007700].[/color][color=#0000bb]SubjectEncoding [/color][color=#007700]= [/color][color=#0000bb]mailMsg[/color][color=#007700].[/color][color=#0000bb]BodyEncoding[/color][COLOR=#007700];
            [/COLOR][color=#0000bb]mailMsg[/color][color=#007700].[/color][color=#0000bb]Body [/color][color=#007700]= [/color][color=#0000bb]Body[/color][color=#007700].[/color][color=#0000bb]Trim[/color][COLOR=#007700]();
            if ([/COLOR][color=#0000bb]Attachments [/color][color=#007700]!= [/color][color=#0000bb]null[/color][COLOR=#007700])
            {
                [/COLOR][color=#0000bb]Attachment mailAttachment[/color][COLOR=#007700];
                foreach ([/COLOR][color=#0000bb]Attachment mailAtt in Attachments[/color][COLOR=#007700])
                    [/COLOR][color=#0000bb]mailMsg[/color][color=#007700].[/color][color=#0000bb]Attachments[/color][color=#007700].[/color][color=#0000bb]Add[/color][color=#007700]([/color][color=#0000bb]mailAtt[/color][COLOR=#007700]);
            }

            [/COLOR][color=#0000bb]SmtpClient client [/color][color=#007700]= new [/color][color=#0000bb]SmtpClient[/color][color=#007700]([/color][color=#0000bb]SMTP[/color][color=#007700].[/color][color=#0000bb]Trim[/color][COLOR=#007700]());
            try
            {
                [/COLOR][color=#0000bb]client[/color][color=#007700].[/color][color=#0000bb]Host [/color][color=#007700]= [/color][color=#dd0000]"localhost"[/color][COLOR=#007700];
                [/COLOR][color=#0000bb]client[/color][color=#007700].[/color][color=#0000bb]Send[/color][color=#007700]([/color][color=#0000bb]mailMsg[/color][COLOR=#007700]);
            }
            catch ([/COLOR][color=#0000bb]Exception ex[/color][color=#007700]) { throw [/color][color=#0000bb]ex[/color][COLOR=#007700]; }
            return [/COLOR][color=#0000bb]true[/color][COLOR=#007700];
        }

        public [/COLOR][color=#0000bb]void CheckService[/color][color=#007700]([/color][color=#0000bb]string ServiceName[/color][COLOR=#007700])
        {
            [/COLOR][COLOR=#ff8000]//Ensure the SMTP Service is installed.
            //Loop through all the services on the machine and find the SMTP Service.
            [/COLOR][color=#0000bb]ServiceController[/color][color=#007700][] [/color][color=#0000bb]services [/color][color=#007700]= [/color][color=#0000bb]ServiceController[/color][color=#007700].[/color][color=#0000bb]GetDevices[/color][COLOR=#007700]();
            [/COLOR][color=#0000bb]ServiceController service [/color][color=#007700]= [/color][color=#0000bb]null[/color][COLOR=#007700];
            [/COLOR][color=#0000bb]bool blnHasSmtpService [/color][color=#007700]= [/color][color=#0000bb]false[/color][COLOR=#007700];
            [/COLOR][color=#0000bb]string Message [/color][color=#007700]= [/color][color=#dd0000]""[/color][COLOR=#007700];

            foreach ([/COLOR][color=#0000bb]ServiceController ser in services[/color][COLOR=#007700])
            {
                if ([/COLOR][color=#0000bb]ser[/color][color=#007700].[/color][color=#0000bb]ServiceName[/color][color=#007700].[/color][color=#0000bb]ToLower[/color][color=#007700]() != [/color][color=#0000bb]ServiceName[/color][color=#007700].[/color][color=#0000bb]ToLower[/color][COLOR=#007700]())
                    continue;
                [/COLOR][color=#0000bb]blnHasSmtpService [/color][color=#007700]= [/color][color=#0000bb]true[/color][COLOR=#007700];
                break;
            }
            if (![/COLOR][color=#0000bb]blnHasSmtpService[/color][color=#007700]) throw new [/color][color=#0000bb]Exception[/color][color=#007700]( [/color][color=#dd0000]"- There're no SMTP service on your system."[/color][COLOR=#007700]);

            if ([/COLOR][color=#0000bb]service[/color][color=#007700].[/color][color=#0000bb]Status [/color][color=#007700]!= [/color][color=#0000bb]ServiceControllerStatus[/color][color=#007700].[/color][color=#0000bb]Running[/color][COLOR=#007700])
            {
                try
                {
                    [/COLOR][color=#0000bb]service[/color][color=#007700].[/color][color=#0000bb]Start[/color][COLOR=#007700]();
                }
                catch ([/COLOR][color=#0000bb]Exception ex[/color][color=#007700]) { throw [/color][color=#0000bb]ex[/color][COLOR=#007700]; }
            }
        }
    }
}  
[/COLOR][color=#0000bb][/color][/COLOR]

ولعمل الكود يجب ان تكون خدمة SMTP مشغلة ومفعلة في جهازك وعندها يمكنك إرسال البريد باتدعاء الدالة: SendEmail
}}}}
تم الشكر بواسطة:


المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  مثال بسيط لتغيير طريقة استخدام اداة العنان Label مع الشرح RaggiTech 0 804 20-10-12, 11:50 AM
آخر رد: RaggiTech
  طريقة تحويل صفحة في ملف Excel إلى DataSet RaggiTech 0 809 17-10-12, 10:09 PM
آخر رد: RaggiTech
  طريقة تحويل الصور الملونه الي صور ابيض واسود RaggiTech 0 868 17-10-12, 08:43 PM
آخر رد: RaggiTech
  طريقة استخدام مكتبة internet download manager في برنامجك !! RaggiTech 0 493 17-10-12, 08:07 PM
آخر رد: RaggiTech
  كود يقوم بحماية برنامجك حيث يعمل عدد من المرات (تحددها بنفسك) ثم يتوقف نهائيا عن العم RaggiTech 0 337 17-10-12, 05:16 PM
آخر رد: RaggiTech

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم