用system.web.mail發送郵件,適用于.net1.1,.net2.0請用system.net.mail
先引用system.web
1,發送簡單郵件
[ c# ] 
mailmessage mail = new mailmessage();mail.to = "[email protected]";mail.from = "[email protected]";mail.subject = "this is a test email.";mail.body = "this is my test email body";smtpmail.smtpserver = "localhost";  //your real server goes heresmtpmail.send( mail );
[ vb.net ] 
dim mail as new mailmessage()mail.to = "[email protected]"mail.from = "[email protected]"mail.subject = "this is a test email."mail.body = "this is my test email body"smtpmail.smtpserver = "localhost" 'your real server goes heresmtpmail.send(mail)這里的smtpserver只能是那些不需要驗證的smtp服務器,像126,sina,yahoo等等的郵箱,都需要驗證,所以不能用。用這些郵箱發信后面會講到2,發送html郵件[ c# ] mailmessage mail = new mailmessage();mail.to = "[email protected]";mail.from = "[email protected]";mail.subject = "this is a test email.";mail.bodyformat = mailformat.html;mail.body = "this is my test email body.<br><b>this part is in bold</b>";smtpmail.smtpserver = "localhost";  //your real server goes heresmtpmail.send( mail );
 [ vb.net ] dim mail as new mailmessage()mail.to = "[email protected]"mail.from = "[email protected]"mail.subject = "this is a test email."mail.bodyformat = mailformat.htmlmail.body = "this is my test email body.<br><b>this part is in bold</b>"smtpmail.smtpserver = "localhost" 'your real server goes heresmtpmail.send(mail)
3,發送附件
[ c# ] 
mailmessage mail = new mailmessage();mail.to = "[email protected]";mail.from = "[email protected]";mail.subject = "this is a test email.";mail.body = "this is my test email body.";mailattachment attachment = new mailattachment( server.mappath( "test.txt" ) ); //create the attachmentmail.attachments.add( attachment ); //add the attachmentsmtpmail.smtpserver = "localhost";  //your real server goes heresmtpmail.send( mail );
[ vb.net ] 
dim mail as new mailmessage()mail.to = "[email protected]"mail.from = "[email protected]"mail.subject = "this is a test email."mail.body = "this is my test email body."dim attachment as new mailattachment(server.mappath("test.txt")) 'create the attachmentmail.attachments.add(attachment) 'add the attachmentsmtpmail.smtpserver = "localhost" 'your real server goes heresmtpmail.send(mail)
4,修改發件人和收件人的名稱比如發件人的地址是[email protected],我們用outlook收到信,from一欄里將直接顯示[email protected].能不能在from一欄里顯示友好一點的名字呢?比如顯示tony gong方法如下:[ c# ] mailmessage mail = new mailmessage();mail.to = "/"john/" <";mail.from'>[email protected]>";mail.from = "/"tony gong/" <";mail.subject'>[email protected]>";mail.subject = "this is a test email.";mail.body = "this is my test email body.";smtpmail.smtpserver = "localhost";  //your real server goes heresmtpmail.send( mail );
 [ vb.net ] dim mail as new mailmessage()mail.to = """john"" <"mail.from'>[email protected]>"mail.from = """tony gong"" <"mail.subject'>[email protected]>"mail.subject = "this is a test email."mail.body = "this is my test email body."smtpmail.smtpserver = "localhost" 'your real server goes heresmtpmail.send(mail)
5,發送給多人
[ c# ] 
mailmessage mail = new mailmessage();mail.to = "[email protected];[email protected];[email protected]";mail.from = "[email protected]";mail.subject = "this is a test email.";mail.body = "this is my test email body.";smtpmail.smtpserver = "localhost";  //your real server goes heresmtpmail.send( mail );
[ vb.net ] 
dim mail as new mailmessage()mail.to = "[email protected];[email protected];[email protected]"mail.from = "[email protected]"mail.subject = "this is a test email."mail.body = "this is my test email body."smtpmail.smtpserver = "localhost" 'your real server goes heresmtpmail.send(mail)
6,用需要smtp驗證的郵箱發信
現在為了防止垃圾郵件,絕大部分smtp服務器需要驗證了
發信方法如下:
[ c# ] 
 mailmessage mail = new mailmessage(); mail.to = "[email protected]"; mail.from = "[email protected]"; mail.subject = "this is a test email."; mail.body = "some text goes here"; mail.fields.add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication mail.fields.add("http://schemas.microsoft.com/cdo/configuration/sendusername", "abc"); //set your username here mail.fields.add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "your password"); //set your password here smtpmail.smtpserver = "smtp.126.com";  //your real server goes here smtpmail.send( mail );
[ vb.net ] 
   dim mail as new mailmessage()   mail.to = "[email protected]"   mail.from = "[email protected]"   mail.subject = "this is a test email."   mail.body = "some text goes here"   mail.fields.add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1") 'basic authentication   mail.fields.add("http://schemas.microsoft.com/cdo/configuration/sendusername", "abc") 'set your username here   mail.fields.add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "your password") 'set your password here   smtpmail.smtpserver = "smtp.126.com" 'your real server goes here   smtpmail.send(mail)7,修改smtp服務器的端口,以及使用ssl加密大部分smtp服務器的端口是25,但有些卻不是同時,絕大部分smtp服務器不需要ssl登陸,有些卻需要比如gmail,smtp端口是:465,同時支持ssl代碼如下:[ c# ]  mailmessage mail = new mailmessage(); mail.to = "[email protected]"; mail.from = "[email protected]"; mail.subject = "this is a test email."; mail.body = "some text goes here"; mail.fields.add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication mail.fields.add("http://schemas.microsoft.com/cdo/configuration/sendusername", "abc"); //set your username here mail.fields.add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "your password"); //set your password here  mail.fields.add("http://schemas.microsoft.com/cdo/configuration/smtpserverport",465); mail.fields.add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
 smtpmail.smtpserver = "smtp.126.com";  //your real server goes here smtpmail.send( mail ); [ vb.net ]    dim mail as new mailmessage()   mail.to = "[email protected]"   mail.from = "[email protected]"   mail.subject = "this is a test email."   mail.body = "some text goes here"   mail.fields.add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1") 'basic authentication   mail.fields.add("http://schemas.microsoft.com/cdo/configuration/sendusername", "abc") 'set your username here   mail.fields.add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "your password") 'set your password here      mail.fields.add("http://schemas.microsoft.com/cdo/configuration/smtpserverport",465)   mail.fields.add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true")   smtpmail.smtpserver = "smtp.126.com" 'your real server goes here   smtpmail.send(mail)
新聞熱點
疑難解答
圖片精選