用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)
新闻热点
疑难解答
图片精选