asp.net 带附件发送邮件

在51aspx.com下了个发邮件的,不过不怎么好用,也没有详细的说明,也没有发送成功,

在网上找了一个类说明,结合了一下,重新写了,只要再改动一下,就很实用了。

引用三个类:

  1. usingSystem.Net.Mail;
  2. usingSystem.Net;
  3. usingSystem.Net.Mime;
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Web;
  5. usingSystem.Web.UI;
  6. usingSystem.Web.UI.WebControls;
  7. usingSystem.Net.Mail;
  8. usingSystem.Net;
  9. usingSystem.Net.Mime;

  10. publicpartialclasstest_sendMail:System.Web.UI.Page
  11. {
  12. protectedvoidPage_Load(objectsender,EventArgse)
  13. {

  14. }
  15. protectedvoidButton1_Click(objectsender,EventArgse)
  16. {
  17. MailMessagemailMsg=newMailMessage();
  18. mailMsg.From=newMailAddress("*****@163.com");//发送邮件用户名
  19. mailMsg.To.Add("*******@163.com");//接收邮件用户
  20. mailMsg.Subject="邮件主题";
  21. mailMsg.Body="邮件主体内容ffffffffffffffffffff";
  22. mailMsg.BodyEncoding=System.Text.Encoding.UTF8;
  23. mailMsg.IsBodyHtml=false;
  24. mailMsg.Priority=MailPriority.High;

  25. stringfile=Request.PhysicalApplicationPath+"test//data.xls"; //附件的地址
  26. //Createthefileattachmentforthise-mailmessage.
  27. Attachmentdata=newAttachment(file,MediaTypeNames.Application.Octet);
  28. //Addtimestampinformationforthefile.
  29. ContentDispositiondisposition=data.ContentDisposition;
  30. disposition.CreationDate=System.IO.File.GetCreationTime(file);
  31. disposition.ModificationDate=System.IO.File.GetLastWriteTime(file);
  32. disposition.ReadDate=System.IO.File.GetLastAccessTime(file);
  33. mailMsg.Attachments.Add(data);

  34. SmtpClientsmtp=newSmtpClient();
  35. //提供身份验证的用户名和密码
  36. //网易邮件用户可能为:usernamepassword
  37. //Gmail用户可能为:username@gmail.compassword
  38. smtp.Credentials=newNetworkCredential("userName","userpwd");
  39. smtp.Port=25;//Gmail使用465和587端口
  40. smtp.Host="smtp.163.com";//如smtp.163.com,smtp.gmail.com
  41. smtp.EnableSsl=false;//如果使用GMail,则需要设置为true
  42. //smtp.SendCompleted+=newSendCompletedEventHandler(SendMailCompleted);
  43. smtp.SendAsync(mailMsg,mailMsg);
  44. }
  45. }

163测试成功,Gmail未测试。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值