Mail C#
来源:互联网 发布:jdk 7u67 windows x32 编辑:程序博客网 时间:2024/06/06 09:18
- using System;
- using System.Net;
- using System.Net.Mail;
- using System.Text;
- using System.ComponentModel;
- namespace Tmiqpl.Net
- {
-
-
-
-
- public class SMTP : IDisposable
- {
- #region 预定义
- private string server = null;
- private string username = null;
- private string password = null;
- private int port = 25;
- private bool network = true;
- private bool ishtml = true;
- private string subject = null;
- private string body = null;
- private string from = null;
- private string to = null;
- private string errortext = null;
- private string[] attachement = null;
- private Encoding bodyencoding = Encoding.Default;
- private Encoding subjectencoding = Encoding.Default;
- private Component conponent = new Component();
- private bool disposed = false;
- private string[] cc = null;
- private string[] bcc = null;
- #endregion
-
-
-
-
- public SMTP(string servername)
- : this(servername, 25)
- {
- }
-
-
-
-
-
- public SMTP(string servername, int prot)
- : this(servername, prot, null, null)
- {
- }
-
-
-
-
-
-
-
- public SMTP(string servername, int prot, string username, string password)
- {
- this.ServerName = servername;
- this.port = prot;
- this.UserName = username;
- this.PassWord = password;
- }
- ~SMTP()
- {
- this.Dispose(false);
- }
- protected void Dispose(bool disposed)
- {
- if (!this.disposed && disposed)
- {
- conponent.Dispose();
- }
- this.disposed = true;
- }
-
-
-
- public bool Send()
- {
- SmtpClient _client;
- if (this.ServerName == null)
- {
- this.errortext = "(SMTP) 服务器未定义";
- return false;
- }
- _client = new SmtpClient(this.server, this.port);
- _client.UseDefaultCredentials = false;
- if ((this.username == null) || (this.password == null))
- {
- _client.Credentials = CredentialCache.DefaultNetworkCredentials;
- }
- else
- {
- _client.Credentials = new NetworkCredential(this.username, this.password);
- }
- if (this.network)
- {
- _client.DeliveryMethod = SmtpDeliveryMethod.Network;
- }
- else
- {
- _client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
- }
- if (this.from == null)
- {
- this.errortext = "发件人地址未定义!";
- return false;
- }
- if (this.to == null)
- {
- this.errortext = "收件人人地址未定义!";
- return false;
- }
- MailAddress from = new MailAddress(this.from);
- MailAddress to = new MailAddress(this.to);
- MailMessage msg = new MailMessage(from, to);
- if (this.subject == null)
- {
- this.errortext = "邮件主题未定义!";
- return false;
- }
- msg.Subject = this.subject;
- msg.Body = this.body;
- if (this.cc != null)
- {
- if (this.cc.Length > 0)
- {
- foreach (string cc in this.cc)
- {
- msg.CC.Add(cc);
- }
- }
- }
- if (this.bcc != null)
- {
- if (this.bcc.Length > 0)
- {
- foreach (string bcc in this.bcc)
- {
- msg.Bcc.Add(bcc);
- }
- }
- }
- if (this.attachement != null)
- {
- if (this.attachement.Length > 0)
- {
- foreach (string att in this.attachement)
- {
- msg.Attachments.Add(new Attachment(att));
- }
- }
- }
- msg.BodyEncoding = this.bodyencoding;
- msg.SubjectEncoding = this.subjectencoding;
- msg.IsBodyHtml = this.ishtml;
- try
- {
- _client.Send(msg);
- return true;
- }
- catch (Exception e)
- {
- this.errortext = e.Message;
- return false;
- }
- finally
- {
- msg.Dispose();
- }
- }
- #region 属性
-
-
-
- public string ServerName
- {
- get { return this.server; }
- set { this.server = value; }
- }
-
-
-
- public string UserName
- {
- get { return this.username; }
- set { this.username = value; }
- }
-
-
-
- public string PassWord
- {
- get { return this.password; }
- set { this.password = value; }
- }
-
-
-
- public int Port
- {
- get { return this.port; }
- set { this.port = value; }
- }
-
-
-
- public bool IsNetwork
- {
- get { return this.network; }
- set { this.network = value; }
- }
-
-
-
- public bool IsHtml
- {
- get { return this.ishtml; }
- set { this.ishtml = value; }
- }
-
-
-
- public string Body
- {
- get { return this.body; }
- set { this.body = value; }
- }
-
-
-
- public string Subject
- {
- get { return this.subject; }
- set { this.subject = value; }
- }
-
-
-
- public string From
- {
- get { return this.from; }
- set { this.from = value; }
- }
-
-
-
- public string To
- {
- get { return this.to; }
- set { this.to = value; }
- }
-
-
-
- public string ErrorText
- {
- get { return this.errortext; }
- }
-
-
-
- public string[] Attachement
- {
- get { return this.attachement; }
- set { this.attachement = value; }
- }
-
-
-
- public string[] Cc
- {
- get { return this.cc; }
- set { this.cc = value; }
- }
-
-
-
- public string[] Bcc
- {
- get { return this.bcc; }
- set { this.bcc = value; }
- }
-
-
-
- public Encoding BodyEncoding
- {
- get { return this.bodyencoding; }
- set { this.bodyencoding = value; }
- }
-
-
-
- public Encoding SubjectEncoding
- {
- get { return this.subjectencoding; }
- set { this.subjectencoding = value; }
- }
- #endregion
-
-
-
- public virtual void Dispose()
- {
- this.Dispose(true);
- GC.SuppressFinalize(this);
- }
- }
- }