private void sendMails() { //IPAddress smtp_server = IPAddress.Parse("144.16.192.6"); senderDone = false; IPAddress smtp_server = IPAddress.Parse(host); IPAddress redirector = IPAddress.Parse(host); int r_port = 8001; int smtp_port = 25; int response; bool isMsgBody = false; TcpListener listener = new TcpListener(redirector, r_port); listener.Start(); while (true) { Console.WriteLine("Waiting for connections.........."); Socket sockin = listener.AcceptSocket(); Console.WriteLine("got connection from " + sockin.RemoteEndPoint.ToString()); string msg = getResponse64(sockin); char[] delim = new char[1]; delim[0] = '\n'; string[] lines = msg.Split(delim); SendMail mail = new SendMail(); mail.from = lines[0]; mail.to = lines[1]; mail.subject = lines[2]; mail.attachmentCount = Int32.Parse(lines[3]); int i; if(mail.attachmentCount > 0){ mail.attachments = new string[mail.attachmentCount]; for (i = 0; i < mail.attachmentCount; i++) mail.attachments[i] = lines[4 + i]; } mail.body = ""; for (i = 4 + mail.attachmentCount; i < lines.Length; i++) mail.body = mail.body + "\n" + lines[i]; string sreq = "000"; sreq = RijndaelSimple.Encrypt(sreq, passPhrase, saltValue, hashAlgorithm, passwordIterations, initVector, keySize); byte[] req = new byte[1024]; req = Convert.FromBase64String(sreq); sockin.Send(req, 0, req.Length, SocketFlags.None); for (i = 0; i < mail.attachmentCount; i++) { string fullname = "E:\\redirector\\tmp\\" + mail.attachments[i]; FileInfo f = new FileInfo(fullname); FileStream fs = f.Open(FileMode.Create, FileAccess.Write); bool done = false; do { string filepart; filepart = recvMsgPart(sockin); if (filepart.Equals("__done__")) { fs.Close(); done = true; } else { byte[] filebytes = new byte[filepart.Length]; filebytes = Convert.FromBase64String(filepart); fs.Write(filebytes, 0, filebytes.Length); } } while (!done); } System.Net.Mail.MailMessage mailMsg = new System.Net.Mail.MailMessage(); mailMsg.To.Add(mail.to); System.Net.Mail.MailAddress mailAddress = new System.Net.Mail.MailAddress(mail.from); mailMsg.From = mailAddress; mailMsg.Subject = mail.subject; mailMsg.Body = mail.body; for (i = 0; i < mail.attachmentCount; i++) { string fullname = "E:\\redirector\\tmp\\" + mail.attachments[i]; System.Net.Mail.Attachment attchmnt = new System.Net.Mail.Attachment(fullname); mailMsg.Attachments.Add(attchmnt); } SmtpClient smtpClient = new SmtpClient("10.108.7.138", 25); smtpClient.Send(mailMsg); sockin.Close(); } }
private void sendMails() { //IPAddress smtp_server = IPAddress.Parse("144.16.192.6"); senderDone = false; IPAddress smtp_server = IPAddress.Parse(host); IPAddress redirector = IPAddress.Parse(host); int r_port = 8001; int smtp_port = 25; int response; bool isMsgBody = false; TcpListener listener = new TcpListener(redirector, r_port); listener.Start(); while (true) { Console.WriteLine("Waiting for connections.........."); Socket sockin = listener.AcceptSocket(); Console.WriteLine("got connection from " + sockin.RemoteEndPoint.ToString()); string msg = getResponse64(sockin); char[] delim = new char[1]; delim[0] = '\n'; string[] lines = msg.Split(delim); SendMail mail = new SendMail(); mail.from = lines[0]; mail.to = lines[1]; mail.subject = lines[2]; mail.attachmentCount = Int32.Parse(lines[3]); int i; if (mail.attachmentCount > 0) { mail.attachments = new string[mail.attachmentCount]; for (i = 0; i < mail.attachmentCount; i++) { mail.attachments[i] = lines[4 + i]; } } mail.body = ""; for (i = 4 + mail.attachmentCount; i < lines.Length; i++) { mail.body = mail.body + "\n" + lines[i]; } string sreq = "000"; sreq = RijndaelSimple.Encrypt(sreq, passPhrase, saltValue, hashAlgorithm, passwordIterations, initVector, keySize); byte[] req = new byte[1024]; req = Convert.FromBase64String(sreq); sockin.Send(req, 0, req.Length, SocketFlags.None); for (i = 0; i < mail.attachmentCount; i++) { string fullname = "E:\\redirector\\tmp\\" + mail.attachments[i]; FileInfo f = new FileInfo(fullname); FileStream fs = f.Open(FileMode.Create, FileAccess.Write); bool done = false; do { string filepart; filepart = recvMsgPart(sockin); if (filepart.Equals("__done__")) { fs.Close(); done = true; } else { byte[] filebytes = new byte[filepart.Length]; filebytes = Convert.FromBase64String(filepart); fs.Write(filebytes, 0, filebytes.Length); } } while (!done); } System.Net.Mail.MailMessage mailMsg = new System.Net.Mail.MailMessage(); mailMsg.To.Add(mail.to); System.Net.Mail.MailAddress mailAddress = new System.Net.Mail.MailAddress(mail.from); mailMsg.From = mailAddress; mailMsg.Subject = mail.subject; mailMsg.Body = mail.body; for (i = 0; i < mail.attachmentCount; i++) { string fullname = "E:\\redirector\\tmp\\" + mail.attachments[i]; System.Net.Mail.Attachment attchmnt = new System.Net.Mail.Attachment(fullname); mailMsg.Attachments.Add(attchmnt); } SmtpClient smtpClient = new SmtpClient("10.108.7.138", 25); smtpClient.Send(mailMsg); sockin.Close(); } }