⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mailmessage.cs

📁 用C#开发实现SMTP相关技术,能接收到带附件的邮件服务功能.
💻 CS
📖 第 1 页 / 共 2 页
字号:
namespace OpenSmtp.Mail {

/******************************************************************************
	Copyright 2001-2004 Ian Stallings
	OpenSmtp.Net is free software; you can redistribute it and/or modify
	it under the terms of the Lesser GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	OpenSmtp.Net is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	Lesser GNU General Public License for more details.

	You should have received a copy of the Lesser GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
/*******************************************************************************/


using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Text;

	/// <summary>
	/// This Type stores the addresses, attachments, body, subject,
	/// and properties of the email message. There can be many
	/// attachments and email addresses in each MailMessage.
	/// <seealso cref="EmailAddress"/>
	/// <seealso cref="Attachment"/>
	/// </summary>
	/// <remarks>
	/// This Type stores the addresses, attachments, body, subject,
	/// and properties of the email message. There can be many
	/// attachments and email addresses in each MailMessage.
	/// <seealso cref="EmailAddress"/>
	/// <seealso cref="Attachment"/>
	/// </remarks>
	/// <example>
	/// <code>
	///		from 			= new EmailAddress("support@OpenSmtp.com", "Support");
	///		to				= new EmailAddress("recipient@OpenSmtp.com", "Joe Smith");
	///		cc				= new EmailAddress("cc@OpenSmtp.com", "CC Name");
	///
	///		msg 			= new MailMessage(from, to);
	///		msg.AddRecipient(cc, AddressType.Cc);
	///		msg.AddRecipient("bcc@OpenSmtp.com", AddressType.Bcc);
	///
	///		msg.Subject 	= "Testing OpenSmtp .Net SMTP component";
	///		msg.Body 		= "Hello Joe Smith.";
	///		msg.HtmlBody 	= "<html><body>Hello Joe Smith.</body></html>";
	///
	///		// mail message properties
	///		msg.Charset		= "ISO-8859-1";
	///		msg.Priority 	= MailPriority.High;
	///		msg.Notification = true;
	///
	///		// add custom headers
	///		msg.AddCustomHeader("X-CustomHeader", "Value");
	///		msg.AddCustomHeader("X-CompanyName", "Value");
	///
	///		// add attachments
	///		msg.AddAttachment(@"..\attachments\test.jpg");
	///		msg.AddAttachment(@"..\attachments\test.htm");
	/// </code>
	/// </example>
	public class MailMessage
	{
		internal EmailAddress			from;
		internal EmailAddress			replyTo;
		internal ArrayList				recipientList;
		internal ArrayList				ccList;
		internal ArrayList				bccList;
		internal ArrayList				attachments;
		internal string					subject;
		internal string					body;
		internal string					htmlBody;
		internal string					mixedBoundary;
		internal string					altBoundary;
		internal string					relatedBoundary;
		internal string					charset = "ISO-8859-1";
		internal bool					notification;
		internal string					priority;
		internal ArrayList				customHeaders;
		internal ArrayList				images;

		public MailMessage()
		{
			recipientList = new ArrayList();
			ccList = new ArrayList();
			bccList = new ArrayList();
			attachments = new ArrayList();
			images = new ArrayList();
			customHeaders = new ArrayList();
			mixedBoundary = MailMessage.generateMixedMimeBoundary();
            altBoundary = MailMessage.generateAltMimeBoundary();
			relatedBoundary = MailMessage.generateRelatedMimeBoundary();
         }

		/// <summary>Constructor using EmailAddress type</summary>
		/// <example>
		/// <code>
		/// 	EmailAddress from 	= new EmailAddress("support@OpenSmtp.com", "Support");
		/// 	EmailAddress to 	= new EmailAddress("recipient@OpenSmtp.com", "Joe Smith");
		/// 	MailMessage msg 	= new MailMessage(from, to);
		/// </code>
		/// </example>
		public MailMessage(EmailAddress sender, EmailAddress recipient):this()
		{
			from = sender;
			recipientList.Add(recipient);
		}

