Created
April 15, 2009 22:15
-
-
Save OJ/96065 to your computer and use it in GitHub Desktop.
A large pile of shit that I recently discovered in an open source code base from CodePlex.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment