NET-发送邮件类
        
        net发送邮件类
    

 代码
代码  1 public class MailSender
    public class MailSender
2
 
     {
{
3 public static void Send(string server, string sender, string recipient, string subject,
        public static void Send(string server, string sender, string recipient, string subject,
4 string body, bool isBodyHtml, Encoding encoding, bool isAuthentication, params string[] files)
    string body, bool isBodyHtml, Encoding encoding, bool isAuthentication, params string[] files)
5
 
         {
{
6 SmtpClient smtpClient = new SmtpClient(server);
            SmtpClient smtpClient = new SmtpClient(server);
7 MailMessage message = new MailMessage(sender, recipient);
            MailMessage message = new MailMessage(sender, recipient);
8 message.IsBodyHtml = isBodyHtml;
            message.IsBodyHtml = isBodyHtml;
9
10 message.SubjectEncoding = encoding;
            message.SubjectEncoding = encoding;
11 message.BodyEncoding = encoding;
            message.BodyEncoding = encoding;
12
13 message.Subject = subject;
            message.Subject = subject;
14 message.Body = body;
            message.Body = body;
15
16 message.Attachments.Clear();
            message.Attachments.Clear();
17 if (files != null && files.Length != 0)
            if (files != null && files.Length != 0)
18
 
             {
{
19 for (int i = 0; i < files.Length; ++i)
                for (int i = 0; i < files.Length; ++i)
20
 
                 {
{
21 Attachment attach = new Attachment(files[i]);
                    Attachment attach = new Attachment(files[i]);
22 message.Attachments.Add(attach);
                    message.Attachments.Add(attach);
23 }
                }
24 }
            }
25
26 if (isAuthentication == true)
            if (isAuthentication == true)
27
 
             {
{
28 smtpClient.Credentials = new NetworkCredential(SmtpConfig.Create().SmtpSetting.User,
                smtpClient.Credentials = new NetworkCredential(SmtpConfig.Create().SmtpSetting.User,
29 SmtpConfig.Create().SmtpSetting.Password);
                    SmtpConfig.Create().SmtpSetting.Password);
30 }
            }
31 smtpClient.Send(message);
            smtpClient.Send(message);
32
33
34 }
        }
35
36 public static void Send(string recipient, string subject, string body)
        public static void Send(string recipient, string subject, string body)
37
 
         {
{
38 Send(SmtpConfig.Create().SmtpSetting.Server, SmtpConfig.Create().SmtpSetting.Sender, recipient, subject, body, true, Encoding.Default, true, null);
            Send(SmtpConfig.Create().SmtpSetting.Server, SmtpConfig.Create().SmtpSetting.Sender, recipient, subject, body, true, Encoding.Default, true, null);
39 }
        }
40
41 public static void Send(string Recipient, string Sender, string Subject, string Body)
        public static void Send(string Recipient, string Sender, string Subject, string Body)
42
 
         {
{
43 Send(SmtpConfig.Create().SmtpSetting.Server, Sender, Recipient, Subject, Body, true, Encoding.UTF8, true, null);
            Send(SmtpConfig.Create().SmtpSetting.Server, Sender, Recipient, Subject, Body, true, Encoding.UTF8, true, null);
44 }
        }
45
46 static readonly string smtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"];
        static readonly string smtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"];
47 static readonly string userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
        static readonly string userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
48 static readonly string pwd = System.Configuration.ConfigurationManager.AppSettings["Pwd"];
        static readonly string pwd = System.Configuration.ConfigurationManager.AppSettings["Pwd"];
49 static readonly int smtpPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpPort"]);
        static readonly int smtpPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpPort"]);
50 static readonly string authorName = System.Configuration.ConfigurationManager.AppSettings["AuthorName"];
        static readonly string authorName = System.Configuration.ConfigurationManager.AppSettings["AuthorName"];
51 static readonly string to = System.Configuration.ConfigurationManager.AppSettings["To"];
        static readonly string to = System.Configuration.ConfigurationManager.AppSettings["To"];
52
53
54 public void Send(string subject, string body)
        public void Send(string subject, string body)
55
 
         {
{
56
57 List<string> toList = StringPlus.GetSubStringList(StringPlus.ToDBC(to), ',');
            List<string> toList = StringPlus.GetSubStringList(StringPlus.ToDBC(to), ',');
58 OpenSmtp.Mail.Smtp smtp = new OpenSmtp.Mail.Smtp(smtpServer, userName, pwd, smtpPort);
            OpenSmtp.Mail.Smtp smtp = new OpenSmtp.Mail.Smtp(smtpServer, userName, pwd, smtpPort);
59 foreach (string s in toList)
            foreach (string s in toList)
60
 
             {
{
61 OpenSmtp.Mail.MailMessage msg = new OpenSmtp.Mail.MailMessage();
                OpenSmtp.Mail.MailMessage msg = new OpenSmtp.Mail.MailMessage();
62 msg.From = new OpenSmtp.Mail.EmailAddress(userName, authorName);
                msg.From = new OpenSmtp.Mail.EmailAddress(userName, authorName);
63 
                
64 msg.AddRecipient(s, OpenSmtp.Mail.AddressType.To);
                msg.AddRecipient(s, OpenSmtp.Mail.AddressType.To);
65
66 //设置邮件正文,并指定格式为 html 格式
                //设置邮件正文,并指定格式为 html 格式
67 msg.HtmlBody = body;
                msg.HtmlBody = body;
68 //设置邮件标题
                //设置邮件标题
69 msg.Subject = subject;
                msg.Subject = subject;
70 //指定邮件正文的编码
                //指定邮件正文的编码
71 msg.Charset = "gb2312";
                msg.Charset = "gb2312";
72 //发送邮件
                //发送邮件
73 smtp.SendMail(msg);
                smtp.SendMail(msg);
74 }
            }
75 }
        }
76 }
    }