		/// <summary>Constructor using string email addresses</summary>
		/// <example>
		/// <code>
		/// 	MailMessage msg = new MailMessage("support@OpenSmtp.com", "recipient@OpenSmtp.com");
		/// </code>
		/// </example>
		public MailMessage(string senderAddress, string recipientAddress):this(new EmailAddress(senderAddress), new EmailAddress(recipientAddress))
		{}


		// -------------------------- Properties --------------------------

		/// <value>Stores the EmailAddress to Reply-To.
		/// If no EmailAddress is specified the From address is used.</value>
		public EmailAddress ReplyTo
		{
			get { return replyTo != null ? replyTo : from; }
			set { replyTo = value; }
		}

		/// <value>Stores the EmailAddress of the sender</value>
		public EmailAddress From
		{
			get { return from; }
			set { from = value; }
		}

		/// <value>Stores the EmailAddress of the recipient</value>
		public ArrayList To
		{
			get { return recipientList; }
			set { recipientList = value; }
		}

		/// <value>Stores the subject of the MailMessage</value>
		public string Subject
		{
			get { return subject; }
			set { subject = value; }
		}

		/// <value>Stores the text body of the MailMessage</value>
		public string Body
		{
			get { return body; }
			set { body = value; }
		}

		/// <value>Stores the HTML body of the MailMessage</value>
		public string HtmlBody
		{
			get { return htmlBody; }
			set { htmlBody = value; }
		}

		/// <value>Stores Mail Priority value</value>
		/// <seealso>MailPriority</seealso>
		public string Priority
		{
			get { return priority; }
			set { priority = value; }
		}


		/// <value>Stores the Read Confirmation Reciept</value>
		public bool Notification
		{
			get { return notification; }
			set { notification = value; }
		}

		/// <value>Stores an ArrayList of CC EmailAddresses</value>
		public ArrayList CC
		{
			get { return ccList; }
			set { ccList = value; }
		}

		/// <value>Stores an ArrayList of BCC EmailAddresses</value>
		public ArrayList BCC
		{
			get { return bccList; }
			set { bccList = value; }
		}

		/// <value>Stores the character set of the MailMessage</value>
		public string Charset
		{
			get { return charset; }
			set { charset = value; }
		}

		/// <value>Stores a list of Attachments</value>
		public ArrayList Attachments
		{
			get { return attachments; }
			set { attachments = value; }
		}

		/// <value>Stores a NameValueCollection of custom headers</value>
		public ArrayList CustomHeaders
		{
			get { return customHeaders; }
			set { customHeaders = value; }
		}

		/// <value>Stores the string boundary used between MIME parts</value>
		internal string AltBoundary
		{
			get { return altBoundary; }
			set { altBoundary = value; }
		}

         /// <value>Stores the string boundary used between MIME parts</value>
         internal string MixedBoundary
         {
             get { return mixedBoundary; }
             set { mixedBoundary = value; }
         }

         /// <summary>Adds a recipient EmailAddress to the MailMessage</summary>
		/// <param name="address">EmailAddress that you want to add to the MailMessage</param>
		/// <param name="type">AddressType of the address</param>
		/// <example>
		/// <code>
		/// 	MailMessage msg = new MailMessage("support@OpenSmtp.com", "recipient@OpenSmtp.com");
		///		EmailAddress cc = new EmailAddress("cc@OpenSmtp.com");
		///		msg.AddRecipient(cc, AddressType.Cc);
		/// </code>
		/// </example>
		public void AddRecipient(EmailAddress address, AddressType type)
		{
			try
			{
				switch(type)
				{
					case AddressType.To:
						recipientList.Add(address);
						break;
					case AddressType.Cc:
						ccList.Add(address);
						break;
					case AddressType.Bcc:
						bccList.Add(address);
						break;
				}
			}
			catch(Exception e)
			{
				throw new SmtpException("Exception in AddRecipient: " + e.ToString());
			}
		}

		/// <summary>Adds a recipient RFC 822 formatted email address to the MailMessage</summary>
		/// <param name="address">RFC 822 formatted email address that you want to add to the MailMessage</param>
		/// <param name="type">AddressType of the email address</param>
		/// <example>
		/// <code>
		/// 	MailMessage msg = new MailMessage("support@OpenSmtp.com", "recipient@OpenSmtp.com");
		///		msg.AddRecipient("cc@OpenSmtp.com", AddressType.Cc);
		/// </code>
		/// </example>
		public void AddRecipient(string address, AddressType type)
		{
			EmailAddress email = new EmailAddress(address);
			AddRecipient(email, type);
		}


