static void ArgumentBasedMail(string jsoninputs) { //Console.WriteLine("Output: " + jsoninputs); MailInputs MailData = Program.DeserializeJson(jsoninputs); Program.SendMailMime(MailData); }
static MailInputs DeserializeJson(string jsontext) { Console.WriteLine("De-serializing..."); Console.WriteLine(jsontext.Length.ToString()); MailInputs maildata = JsonConvert.DeserializeObject <MailInputs>(jsontext.Replace("\"", "\'")); Console.WriteLine("De-serialization completed: " + maildata.To); return(maildata); }
static async Task FileBasedMail() { string text = await ReadAndDisplayFilesAsync(); Console.WriteLine("Output: " + text); MailInputs MailData = Program.DeserializeJson(text); Program.SendMailMime(MailData); //DeserializeJsonTest(); }
static void SendMailMime(MailInputs maildata) { try { //--------------------- // Configuration // User setup string User = maildata.User; string UserName = maildata.User; string pssw = maildata.Password; // Server setup string server = maildata.Server; int port = Convert.ToInt32(maildata.Port); // Mail setup string attachment = maildata.Attachment; string MailBody = maildata.Body; string MailSubject = maildata.Subject; string toAddressString = maildata.To; string ccAddressString = maildata.Cc; string bccAddressString = maildata.Bcc; //-------------------- MimeMessage message = new MimeMessage(); MailboxAddress from = new MailboxAddress(UserName, User); message.From.Add(from); if (!string.IsNullOrEmpty(toAddressString)) { InternetAddressList toList = new InternetAddressList(); toList.AddRange(MimeKit.InternetAddressList.Parse(toAddressString)); message.To.AddRange(toList); } if (!string.IsNullOrEmpty(ccAddressString)) { InternetAddressList ccList = new InternetAddressList(); ccList.AddRange(MimeKit.InternetAddressList.Parse(ccAddressString)); message.Cc.AddRange(ccList); } if (!string.IsNullOrEmpty(bccAddressString)) { InternetAddressList bccList = new InternetAddressList(); bccList.AddRange(MimeKit.InternetAddressList.Parse(bccAddressString)); message.Bcc.AddRange(bccList); } //Mail body BodyBuilder bodyBuilder = new BodyBuilder(); //bodyBuilder.HtmlBody = "<h1>This is a mail body</h1>"; message.Subject = MailSubject; if (!string.IsNullOrEmpty(MailBody)) { bodyBuilder.TextBody = MailBody; } // if (!string.IsNullOrEmpty(attachment)) { bodyBuilder.Attachments.Add(attachment); } message.Body = bodyBuilder.ToMessageBody(); SmtpClient client = new SmtpClient(); client.Connect(server, port, SecureSocketOptions.None); client.Authenticate(User, pssw); client.Send(message); client.Disconnect(true); client.Dispose(); Console.WriteLine(""); //Console.ReadLine(); //............................ } catch (Exception ep) { Console.WriteLine("failed to send email with the following error:"); Console.WriteLine(ep.Message); Console.ReadLine(); } }