使用.net(C#)发送邮件学习手册(带成功案例)

时间:2023-03-09 16:18:22
使用.net(C#)发送邮件学习手册(带成功案例)

使用.net(C#)发送邮件学习手册(带成功案例) 
1.了解发送邮件的三种方式 
2.实例介绍使用client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis 
3.如何设定本机IIS的SMTP服务器 
1.了解发送邮件的三种方式 
第一:client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
//通過遠程SMTP服務器傳送該郵件,這裡的network表示你要使用的远程SMTP服務器。 
第二:client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis; 
//通過本機SMTP服務器傳送該郵件,这里的PickupDirectoryFromIis表示你的邮件会通过本机IIS的SMTP服務器传送你的邮件。所以如果使用该项一定要设定在SMTP服務器上设定好你要转到的服务器的地址。下文会详细介绍。 
第三:client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory; 
//表示电子邮件会被复制到System.Net.Mail.SmtpDeliveryMethod.PickupDirectorylocation所指定的目录中。以便有其他程序来执行发送该邮件。

2.实例介绍使用client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis传送邮件。
(1)mail.aspx的代码如下(直接粘贴):

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="mail.aspx.cs" Inherits="mail" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5. <title>mail to users</title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
  11. </div>
  12. </form>
  13. </body>
  14. </html>

(2)mail.aspx.cs代码如下: 
注意:一般公司 都是代理上网的。所以如果使用该项。只能发送内部网的邮件。 
但是并不是说该项不能发送外部网的邮件。而是代理*的原因。

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Net;
  12. using System.Net.Mail;
  13. public partial class mail : System.Web.UI.Page
  14. {
  15. protected void Page_Load(object sender, EventArgs e)
  16. {
  17. //SendMail(发件者, 收件者, 主旨, 内容, 主机,发件者昵称, 密码 ,附件)
  18. SendMail("sunjie@yyhj.com.cn", "lilei.luo@yyhj.com.cn", "主旨", "邮件内容测试", "exhj.yyhj.com.cn", "孙节", "yyhj", "");
  19. }
  20. public void SendMail(string send, string recieve, string subject, string mailbody, string host, string uname, string pwd, string strFileName)
  21. {
  22. //生成一个   使用SMTP发送邮件的客户端对象
  23. System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
  24. //生成一个主机IP
  25. //client.Port = 25; //587, 465, 995
  26. client.Host = host;
  27. //表示不以当前登录用户的默认凭据进行身份验证
  28. client.UseDefaultCredentials =true ;
  29. //包含用户名和密码
  30. if (uname != "")
  31. {
  32. client.Credentials = new System.Net.NetworkCredential(uname, pwd);
  33. }
  34. //指定如何发送电子邮件。
  35. client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis;
  36. //通过本机SMTP服务器传送该邮件,
  37. //其实使用该项的话就可以随意设定“主机,发件者昵称, 密码”,因为你的IIS服务器已经设定好了。而且公司内部发邮件是不需要验证的。
  38. System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
  39. message.To.Add(recieve);
  40. message.From = new System.Net.Mail.MailAddress(send, uname, System.Text.Encoding.UTF8);
  41. message.Subject = subject;
  42. message.Body = mailbody;
  43. //定义邮件正文,主题的编码方式
  44. message.BodyEncoding = System.Text.Encoding.GetEncoding("UTF-8");
  45. message.SubjectEncoding = System.Text.Encoding.GetEncoding("UTF-8");
  46. //获取或设置一个值,该值指示电子邮件正文是否为   HTML。
  47. message.IsBodyHtml = false;
  48. //指定邮件优先级
  49. message.Priority = System.Net.Mail.MailPriority.High;
  50. //添加附件
  51. //System.Net.Mail.Attachment data = new Attachment(@"E:\9527\tubu\PA260445.JPG", System.Net.Mime.MediaTypeNames.Application.Octet);
  52. if (strFileName != "" && strFileName != null)
  53. {
  54. Attachment data = new Attachment(strFileName);
  55. message.Attachments.Add(data);
  56. }
  57. try
  58. {
  59. //发送
  60. client.Send(message);
  61. Label1.Text = "发送成功!";
  62. }
  63. catch (System.Net.Mail.SmtpException ex)
  64. {
  65. Label1.Text ="发送失败:"+ ex.Message;
  66. }
  67. }
  68. }

2.介绍使用client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network传送邮件。 
使用该项的话。你的电脑首先必须是直接链接外网的。 
那就直接把mail.aspx.cs里的client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis;换成client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
然后要设定的就是 
//SendMail(发件者, 收件者, 主旨, 内容, 主机,发件者昵称, 密码 ,附件) 
SendMail("loeley@gmail.com", "sy4l@163.com", "主旨", "12.37郵件內容", "smtp.163.com", "loeley", "81859505", ""); 
转自:http://hi.baidu.com/lslyl/blog/item/ba67366ef4202ddd80cb4afa.html