		/// <summary>Adds an Attachment to the MailMessage using a file path</summary>
		/// <param name="filepath">File path to the file you want to attach to the MailMessage</param>
		/// <example>
		/// <code>
		/// 	MailMessage msg = new MailMessage("support@OpenSmtp.com", "recipient@OpenSmtp.com");
		///		msg.AddAttachment(@"C:\file.jpg");
		/// </code>
		/// </example>
		// start added/modified by mb
		public void AddAttachment(string filepath)
		{
			AddAttachment(filepath, NewCid());
		}

		/// <summary>Adds an included image to the MailMessage using a file path</summary>
		/// <param name="filepath">File path to the file you want to attach to the MailMessage</param>
		/// <example>
		/// <code>
		/// 	MailMessage msg = new MailMessage("support@OpenSmtp.com", "recipient@OpenSmtp.com");
		///		msg.AddImage(@"C:\file.jpg");
		/// </code>
		/// </example>
		// start added/modified by mb
		public void AddImage(string filepath)
		{
			AddImage(filepath, NewCid());
		}

		public void AddImage(string filepath, string cid) 
		{
			if (filepath != null)
			{
				Attachment image = new Attachment(filepath);
				image.contentid=cid;
				images.Add(image);
			}			
		}
	
	
		/// <summary>
		/// Generate a content id
		/// </summary>
		/// <returns></returns>
		private string NewCid() 
		{
			int attachmentid=attachments.Count+images.Count+1;
			return "att"+attachmentid;
		}

		public void AddAttachment(string filepath, string cid) 
		{
			if (filepath != null)
			{
				Attachment attachment = new Attachment(filepath);
				attachment.contentid=cid;
				attachments.Add(attachment);
			}			
		}
		// end added by mb

		/// <summary>Adds an Attachment to the MailMessage using an Attachment instance</summary>
		/// <param name="attachment">Attachment you want to attach to the MailMessage</param>
		/// <example>
		/// <code>
		/// 	MailMessage msg = new MailMessage("support@OpenSmtp.com", "recipient@OpenSmtp.com");
		///		Attachment att = new Attachment(@"C:\file.jpg");
		///		msg.AddAttachment(att);
		/// </code>
		/// </example>
		public void AddAttachment(Attachment attachment)
		{
			if (attachment != null)
			{
                 attachments.Add(attachment);
             }
		}

		/// <summary>Adds an Attachment to the MailMessage using a provided Stream</summary>
		/// <param name="stream">stream you want to attach to the MailMessage</param>
		/// <example>
		/// <code>
		/// 	MailMessage msg = new MailMessage("support@OpenSmtp.com", "recipient@OpenSmtp.com");
		///		Attachment att = new Attachment(new FileStream(@"C:\File.jpg", FileMode.Open, FileAccess.Read), "Test Name");
		///		msg.AddAttachment(att);
		/// </code>
		/// </example>
		public void AddAttachment(Stream stream)
		{
			if (stream != null)
			{
                 attachments.Add(stream);
             }
		}


		/// <summary>
		/// Adds an custom header to the MailMessage.
		///	NOTE: some SMTP servers will reject improper custom headers
		///</summary>
		/// <param name="name">Name of the custom header</param>
		/// <param name="body">Value of the custom header</param>
		/// <example>
		/// <code>
		/// 	MailMessage msg = new MailMessage("support@OpenSmtp.com", "recipient@OpenSmtp.com");
		///		msg.AddCustomHeader("X-Something", "HeaderValue");
		/// </code>
		/// </example>
		public void AddCustomHeader(string name, string body)
		{
			if (name != null && body != null)
			{
                 AddCustomHeader(new MailHeader(name, body));
             }
		}

		/// <summary>
		/// Adds an custom header to the MailMessage.
		///	NOTE: some SMTP servers will reject improper custom headers

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -