Form1.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:5k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Web.Mail;
  8. namespace SendEmail
  9. {
  10. /// <summary>
  11. /// Summary description for Form1.
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private System.Windows.Forms.TextBox textBox1;
  16. private System.Windows.Forms.TextBox textBox2;
  17. private System.Windows.Forms.RichTextBox richTextBox1;
  18. private System.Windows.Forms.Button button1;
  19. private System.Windows.Forms.Label label1;
  20. private System.Windows.Forms.Label label2;
  21. private System.Windows.Forms.Label label3;
  22. /// <summary>
  23. /// Required designer variable.
  24. /// </summary>
  25. private System.ComponentModel.Container components = null;
  26. public Form1()
  27. {
  28. //
  29. // Required for Windows Form Designer support
  30. //
  31. InitializeComponent();
  32. //
  33. // TODO: Add any constructor code after InitializeComponent call
  34. //
  35. }
  36. /// <summary>
  37. /// Clean up any resources being used.
  38. /// </summary>
  39. protected override void Dispose( bool disposing )
  40. {
  41. if( disposing )
  42. {
  43. if (components != null) 
  44. {
  45. components.Dispose();
  46. }
  47. }
  48. base.Dispose( disposing );
  49. }
  50. #region Windows Form Designer generated code
  51. /// <summary>
  52. /// Required method for Designer support - do not modify
  53. /// the contents of this method with the code editor.
  54. /// </summary>
  55. private void InitializeComponent()
  56. {
  57. this.textBox1 = new System.Windows.Forms.TextBox();
  58. this.textBox2 = new System.Windows.Forms.TextBox();
  59. this.richTextBox1 = new System.Windows.Forms.RichTextBox();
  60. this.button1 = new System.Windows.Forms.Button();
  61. this.label1 = new System.Windows.Forms.Label();
  62. this.label2 = new System.Windows.Forms.Label();
  63. this.label3 = new System.Windows.Forms.Label();
  64. this.SuspendLayout();
  65. // 
  66. // textBox1
  67. // 
  68. this.textBox1.Location = new System.Drawing.Point(168, 24);
  69. this.textBox1.Name = "textBox1";
  70. this.textBox1.Size = new System.Drawing.Size(216, 20);
  71. this.textBox1.TabIndex = 0;
  72. this.textBox1.Text = "someone@somewhere.com";
  73. // 
  74. // textBox2
  75. // 
  76. this.textBox2.Location = new System.Drawing.Point(168, 64);
  77. this.textBox2.Name = "textBox2";
  78. this.textBox2.Size = new System.Drawing.Size(216, 20);
  79. this.textBox2.TabIndex = 1;
  80. this.textBox2.Text = "Subject";
  81. // 
  82. // richTextBox1
  83. // 
  84. this.richTextBox1.Location = new System.Drawing.Point(16, 128);
  85. this.richTextBox1.Name = "richTextBox1";
  86. this.richTextBox1.Size = new System.Drawing.Size(368, 208);
  87. this.richTextBox1.TabIndex = 2;
  88. this.richTextBox1.Text = "From the C# Email Test Program";
  89. // 
  90. // button1
  91. // 
  92. this.button1.Location = new System.Drawing.Point(40, 344);
  93. this.button1.Name = "button1";
  94. this.button1.Size = new System.Drawing.Size(312, 40);
  95. this.button1.TabIndex = 3;
  96. this.button1.Text = "Send Mail";
  97. this.button1.Click += new System.EventHandler(this.button1_Click);
  98. // 
  99. // label1
  100. // 
  101. this.label1.Location = new System.Drawing.Point(48, 24);
  102. this.label1.Name = "label1";
  103. this.label1.TabIndex = 4;
  104. this.label1.Text = "To:";
  105. this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  106. // 
  107. // label2
  108. // 
  109. this.label2.Location = new System.Drawing.Point(56, 64);
  110. this.label2.Name = "label2";
  111. this.label2.TabIndex = 5;
  112. this.label2.Text = "Subject:";
  113. this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  114. // 
  115. // label3
  116. // 
  117. this.label3.Location = new System.Drawing.Point(16, 96);
  118. this.label3.Name = "label3";
  119. this.label3.TabIndex = 6;
  120. this.label3.Text = "Body:";
  121. this.label3.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
  122. // 
  123. // Form1
  124. // 
  125. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  126. this.ClientSize = new System.Drawing.Size(400, 389);
  127. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  128.   this.label3,
  129.   this.label2,
  130.   this.label1,
  131.   this.button1,
  132.   this.richTextBox1,
  133.   this.textBox2,
  134.   this.textBox1});
  135. this.Name = "Form1";
  136. this.Text = "SendEmail";
  137. this.ResumeLayout(false);
  138. }
  139. #endregion
  140. /// <summary>
  141. /// The main entry point for the application.
  142. /// </summary>
  143. [STAThread]
  144. static void Main() 
  145. {
  146. Application.Run(new Form1());
  147. }
  148. private void button1_Click(object sender, System.EventArgs e)
  149. {
  150. /* This example illstrates sending email from C# with the MailMessage class
  151.  * 
  152.  * The properties of your email are set from the properties of the MailMessage object
  153.  * (To, Subject, Body, From and many more)
  154.  * 
  155.  * 
  156.  * The static Send() method of the SmtpMail class sends the message
  157.  * 
  158.  * */
  159.              
  160.  
  161. MailMessage theMessage = new MailMessage();
  162. // Insert the from address here
  163. theMessage.From = @"somebody@somewhere.com";
  164. // Set the message properties from the form
  165. theMessage.To = this.textBox1.Text;
  166. theMessage.Subject = this.textBox2.Text;
  167. theMessage.Body = this.richTextBox1.Text;
  168. // Insert your mail server here
  169. SmtpMail.SmtpServer = "MAILSERVER";
  170. // Send the message!
  171. SmtpMail.Send(theMessage);
  172. }
  173. }
  174. }