Created
September 8, 2021 12:55
-
-
Save glebov21/9ba3836c55e10ec9eb63033134040c66 to your computer and use it in GitHub Desktop.
Send mail
This file contains 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
using System; | |
using System.Text; | |
using System.Net.Mail; | |
using System.IO; | |
using System.Linq; | |
using System.Collections.Generic; | |
using System.Net; | |
using System.Threading; | |
using System.Net.Mime; | |
using System.Diagnostics; | |
namespace WebApplication{ | |
public partial class Default : System.Web.UI.Page | |
{ | |
private string smtpEmail = "samma.ru"; | |
private string smtpPass = ""; | |
private string smtpHost = "localhost"; //amma.ru | |
private int smtpPort = 25; | |
private string smtpFromEmail ="ma.ru"; | |
private bool smtpSSL = false; | |
private WebClient webClient = new WebClient(); | |
public void Page_Init(object o, EventArgs e) | |
{ | |
Response.Charset = "utf-8"; | |
} | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
} | |
private bool InitResponseAndCheckEmail(){ | |
Response.Clear(); | |
Response.ClearHeaders(); | |
Response.AddHeader("Content-Type", "text/plain"); | |
if(string.IsNullOrEmpty(tbEmail.Text)){ | |
Response.Write("Email пустой"); | |
Response.End(); | |
return false; | |
} | |
return true; | |
} | |
private void CompileAndSendResponse(string bodyText, string batPath, string outputFilePath){ | |
System.Threading.Tasks.Task.Run(()=>{ | |
try{ | |
// Run compilation | |
var workingDir = Path.GetDirectoryName(batPath); | |
var startInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c \"" + batPath + "\""); | |
startInfo.WorkingDirectory = workingDir; | |
startInfo.RedirectStandardOutput = true; | |
startInfo.RedirectStandardError = true; | |
startInfo.UseShellExecute = false; | |
System.Diagnostics.Process process = new System.Diagnostics.Process(); | |
process.StartInfo = startInfo; | |
// Remove previous output file | |
if(File.Exists(outputFilePath)) | |
File.Delete(outputFilePath); | |
process.Start(); | |
string output = ""; | |
string error = ""; //! warnings and errors | |
Thread et = new Thread(() => { error = process.StandardError.ReadToEnd(); }); | |
et.Start(); | |
Thread ot = new Thread(() => { output = process.StandardOutput.ReadToEnd(); }); | |
ot.Start(); | |
//ot.Join(); | |
//et.Join(); | |
//process.WaitForExit(); | |
while (!process.HasExited) | |
{ | |
Thread.Sleep(1000); | |
} | |
string pieceOfOutput = output.Substring(Math.Max(0, output.Length - 500)).Replace(workingDir, ""); | |
pieceOfOutput += "\n"; | |
pieceOfOutput += error.Substring(Math.Max(0, error.Length - 500)).Replace(workingDir, ""); | |
this.SendFileAsEmail(tbEmail.Text, (oMail)=>{ | |
string file = outputFilePath; | |
if(File.Exists(file)) | |
{ | |
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet); | |
ContentDisposition disposition = data.ContentDisposition; | |
disposition.CreationDate = System.IO.File.GetCreationTime(file); | |
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file); | |
disposition.ReadDate = System.IO.File.GetLastAccessTime(file); | |
oMail.Attachments.Add(data); | |
oMail.Body = "HEX для windows скомпилирован. Файл прикреплен."; | |
}else{ | |
oMail.Body = "Ошибка компиляции. " + Environment.NewLine + pieceOfOutput; | |
} | |
}); | |
}catch (Exception ex){ | |
try{ | |
this.SendFileAsEmail(tbEmail.Text, (oMail)=>{ | |
//DEBUG: | |
oMail.Body = "Ошибка обработки: " + ex.ToString(); | |
//oMail.Body = "Ошибка обработки: " + ex.Message; | |
}); | |
}catch{} | |
} | |
}); //task | |
Response.Write("Результат после обработки отправится на электронную почту"); | |
Response.End(); | |
} | |
protected void OnRunWinClick(object sender, EventArgs e){ | |
if(!InitResponseAndCheckEmail()) | |
return; | |
CompileAndSendResponse("HEX для windows скомпилирован. Файл прикреплен.", batCompiteWinAppPath, resultWinFilePath); | |
} | |
private void WaitForMakeAppTerminated(string appPath){ | |
while(true){ | |
bool isFoundProc = false; | |
foreach(var proc in Process.GetProcesses()){ | |
if(proc.MainModule != null && string.Equals(Path.GetFullPath(proc.MainModule.FileName),Path.GetFullPath(appPath), StringComparison.OrdinalIgnoreCase)) | |
{ | |
isFoundProc = true; | |
break; | |
} | |
} | |
if(!isFoundProc) | |
break; | |
} | |
} | |
private void SendFileAsEmail(string toEmail, Action<MailMessage> messageFilling){ | |
MailAddress from = new MailAddress(smtpFromEmail, "Autogramma"); | |
MailAddress to = new MailAddress(toEmail); | |
using(MailMessage oMail = new MailMessage(from, to)) | |
{ | |
oMail.Subject = "Аамма: результат компиляции"; | |
SmtpClient smtp = new SmtpClient(); | |
smtp.Host = smtpHost; | |
smtp.UseDefaultCredentials = false; | |
smtp.Credentials = new NetworkCredential(smtpEmail, smtpPass); | |
smtp.EnableSsl = smtpSSL; | |
smtp.Port = smtpPort; | |
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; | |
//587 465 25 | |
if(messageFilling != null) | |
messageFilling(oMail); | |
smtp.Send(oMail); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment