C#使用SMTP发送Mail

     下面将介绍使用.Net中System.Net.Mail类(http://msdn.microsoft.com/zh-cn/library/system.net.mail.smtpclient(VS.80).aspx)实现一个发送邮件的程序,在此将其做成一个组件(方便以后其他project使用)。在此需要说明一点;如果您使用的是第三方提供的邮箱,请确认您的邮箱已经开通SMTP服务

     尤其在使用网易的邮箱时请确认您的帐号是否是在06年以前注册的:以下是网易官方解释(http://help.163.com/09/0219/09/52GO7AKR007536NI.html):
  目前免费邮箱新注册的用户不支持直接开通smtp、pop3的服务,之前已开通客户端功能的老用户不受影响。如果需要使用该功能,您可开通增值服务邮箱伴侣或随身邮 ,即可同时获取poo功能。或者您可以选择 VIP邮箱。另外我们也会陆续通过活动、“邮箱会员”等方式向有需要的用户提供该项服务,敬请关注。感谢您使用我们的产品!

     如果是在找不到SMTP服务器,就是用Exchange邮箱提供的SMTP服务器(经常使用OutLook的朋友很熟悉),在没招的话在本地计算机上安装SMTP服务。

ContractedBlock.gif ExpandedBlockStart.gif Code
 1using System;
 2using System.Collections.Specialized;
 3using System.Text;
 4using System.Net;
 5using System.Net.Mail;
 6
 7namespace MailHelper
 8ExpandedBlockStart.gifContractedBlock.gif{
 9    public class MailOper
10ExpandedSubBlockStart.gifContractedSubBlock.gif    {
11ContractedSubBlock.gifExpandedSubBlockStart.gif        Field#region Field
12        private string _host;//SMTP服务器
13        private int _port;//SMTP端口号,通常为25
14        private string _account;//登录服务器的帐号
15        private string _pwd;//登录服务器密码
16        public static SmtpClient sc;//详见 http://msdn.microsoft.com/zh-cn/library/system.net.mail.smtpclient(VS.80).aspx
17        #endregion

18
19ContractedSubBlock.gifExpandedSubBlockStart.gif        Constraction#region Constraction
20ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
21        /// Build MailOper By Config
22        /// </summary>
23        /// <param name="apps"></param>

24        public MailOper(NameValueCollection apps)
25ExpandedSubBlockStart.gifContractedSubBlock.gif        {
26            _host = apps["SmtpServer"];
27            _port = int.TryParse(apps["SmtpPort"], out _port) ? _port : 25;
28            _account = apps["SmtpUserName"];
29            _pwd = apps["SmtpUserPwd"];
30            InitOper();
31        }

32
33        public MailOper(string host, int port, string account, string pwd)
34ExpandedSubBlockStart.gifContractedSubBlock.gif        {
35            _host = host;
36            _port = port;
37            _account = account;
38            _pwd = pwd;
39            InitOper();
40        }

41        #endregion

42
43ContractedSubBlock.gifExpandedSubBlockStart.gif        Methord#region Methord
44        private void InitOper()
45ExpandedSubBlockStart.gifContractedSubBlock.gif        {
46            if (null == sc)
47ExpandedSubBlockStart.gifContractedSubBlock.gif            {
48                sc = new SmtpClient(_host, _port);
49                sc.Credentials = new NetworkCredential(_account, _pwd);
50                sc.DeliveryMethod = SmtpDeliveryMethod.Network;                
51            }

52        }

53ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
54        /// 发送Mail
55        /// </summary>
56        /// <param name="mailTo"></param>
57        /// <param name="title"></param>
58        /// <param name="body"></param>
59        /// <param name="isHTML"></param>

60        public void SendMail(string mailTo, string mailfrom, string title, string body, bool isHTML)
61ExpandedSubBlockStart.gifContractedSubBlock.gif        {
62            MailMessage msg = new MailMessage(mailfrom, mailTo, title, body);
63            msg.BodyEncoding = Encoding.UTF8;
64            msg.IsBodyHtml = isHTML;
65            sc.Send(msg);
66        }

67        #endregion

68    }

69}

70

 

以下是测试程序(注意引用以上Dll)

 

ContractedBlock.gif ExpandedBlockStart.gif Code
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using MailHelper;
 5
 6namespace ConsoleApplication2
 7ExpandedBlockStart.gifContractedBlock.gif{
 8    class Program
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    {
10        static void Main(string[] args)
11ExpandedSubBlockStart.gifContractedSubBlock.gif        {
12            MailOper mob = new MailOper("mail.xx.com"25"xx@xx.com.cn""xx-");//注意修改成您的SMTP
13            mob.SendMail("xx@hotmail.com""xx@xx.com.cn""This is a test mail from MailOB!""content"false);
14            Console.Read();
15        }

16    }

17}

18

转载于:https://www.cnblogs.com/netwenchao/archive/2009/05/21/1486346.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值