/// <summary> /// Send any attachments. /// </summary> /// <param name="buf">String work area.</param> /// <param name="type">Attachment type to send.</param> private void SendAttachments(StringBuilder buf, AttachmentLocation type) { //declare mime info for attachment byte[] fbuf = new byte[2048]; int num; SmtpAttachment attachment; string seperator = type == AttachmentLocation.Attachment ? "\r\n--#SEPERATOR1#" : "\r\n--#SEPERATOR2#"; buf.Length = 0; foreach (object o in files) { attachment = (SmtpAttachment)o; if (attachment.Location != type) { continue; } CryptoStream cs = new CryptoStream( new FileStream(attachment.FileName, FileMode.Open, FileAccess.Read, FileShare.Read), new ToBase64Transform(), CryptoStreamMode.Read); con.SendCommand(seperator); buf.Append("Content-Type: "); buf.Append(attachment.ContentType); buf.Append("; name="); buf.Append(Path.GetFileName(attachment.FileName)); con.SendCommand(buf.ToString()); con.SendCommand("Content-Transfer-Encoding: base64"); buf.Length = 0; buf.Append("Content-Disposition: attachment; filename="); buf.Append(Path.GetFileName(attachment.FileName)); con.SendCommand(buf.ToString()); buf.Length = 0; buf.Append("Content-ID: "); buf.Append(Path.GetFileNameWithoutExtension(attachment.FileName)); buf.Append("\r\n"); con.SendCommand(buf.ToString()); buf.Length = 0; num = cs.Read(fbuf, 0, 2048); while (num > 0) { con.SendData(Encoding.ASCII.GetChars(fbuf, 0, num), 0, num); //con.SendData( Encoding.Unicode.GetChars(fbuf, 0, num), 0, num); num = cs.Read(fbuf, 0, 2048); } cs.Close(); con.SendCommand(""); } }
/// <summary> /// Send the message. /// </summary> public void SendMessage() { lock(this) { //do some sanity checking if(host == null || host.Trim().Length == 0) { throw new Exception("No host specified."); } if(from == null || from.Trim().Length == 0) { throw new Exception("No from address."); } if(to.Count == 0) { throw new Exception("No to address."); } //set up initial connection con = new SmtpConnection(); if(port <= 0) port = 25; con.Open(host, port); StringBuilder buf = new StringBuilder( 8192 ); string response; int code; //read greeting con.GetReply(out response, out code); //introduce ourselves buf.Append("HELO "); buf.Append(host); con.SendCommand(buf.ToString()); con.GetReply(out response, out code); buf.Length = 0; buf.Append("MAIL FROM:<"); buf.Append(from); buf.Append(">"); con.SendCommand(buf.ToString()); con.GetReply(out response, out code); buf.Length = 0; //set up list of to addresses foreach(object o in to) { buf.Append("RCPT TO:<"); buf.Append(o); buf.Append(">"); con.SendCommand(buf.ToString()); con.GetReply(out response, out code); buf.Length = 0; } //set headers con.SendCommand("DATA"); con.SendCommand("X-Mailer: smw.smtp.SmtpEmailer"); DateTime today = DateTime.Now; buf.Append("DATE: "); buf.Append(today.ToLongDateString()); con.SendCommand(buf.ToString()); buf.Length = 0; buf.Append("FROM: "); buf.Append(from); con.SendCommand(buf.ToString()); buf.Length = 0; buf.Append("TO: "); buf.Append(to[0]); for(int x = 1; x < to.Count; ++x) { buf.Append(";"); buf.Append(to[x]); } con.SendCommand(buf.ToString()); buf.Length = 0; buf.Append("REPLY-TO: "); buf.Append(from); con.SendCommand(buf.ToString()); buf.Length = 0; buf.Append("SUBJECT: "); buf.Append( subject ); //buf.Append(subject); con.SendCommand(buf.ToString()); buf.Length = 0; //declare mime info for message con.SendCommand("MIME-Version: 1.0"); if( !sendAsHtml || (sendAsHtml && ((SmtpAttachment.inlineCount > 0) || (SmtpAttachment.attachCount > 0))) ) { con.SendCommand("Content-Type: multipart/mixed; boundary=\"#SEPERATOR1#\"\r\n"); con.SendCommand("This is a multi-part message.\r\n\r\n--#SEPERATOR1#"); } if(sendAsHtml) { con.SendCommand("Content-Type: multipart/related; boundary=\"#SEPERATOR2#\""); con.SendCommand("Content-Transfer-Encoding: quoted-printable\r\n"); con.SendCommand("--#SEPERATOR2#"); } if(sendAsHtml && SmtpAttachment.inlineCount > 0) { con.SendCommand("Content-Type: multipart/alternative; boundary=\"#SEPERATOR3#\""); con.SendCommand("Content-Transfer-Encoding: quoted-printable\r\n"); con.SendCommand("--#SEPERATOR3#"); con.SendCommand("Content-Type: text/html; charset=" + m_strCharSet ); con.SendCommand("Content-Transfer-Encoding: quoted-printable\r\n"); con.SendCommand(EncodeBodyAsQuotedPrintable()); con.SendCommand("--#SEPERATOR3#"); con.SendCommand("Content-Type: text/plain; charset=" + m_strCharSet); con.SendCommand("\r\nIf you can see this, then your email client does not support MHTML messages."); con.SendCommand("--#SEPERATOR3#--\r\n"); con.SendCommand("--#SEPERATOR2#\r\n"); SendAttachments(buf, AttachmentLocation.Inline); } else { if(sendAsHtml) { con.SendCommand("Content-Type: text/html; charset=" + m_strCharSet); con.SendCommand("Content-Transfer-Encoding: quoted-printable\r\n"); } else { con.SendCommand("Content-Type: text/plain; charset=" + m_strCharSet); con.SendCommand("Content-Transfer-Encoding: quoted-printable\r\n"); } con.SendCommand(EncodeBodyAsQuotedPrintable()); } if(sendAsHtml) { con.SendCommand("\r\n--#SEPERATOR2#--"); } if(SmtpAttachment.attachCount > 0) { //send normal attachments SendAttachments(buf, AttachmentLocation.Attachment); } //finish up message con.SendCommand(""); if(SmtpAttachment.inlineCount > 0 || SmtpAttachment.attachCount > 0) { con.SendCommand("--#SEPERATOR1#--"); } con.SendCommand("."); con.GetReply(out response, out code); con.SendCommand("QUIT"); con.GetReply(out response, out code); con.Close(); con = null; if(OnMailSent != null) { OnMailSent(); } } }
/// <summary> /// Send the message. /// </summary> public void SendMessage() { lock (this) { //do some sanity checking if (host == null || host.Trim().Length == 0) { throw new Exception("No host specified."); } if (from == null || from.Trim().Length == 0) { throw new Exception("No from address."); } if (to.Count == 0) { throw new Exception("No to address."); } //set up initial connection con = new SmtpConnection(); if (port <= 0) { port = 25; } con.Open(host, port); StringBuilder buf = new StringBuilder(8192); string response; int code; //read greeting con.GetReply(out response, out code); //introduce ourselves buf.Append("HELO "); buf.Append(host); con.SendCommand(buf.ToString()); con.GetReply(out response, out code); buf.Length = 0; buf.Append("MAIL FROM:<"); buf.Append(from); buf.Append(">"); con.SendCommand(buf.ToString()); con.GetReply(out response, out code); buf.Length = 0; //set up list of to addresses foreach (object o in to) { buf.Append("RCPT TO:<"); buf.Append(o); buf.Append(">"); con.SendCommand(buf.ToString()); con.GetReply(out response, out code); buf.Length = 0; } //set headers con.SendCommand("DATA"); con.SendCommand("X-Mailer: smw.smtp.SmtpEmailer"); DateTime today = DateTime.Now; buf.Append("DATE: "); buf.Append(today.ToLongDateString()); con.SendCommand(buf.ToString()); buf.Length = 0; buf.Append("FROM: "); buf.Append(from); con.SendCommand(buf.ToString()); buf.Length = 0; buf.Append("TO: "); buf.Append(to[0]); for (int x = 1; x < to.Count; ++x) { buf.Append(";"); buf.Append(to[x]); } con.SendCommand(buf.ToString()); buf.Length = 0; buf.Append("REPLY-TO: "); buf.Append(from); con.SendCommand(buf.ToString()); buf.Length = 0; buf.Append("SUBJECT: "); buf.Append(subject); //buf.Append(subject); con.SendCommand(buf.ToString()); buf.Length = 0; //declare mime info for message con.SendCommand("MIME-Version: 1.0"); if (!sendAsHtml || (sendAsHtml && ((SmtpAttachment.inlineCount > 0) || (SmtpAttachment.attachCount > 0))) ) { con.SendCommand("Content-Type: multipart/mixed; boundary=\"#SEPERATOR1#\"\r\n"); con.SendCommand("This is a multi-part message.\r\n\r\n--#SEPERATOR1#"); } if (sendAsHtml) { con.SendCommand("Content-Type: multipart/related; boundary=\"#SEPERATOR2#\""); con.SendCommand("Content-Transfer-Encoding: quoted-printable\r\n"); con.SendCommand("--#SEPERATOR2#"); } if (sendAsHtml && SmtpAttachment.inlineCount > 0) { con.SendCommand("Content-Type: multipart/alternative; boundary=\"#SEPERATOR3#\""); con.SendCommand("Content-Transfer-Encoding: quoted-printable\r\n"); con.SendCommand("--#SEPERATOR3#"); con.SendCommand("Content-Type: text/html; charset=" + m_strCharSet); con.SendCommand("Content-Transfer-Encoding: quoted-printable\r\n"); con.SendCommand(EncodeBodyAsQuotedPrintable()); con.SendCommand("--#SEPERATOR3#"); con.SendCommand("Content-Type: text/plain; charset=" + m_strCharSet); con.SendCommand("\r\nIf you can see this, then your email client does not support MHTML messages."); con.SendCommand("--#SEPERATOR3#--\r\n"); con.SendCommand("--#SEPERATOR2#\r\n"); SendAttachments(buf, AttachmentLocation.Inline); } else { if (sendAsHtml) { con.SendCommand("Content-Type: text/html; charset=" + m_strCharSet); con.SendCommand("Content-Transfer-Encoding: quoted-printable\r\n"); } else { con.SendCommand("Content-Type: text/plain; charset=" + m_strCharSet); con.SendCommand("Content-Transfer-Encoding: quoted-printable\r\n"); } con.SendCommand(EncodeBodyAsQuotedPrintable()); } if (sendAsHtml) { con.SendCommand("\r\n--#SEPERATOR2#--"); } if (SmtpAttachment.attachCount > 0) { //send normal attachments SendAttachments(buf, AttachmentLocation.Attachment); } //finish up message con.SendCommand(""); if (SmtpAttachment.inlineCount > 0 || SmtpAttachment.attachCount > 0) { con.SendCommand("--#SEPERATOR1#--"); } con.SendCommand("."); con.GetReply(out response, out code); con.SendCommand("QUIT"); con.GetReply(out response, out code); con.Close(); con = null; if (OnMailSent != null) { OnMailSent(); } } }