77
78 public class SmtpSetting
    public class SmtpSetting
79
 
     {
{
80 private string _server;
        private string _server;
81
82 public string Server
        public string Server
83
 
         {
{
84
 get
            get  { return _server; }
{ return _server; }
85
 set
            set  { _server = value; }
{ _server = value; }
86 }
        }
87 private bool _authentication;
        private bool _authentication;
88
89 public bool Authentication
        public bool Authentication
90
 
         {
{
91
 get
            get  { return _authentication; }
{ return _authentication; }
92
 set
            set  { _authentication = value; }
{ _authentication = value; }
93 }
        }
94 private string _user;
        private string _user;
95
96 public string User
        public string User
97
 
         {
{
98
 get
            get  { return _user; }
{ return _user; }
99
 set
            set  { _user = value; }
{ _user = value; }
100 }
        }
101 private string _sender;
        private string _sender;
102
103 public string Sender
        public string Sender
104
 
         {
{
105
 get
            get  { return _sender; }
{ return _sender; }
106
 set
            set  { _sender = value; }
{ _sender = value; }
107 }
        }
108 private string _password;
        private string _password;
109
110 public string Password
        public string Password
111
 
         {
{
112
 get
            get  { return _password; }
{ return _password; }
113
 set
            set  { _password = value; }
{ _password = value; }
114 }
        }
115 }
    }
116
117 public class SmtpConfig
    public class SmtpConfig
