1 //This code shows how to send email in asp.net using C#. 2 using System; 3 using System.Drawing; 4 using System.Collections; 5 using System.ComponentModel; 6 using System.Windows.Forms; 7 using System.Data; 8 using System.Web.Mail;//don't forget to include this namespace 9 10 namespace Emailtest 11 { 12 /// <summary> 13 /// Summary description for Form1. 14 /// </summary> 15 public class Form1 : System.Windows.Forms.form 16 { 17 private System.Windows.Forms.TextBox textTo; 18 private System.Windows.Forms.TextBox textSub; 19 private System.Windows.Forms.RichTextBox richText; 20 private System.Windows.Forms.Button btnSend; 21 private System.Windows.Forms.TextBox textFrom; 22 /// <summary> 23 /// Required designer variable. 24 /// </summary> 25 private System.ComponentModel.Container components = null; 26 27 public Form1() 28 { 29 // 30 // Required for Windows Form Designer support 31 // 32 InitializeComponent(); 33 34 // 35 // TODO: Add any constructor code after InitializeComponent call 36 // 37 } 38 39 /// <summary> 40 /// Clean up any resources being used. 41 /// </summary> 42 protected override void Dispose( bool disposing ) 43 { 44 if( disposing ) 45 { 46 if (components != null) 47 { 48 components.Dispose(); 49 } 50 } 51 base.Dispose( disposing ); 52 } 53 54 /// <summary> 55 /// The main entry point for the application. 56 /// </summary> 57 [STAThread] 58 static void Main() 59 { 60 Application.Run(new Form1()); 61 } 62 63 64 65 private void btnSend_Click(object sender, System.EventArgs e) 66 { 67 try 68 { 69 //create mail message object 70 MailMessage Mailer = new MailMessage(); 71 Mailer.From = this.textFrom.Text; 72 Mailer.To = this.textTo.Text; 73 Mailer.Subject = this.textSub.Text; 74 Mailer.Body = this.richText.Text; 75 76 //add attachment to send 77 MailAttachment att = new MailAttachment(@"c:\mail\test.txt"); 78 Mailer.Attachments.Add(att); 79 80 //set the type of format either HTML or Text 81 Mailer.BodyFormat = MailFormat.Text; 82 83 //the ip address of your SMTP server 84 SmtpMail.SmtpServer = "127.0.0.1"; 85 SmtpMail.Send(Mailer); 86 87 } 88 catch(Exception e1) 89 { 90 91 MessageBox.Show(e1.Message,"Error!",MessageBoxButtons.OK,MessageBoxIcon.Error); 92 93 } 94 95 } 96 } 97 }