国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

.net發送郵件的一些技巧

2024-07-10 13:08:59
字體:
來源:轉載
供稿:網友

用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)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 滨州市| 宁安市| 蓝山县| 繁昌县| 广水市| 建阳市| 光泽县| 龙游县| 北川| 洛宁县| 衡水市| 天津市| 涟水县| 沙田区| 铜陵市| 紫云| 海安县| 玉树县| 安多县| 涟源市| 阜新市| 宜黄县| 石柱| 孝义市| 惠来县| 德昌县| 静安区| 佛山市| 青岛市| 五大连池市| 吐鲁番市| 武山县| 钟山县| 扶余县| 洪雅县| 武清区| 永寿县| 斗六市| 新竹市| 正安县| 崇仁县|