System.Net.Smtp.SmtpMessage.GenerateMessage C# (CSharp) Method

GenerateMessage() public method

public GenerateMessage ( ) : String
return String
		public String GenerateMessage()
		{
			StringBuilder message = new StringBuilder();
			String bound = "";
			String boundMixed = "";

			if (Type == MessageType.None)
			{
				throw new SmtpException("Type == MessageType.NONE");
			}

			message.AppendLine("To: " + To);
			message.AppendLine("From: " + From);
			message.AppendLine("Subject: " + Subject);
			message.AppendLine("Reply-To: " + (ReplyTo != "" ? ReplyTo : From));
			if (ReadReceiptTo != "") message.AppendLine("Disposition-Notification-To: " + ReadReceiptTo);

			foreach (SmtpHeader h in Headers)
			{
				message.AppendLine(h.Name + ": " + h.Value);
			}

			message.AppendLine("MIME-Version: 1.0");

			if (_attachments.Count > 0)
			{
				bound = GenerateBound();
				boundMixed = GenerateBound();
				message.AppendLine("Content-type: multipart/mixed; boundary=\"" + boundMixed + "\"");
			}
			else switch (Type)
			{
				case MessageType.TextOnly:
					message.AppendLine("Content-type: text/plain");
					break;
				case MessageType.HtmlOnly:
					message.AppendLine("Content-type: text/html");
					break;
				case MessageType.TextAndHtml:
					bound = GenerateBound();
					message.AppendLine("Content-type: multipart/alternative; boundary=\"" + bound + "\"");
					break;
			}

			message.AppendLine();

			if (_attachments.Count > 0)
			{
				// open up bound mixed
				message.AppendLine("--" + boundMixed);
				message.AppendLine("Content-type: multipart/alternative; boundary=\"" + bound + "\"");
				message.AppendLine();

				message.AppendLine("--" + bound);

				if (Type == MessageType.TextOnly)
				{
					message.AppendLine("Content-type: text/plain");
					message.AppendLine();
					message.AppendLine(BodyText);
				}
				else if (Type == MessageType.HtmlOnly)
				{
					message.AppendLine("Content-type: text/html");
					message.AppendLine();
					message.AppendLine(BodyHtml);
				}
				else if (Type == MessageType.TextAndHtml)
				{
					message.AppendLine("Content-type: text/plain");
					message.AppendLine();
					message.AppendLine(BodyText);
					message.AppendLine();

					message.AppendLine("--" + bound);
					message.AppendLine("Content-type: text/html");
					message.AppendLine();
					message.AppendLine(BodyHtml);
				}

				message.AppendLine();
				message.AppendLine("--" + bound + "--");
				message.AppendLine();

				foreach (SmtpAttachment i in Attachments)
				{
					message.AppendLine("--" + boundMixed);
					message.AppendLine("Content-Type: " + i.GetMimeType() + "; name=\"" + i.FileNameShort + "\"");
					message.AppendLine("Content-Transfer-Encoding: base64");
					message.AppendLine("Content-Disposition: attachment; filename=\"" + i.FileNameShort + "\"");
					message.AppendLine();
					message.AppendLine(i.GetBase64());
					message.AppendLine();
				}

				message.AppendLine("--" + boundMixed + "--");
			}
			else
			{
				if (Type == MessageType.TextOnly)
				{
					message.AppendLine(BodyText);
				}
				else if (Type == MessageType.HtmlOnly)
				{
					message.AppendLine(BodyHtml);
				}
				else if (Type == MessageType.TextAndHtml)
				{
					message.AppendLine("--" + bound);
					message.AppendLine("Content-type: text/plain");
					message.AppendLine();
					message.AppendLine(BodyText);

					message.AppendLine("--" + bound);
					message.AppendLine("Content-type: text/html");
					message.AppendLine();
					message.AppendLine(BodyHtml);

					message.AppendLine("--" + bound + "--");
				}
			}

			return message.ToString();
		}

Usage Example

Esempio n. 1
0
 public void SendMessage(SmtpMessage msg)
 {
     _messageQueue.Add(1);
     SendMailFrom(msg.From);
     SendRcptTo(msg.To);
     SendData(msg.GenerateMessage());
     _messageQueue.RemoveAt(0);
 }
All Usage Examples Of System.Net.Smtp.SmtpMessage::GenerateMessage