118
 
     {
{
119 private static SmtpConfig _smtpConfig;
        private static SmtpConfig _smtpConfig;
120 private string ConfigFile
        private string ConfigFile
121
 
         {
{
122 get
            get
123
 
             {
{
124 string configPath = ConfigurationManager.AppSettings["SmtpConfigPath"];
                string configPath = ConfigurationManager.AppSettings["SmtpConfigPath"];
125 if (string.IsNullOrEmpty(configPath) || configPath.Trim().Length == 0)
                if (string.IsNullOrEmpty(configPath) || configPath.Trim().Length == 0)
126
 
                 {
{
127 configPath = HttpContext.Current.Request.MapPath("/Config/SmtpSetting.config");
                    configPath = HttpContext.Current.Request.MapPath("/Config/SmtpSetting.config");
128 }
                }
129 else
                else
130
 
                 {
{
131 if (!Path.IsPathRooted(configPath))
                    if (!Path.IsPathRooted(configPath))
132 configPath = HttpContext.Current.Request.MapPath(Path.Combine(configPath, "SmtpSetting.config"));
                        configPath = HttpContext.Current.Request.MapPath(Path.Combine(configPath, "SmtpSetting.config"));
133 else
                    else
134 configPath = Path.Combine(configPath, "SmtpSetting.config");
                        configPath = Path.Combine(configPath, "SmtpSetting.config");
135 }
                }
136 return configPath;
                return configPath;
137 }
            }
138 }
        }
139 public SmtpSetting SmtpSetting
        public SmtpSetting SmtpSetting
140
 
         {
{
141 get
            get
142
 
             {
{
143 XmlDocument doc = new XmlDocument();
                XmlDocument doc = new XmlDocument();
144 doc.Load(this.ConfigFile);
                doc.Load(this.ConfigFile);
145 SmtpSetting smtpSetting = new SmtpSetting();
                SmtpSetting smtpSetting = new SmtpSetting();
146 smtpSetting.Server = doc.DocumentElement.SelectSingleNode("Server").InnerText;
                smtpSetting.Server = doc.DocumentElement.SelectSingleNode("Server").InnerText;
147 smtpSetting.Authentication = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("Authentication").InnerText);
                smtpSetting.Authentication = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("Authentication").InnerText);
148 smtpSetting.User = doc.DocumentElement.SelectSingleNode("User").InnerText;
                smtpSetting.User = doc.DocumentElement.SelectSingleNode("User").InnerText;
149 smtpSetting.Password = doc.DocumentElement.SelectSingleNode("Password").InnerText;
                smtpSetting.Password = doc.DocumentElement.SelectSingleNode("Password").InnerText;
150 smtpSetting.Sender = doc.DocumentElement.SelectSingleNode("Sender").InnerText;
                smtpSetting.Sender = doc.DocumentElement.SelectSingleNode("Sender").InnerText;
151
152 return smtpSetting;
                return smtpSetting;
153 }
            }
154 }
        }
155 private SmtpConfig()
        private SmtpConfig()
156
 
         {
{
157
158 }
        }
159 public static SmtpConfig Create()
        public static SmtpConfig Create()
160
 
         {
{
161 if (_smtpConfig == null)
            if (_smtpConfig == null)
162
 
             {
{
163 _smtpConfig = new SmtpConfig();
                _smtpConfig = new SmtpConfig();
164 }
            }
165 return _smtpConfig;
            return _smtpConfig;
166 }
        }
167 }
    }
 public class MailSender
    public class MailSender2

 
     {
{3
 public static void Send(string server, string sender, string recipient, string subject,
        public static void Send(string server, string sender, string recipient, string subject,4
 string body, bool isBodyHtml, Encoding encoding, bool isAuthentication, params string[] files)
    string body, bool isBodyHtml, Encoding encoding, bool isAuthentication, params string[] files)5

 
         {
{6
 SmtpClient smtpClient = new SmtpClient(server);
            SmtpClient smtpClient = new SmtpClient(server);7
 MailMessage message = new MailMessage(sender, recipient);
            MailMessage message = new MailMessage(sender, recipient);8
 message.IsBodyHtml = isBodyHtml;
            message.IsBodyHtml = isBodyHtml;9

10
 message.SubjectEncoding = encoding;
            message.SubjectEncoding = encoding;11
 message.BodyEncoding = encoding;
            message.BodyEncoding = encoding;12

13
 message.Subject = subject;
            message.Subject = subject;14
 message.Body = body;
            message.Body = body;15

16
 message.Attachments.Clear();
            message.Attachments.Clear();17
 if (files != null && files.Length != 0)
            if (files != null && files.Length != 0)18

 
             {
{19
 for (int i = 0; i < files.Length; ++i)
                for (int i = 0; i < files.Length; ++i)20

 
                 {
{21
 Attachment attach = new Attachment(files[i]);
                    Attachment attach = new Attachment(files[i]);22
 message.Attachments.Add(attach);
                    message.Attachments.Add(attach);23
 }
                }24
 }
            }25

26
 if (isAuthentication == true)
            if (isAuthentication == true)27

 
             {
{28
 smtpClient.Credentials = new NetworkCredential(SmtpConfig.Create().SmtpSetting.User,
                smtpClient.Credentials = new NetworkCredential(SmtpConfig.Create().SmtpSetting.User,29
 SmtpConfig.Create().SmtpSetting.Password);
                    SmtpConfig.Create().SmtpSetting.Password);30
 }
            }31
 smtpClient.Send(message);
            smtpClient.Send(message);32

33

34
 }
        }35

36
 public static void Send(string recipient, string subject, string body)
        public static void Send(string recipient, string subject, string body)37

 
         {
{38
 Send(SmtpConfig.Create().SmtpSetting.Server, SmtpConfig.Create().SmtpSetting.Sender, recipient, subject, body, true, Encoding.Default, true, null);
            Send(SmtpConfig.Create().SmtpSetting.Server, SmtpConfig.Create().SmtpSetting.Sender, recipient, subject, body, true, Encoding.Default, true, null);39
 }
        }40

41
 public static void Send(string Recipient, string Sender, string Subject, string Body)
        public static void Send(string Recipient, string Sender, string Subject, string Body)42

 
         {
{43
 Send(SmtpConfig.Create().SmtpSetting.Server, Sender, Recipient, Subject, Body, true, Encoding.UTF8, true, null);
            Send(SmtpConfig.Create().SmtpSetting.Server, Sender, Recipient, Subject, Body, true, Encoding.UTF8, true, null);44
 }
        }45

46
 static readonly string smtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"];
        static readonly string smtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"];47
 static readonly string userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
        static readonly string userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];48
 static readonly string pwd = System.Configuration.ConfigurationManager.AppSettings["Pwd"];
        static readonly string pwd = System.Configuration.ConfigurationManager.AppSettings["Pwd"];49
 static readonly int smtpPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpPort"]);
        static readonly int smtpPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpPort"]);50
 static readonly string authorName = System.Configuration.ConfigurationManager.AppSettings["AuthorName"];
        static readonly string authorName = System.Configuration.ConfigurationManager.AppSettings["AuthorName"];51
 static readonly string to = System.Configuration.ConfigurationManager.AppSettings["To"];
        static readonly string to = System.Configuration.ConfigurationManager.AppSettings["To"];52

53

54
 public void Send(string subject, string body)
        public void Send(string subject, string body)55

 
         {
{56

57
 List<string> toList = StringPlus.GetSubStringList(StringPlus.ToDBC(to), ',');
            List<string> toList = StringPlus.GetSubStringList(StringPlus.ToDBC(to), ',');58
 OpenSmtp.Mail.Smtp smtp = new OpenSmtp.Mail.Smtp(smtpServer, userName, pwd, smtpPort);
            OpenSmtp.Mail.Smtp smtp = new OpenSmtp.Mail.Smtp(smtpServer, userName, pwd, smtpPort);59
 foreach (string s in toList)
            foreach (string s in toList)60

 
             {
{61
 OpenSmtp.Mail.MailMessage msg = new OpenSmtp.Mail.MailMessage();
                OpenSmtp.Mail.MailMessage msg = new OpenSmtp.Mail.MailMessage();62
 msg.From = new OpenSmtp.Mail.EmailAddress(userName, authorName);
                msg.From = new OpenSmtp.Mail.EmailAddress(userName, authorName);63
 
                64
 msg.AddRecipient(s, OpenSmtp.Mail.AddressType.To);
                msg.AddRecipient(s, OpenSmtp.Mail.AddressType.To);65

66
 //设置邮件正文,并指定格式为 html 格式
                //设置邮件正文,并指定格式为 html 格式67
 msg.HtmlBody = body;
                msg.HtmlBody = body;68
 //设置邮件标题
                //设置邮件标题69
 msg.Subject = subject;
                msg.Subject = subject;70
 //指定邮件正文的编码
                //指定邮件正文的编码71
 msg.Charset = "gb2312";
                msg.Charset = "gb2312";72
 //发送邮件
                //发送邮件73
 smtp.SendMail(msg);
                smtp.SendMail(msg);74
 }
            }75
 }
        }76
 }
    }77

