在51aspx.com下了个发邮件的,不过不怎么好用,也没有详细的说明,也没有发送成功,
在网上找了一个类说明,结合了一下,重新写了,只要再改动一下,就很实用了。
引用三个类:
- usingSystem.Net.Mail;
- usingSystem.Net;
- usingSystem.Net.Mime;
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Web;
- usingSystem.Web.UI;
- usingSystem.Web.UI.WebControls;
- usingSystem.Net.Mail;
- usingSystem.Net;
- usingSystem.Net.Mime;
-
- publicpartialclasstest_sendMail:System.Web.UI.Page
- {
- protectedvoidPage_Load(objectsender,EventArgse)
- {
-
- }
- protectedvoidButton1_Click(objectsender,EventArgse)
- {
- MailMessagemailMsg=newMailMessage();
- mailMsg.From=newMailAddress("*****@163.com");//发送邮件用户名
- mailMsg.To.Add("*******@163.com");//接收邮件用户
- mailMsg.Subject="邮件主题";
- mailMsg.Body="邮件主体内容ffffffffffffffffffff";
- mailMsg.BodyEncoding=System.Text.Encoding.UTF8;
- mailMsg.IsBodyHtml=false;
- mailMsg.Priority=MailPriority.High;
-
- stringfile=Request.PhysicalApplicationPath+"test//data.xls"; //附件的地址
- //Createthefileattachmentforthise-mailmessage.
- Attachmentdata=newAttachment(file,MediaTypeNames.Application.Octet);
- //Addtimestampinformationforthefile.
- ContentDispositiondisposition=data.ContentDisposition;
- disposition.CreationDate=System.IO.File.GetCreationTime(file);
- disposition.ModificationDate=System.IO.File.GetLastWriteTime(file);
- disposition.ReadDate=System.IO.File.GetLastAccessTime(file);
- mailMsg.Attachments.Add(data);
-
- SmtpClientsmtp=newSmtpClient();
- //提供身份验证的用户名和密码
- //网易邮件用户可能为:usernamepassword
- //Gmail用户可能为:username@gmail.compassword
- smtp.Credentials=newNetworkCredential("userName","userpwd");
- smtp.Port=25;//Gmail使用465和587端口
- smtp.Host="smtp.163.com";//如smtp.163.com,smtp.gmail.com
- smtp.EnableSsl=false;//如果使用GMail,则需要设置为true
- //smtp.SendCompleted+=newSendCompletedEventHandler(SendMailCompleted);
- smtp.SendAsync(mailMsg,mailMsg);
- }
- }
163测试成功,Gmail未测试。。。。