private void RelayMessage() { try { if(!m_RelayTable.Contains(Thread.CurrentThread)){ SCore.WriteLog(m_pServer.m_SartUpPath + "mailServiceError.log","RelayMails: params missing"); return; } string messageFile = m_RelayTable[Thread.CurrentThread].ToString(); bool sendOk = false; using(FileStream fs = File.Open(messageFile,FileMode.Open,FileAccess.ReadWrite)){ // Get relay info RelayInfo relayInf = new RelayInfo(fs,1,1); string from = relayInf.From; if(from.Length == 0){ from = relayInf.To; } SMTP_Client smtpClnt = new SMTP_Client(); smtpClnt.UseSmartHost = UseSmartHost; smtpClnt.SmartHost = SmartHost; smtpClnt.DnsServers = new string[]{Dns1,Dns2}; sendOk = smtpClnt.Send(new string[]{relayInf.To},from,fs); } if(sendOk){ // Message sended successfuly, may delete it File.Delete(messageFile); } // send failed else{ // Move message to Retry folder string msgFileName = Path.GetFileName(messageFile); File.Move(messageFile,m_pServer.m_MailStorePath + "Retry\\" + msgFileName); } } catch(Exception x){ if(!(x is IOException)){ Error.DumpError(x,new System.Diagnostics.StackTrace()); } } finally{ RemoveThread(Thread.CurrentThread); } }
private void SendRetryMail() { try { if(!m_RelayTable.Contains(Thread.CurrentThread)){ SCore.WriteLog(m_pServer.m_SartUpPath + "mailServiceError.log","SendRetryMail: params missing"); return; } string messageFile = m_RelayTable[Thread.CurrentThread].ToString(); using(FileStream fs = File.Open(messageFile,FileMode.Open,FileAccess.ReadWrite)){ // Get relay info RelayInfo relayInf = new RelayInfo(fs,RelayUndelivered,RelayUndelWarning); string from = relayInf.From; if(from.Length == 0){ from = relayInf.To; } SMTP_Client smtpClnt = new SMTP_Client(); smtpClnt.UseSmartHost = UseSmartHost; smtpClnt.SmartHost = SmartHost; smtpClnt.DnsServers = new string[]{Dns1,Dns2}; if(smtpClnt.Send(new string[]{relayInf.To},from,fs)){ fs.Close(); // Message sended successfuly, may delete it File.Delete(messageFile); } // send failed else{ string error = smtpClnt.Errors[0].ErrorText; // If destination recipient is invalid or Undelivered Date Exceeded, try to return message to sender if(smtpClnt.Errors[0].ErrorType != SMTP_ErrorType.UnKnown || relayInf.IsUndeliveredDateExceeded){ MakeUndeliveredNotify(relayInf,error,fs); fs.Close(); // Undelivery note made, may delete message if(relayInf.From.Length > 0){ File.Delete(messageFile); } // There isn't return address, can't send undelivery note else if(StoreUndeliveredMessages){ // Check if Directory exists, if not Create if(!Directory.Exists(m_pServer.m_MailStorePath + "Undelivered\\")){ Directory.CreateDirectory(m_pServer.m_MailStorePath + "Undelivered\\"); } File.Move(messageFile,messageFile.Replace("Retry","Undelivered")); } } else if(relayInf.MustSendWarning){ MakeUndeliveredWarning(relayInf,error,fs); byte[] mustSendWarningBit = System.Text.Encoding.ASCII.GetBytes("1"); fs.Position = relayInf.WarningBitPos; fs.Write(mustSendWarningBit,0,mustSendWarningBit.Length); } } } } catch(Exception x){ if(!(x is IOException)){ Error.DumpError(x,new System.Diagnostics.StackTrace()); } } finally{ RemoveThread(Thread.CurrentThread); } }
/// <summary> /// Creates undelivered warning for user and places it to relay folder. /// </summary> /// <param name="relayInfo">Relay info</param> /// <param name="error">SMTP returned error text.</param> /// <param name="file">Messsage file.</param> private void MakeUndeliveredWarning(RelayInfo relayInfo,string error,Stream file) { try { // If sender isn't specified, we can't send warning to sender. // Just skip warning sending. if(relayInfo.From.Length == 0){ return; } file.Position = relayInfo.MessageStartPos; // Make new message MimeConstructor mime = new MimeConstructor(); mime.From = "postmaster"; mime.To = new string[]{relayInfo.From}; mime.Subject = "Undelivered mail warning"; mime.Attachments.Add(new Attachment("data.eml",file)); string bodyTxt = Relay.UndelWarningTemplate; bodyTxt = bodyTxt.Replace("<#RECEPTIENT>",relayInfo.To); bodyTxt = bodyTxt.Replace("<#ERROR>",error); bodyTxt = bodyTxt.Replace("<#UNDELIVERED_HOURS>",relayInfo.DeviveringForHours.ToString()); mime.Body = bodyTxt; byte[] data = System.Text.Encoding.Default.GetBytes(mime.ConstructMime()); using(MemoryStream strm = new MemoryStream(data)){ m_pServer.ProcessAndStoreMessage("",new string[]{relayInfo.From},strm); } // byte[] data = System.Text.Encoding.Default.GetBytes(mime.ConstructMime()); // MailStore.StoreMessage("",new MemoryStream(data),relayInfo.From,"",true); } catch(Exception x) { Error.DumpError(x,new System.Diagnostics.StackTrace()); } }