78
 public class SmtpSetting
    public class SmtpSetting79

 
     {
{80
 private string _server;
        private string _server;81

82
 public string Server
        public string Server83

 
         {
{84

 get
            get  { return _server; }
{ return _server; }85

 set
            set  { _server = value; }
{ _server = value; }86
 }
        }87
 private bool _authentication;
        private bool _authentication;88

89
 public bool Authentication
        public bool Authentication90

 
         {
{91

 get
            get  { return _authentication; }
{ return _authentication; }92

 set
            set  { _authentication = value; }
{ _authentication = value; }93
 }
        }94
 private string _user;
        private string _user;95

96
 public string User
        public string User97

 
         {
{98

 get
            get  { return _user; }
{ return _user; }99

 set
            set  { _user = value; }
{ _user = value; }100
 }
        }101
 private string _sender;
        private string _sender;102

103
 public string Sender
        public string Sender104

 
         {
{105

 get
            get  { return _sender; }
{ return _sender; }106

 set
            set  { _sender = value; }
{ _sender = value; }107
 }
        }108
 private string _password;
        private string _password;109

110
 public string Password
        public string Password111

 
         {
{112

 get
            get  { return _password; }
{ return _password; }113

 set
            set  { _password = value; }
{ _password = value; }114
 }
        }115
 }
    }116

117
 public class SmtpConfig
    public class SmtpConfig118

 
     {
{119
 private static SmtpConfig _smtpConfig;
        private static SmtpConfig _smtpConfig;120
 private string ConfigFile
        private string ConfigFile121

 
         {
{122
 get
            get123

 
             {
{124
 string configPath = ConfigurationManager.AppSettings["SmtpConfigPath"];
                string configPath = ConfigurationManager.AppSettings["SmtpConfigPath"];125
 if (string.IsNullOrEmpty(configPath) || configPath.Trim().Length == 0)
                if (string.IsNullOrEmpty(configPath) || configPath.Trim().Length == 0)126

 
                 {
{127
 configPath = HttpContext.Current.Request.MapPath("/Config/SmtpSetting.config");
                    configPath = HttpContext.Current.Request.MapPath("/Config/SmtpSetting.config");128
 }
                }129
 else
                else130

 
                 {
{131
 if (!Path.IsPathRooted(configPath))
                    if (!Path.IsPathRooted(configPath))132
 configPath = HttpContext.Current.Request.MapPath(Path.Combine(configPath, "SmtpSetting.config"));
                        configPath = HttpContext.Current.Request.MapPath(Path.Combine(configPath, "SmtpSetting.config"));133
 else
                    else134
 configPath = Path.Combine(configPath, "SmtpSetting.config");
                        configPath = Path.Combine(configPath, "SmtpSetting.config");135
 }
                }136
 return configPath;
                return configPath;137
 }
            }138
 }
        }139
 public SmtpSetting SmtpSetting
        public SmtpSetting SmtpSetting140

 
         {
{141
 get
            get142

 
             {
{143
 XmlDocument doc = new XmlDocument();
                XmlDocument doc = new XmlDocument();144
 doc.Load(this.ConfigFile);
                doc.Load(this.ConfigFile);145
 SmtpSetting smtpSetting = new SmtpSetting();
                SmtpSetting smtpSetting = new SmtpSetting();146
 smtpSetting.Server = doc.DocumentElement.SelectSingleNode("Server").InnerText;
                smtpSetting.Server = doc.DocumentElement.SelectSingleNode("Server").InnerText;147
 smtpSetting.Authentication = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("Authentication").InnerText);
                smtpSetting.Authentication = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("Authentication").InnerText);148
 smtpSetting.User = doc.DocumentElement.SelectSingleNode("User").InnerText;
                smtpSetting.User = doc.DocumentElement.SelectSingleNode("User").InnerText;149
 smtpSetting.Password = doc.DocumentElement.SelectSingleNode("Password").InnerText;
                smtpSetting.Password = doc.DocumentElement.SelectSingleNode("Password").InnerText;150
 smtpSetting.Sender = doc.DocumentElement.SelectSingleNode("Sender").InnerText;
                smtpSetting.Sender = doc.DocumentElement.SelectSingleNode("Sender").InnerText;151

152
 return smtpSetting;
                return smtpSetting;153
 }
            }154
 }
        }155
 private SmtpConfig()
        private SmtpConfig()156

 
         {
{157

158
 }
        }159
 public static SmtpConfig Create()
        public static SmtpConfig Create()160

 
         {
{161
 if (_smtpConfig == null)
            if (_smtpConfig == null)162

 
             {
{163
 _smtpConfig = new SmtpConfig();
                _smtpConfig = new SmtpConfig();164
 }
            }165
 return _smtpConfig;
            return _smtpConfig;166
 }
        }167
 }
    }
    一个完整的人生应该是宽恕、容忍、等待和爱!
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号