public void TestFileFromString() { EmailMessage emailmessage = new EmailMessage(); emailmessage.FromAddress = TestAddressHelper.GetFromAddress(); emailmessage.AddToAddress(TestAddressHelper.GetToAddress()); emailmessage.Subject = "Here's your license"; emailmessage.TextPart = new TextAttachment("This is a license.\r\n\r\n" + "We will spend your money on a new plasma TV."); emailmessage.HtmlPart = new HtmlAttachment("<html><body>" + "<p>This is a license.</p>\r\n" + "<p>We will spend your money on a new <i>plasma TV</i>.</p>\r\n" + "</body></html>"); String content = "This is String Line 1\r\nThis is String Line 2"; FileAttachment fileAttachment = new FileAttachment(content); //fileAttachment.ContentType fileAttachment.FileName = "License.txt"; fileAttachment.CharSet = System.Text.Encoding.ASCII; fileAttachment.ContentType = "text/plain"; fileAttachment.Encoding = DotNetOpenMail.Encoding.EncodingType.SevenBit; emailmessage.AddMixedAttachment(fileAttachment); //emailmessage.Send(TestAddressHelper.GetSmtpServer()); }
public void TestThree() { EmailMessage emailmessage = new EmailMessage(); emailmessage.FromAddress = TestAddressHelper.GetFromAddress(); emailmessage.AddToAddress(TestAddressHelper.GetToAddress()); emailmessage.Subject = "Something has come up"; emailmessage.TextPart = new TextAttachment("I regret that something has " + "come up unexpectedly,\r\n" + "and I must postpone our meeting.\r\n\r\n" + "Please read the 20 pages of my thoughts on this in the attached\r\n" + "PDF file.\r\n\r\n-Marcel"); emailmessage.HtmlPart = new HtmlAttachment("<p>I regret that something " + "has come up unexpectedly,\r\n" + "and I must postpone our meeting.</p>\r\n" + "<p>Please read the 20 pages of my thoughts on this in the attached\r\n" + "PDF file.</p>\r\n<p>-Marcel</p>"); FileAttachment fileattachment = new FileAttachment(new FileInfo(@"..\..\TestFiles\grover.jpg")); fileattachment.ContentType = "image/jpeg"; emailmessage.AddMixedAttachment(fileattachment); emailmessage.Send(TestAddressHelper.GetSmtpServer()); }
public void TestLargerBinaryFileFromStream() { String filename = "casingTheJoint.jpg"; EmailMessage emailmessage = new EmailMessage(); emailmessage.FromAddress = TestAddressHelper.GetFromAddress(); emailmessage.AddToAddress(TestAddressHelper.GetToAddress()); emailmessage.Subject = "Here's your file"; emailmessage.TextPart = new TextAttachment("This a zip file."); emailmessage.HtmlPart = new HtmlAttachment("<html><body>" + "<p>This a zip file.</p>\r\n"); FileInfo fileinfo = new FileInfo(@"..\..\TestFiles\" + filename); FileStream filestream = fileinfo.OpenRead(); MemoryStream stream = new MemoryStream(); StreamWriter sw = new StreamWriter(stream); sw.Flush(); //BinaryReader br=new BinaryReader(stream); BinaryReader br = new BinaryReader(filestream); byte[] bytes = br.ReadBytes((int)fileinfo.Length); br.Close(); FileAttachment fileAttachment = new FileAttachment(bytes); //fileAttachment.ContentType fileAttachment.FileName = filename; fileAttachment.ContentType = "application/zip"; emailmessage.AddMixedAttachment(fileAttachment); emailmessage.Send(TestAddressHelper.GetSmtpServer()); }
public void TestFileFromStream() { EmailMessage emailmessage = new EmailMessage(); emailmessage.FromAddress = TestAddressHelper.GetFromAddress(); emailmessage.AddToAddress(TestAddressHelper.GetToAddress()); emailmessage.Subject = "Here's your license"; emailmessage.TextPart = new TextAttachment("This is a license.\r\n\r\n" + "We will spend your money on a new plasma TV."); emailmessage.HtmlPart = new HtmlAttachment("<html><body>" + "<p>This is a license.</p>\r\n" + "<p>We will spend your money on a new <i>plasma TV</i>.</p>\r\n" + "</body></html>"); MemoryStream stream = new MemoryStream(); StreamWriter sw = new StreamWriter(stream); sw.WriteLine("this is some test data 1"); sw.WriteLine("this is some test data 2"); sw.WriteLine("this is some test data 3"); sw.WriteLine("this is some test data 4"); sw.Flush(); stream.Seek(0, SeekOrigin.Begin); //BinaryReader br=new BinaryReader(stream); FileAttachment fileAttachment = new FileAttachment(new StreamReader(stream)); //fileAttachment.ContentType fileAttachment.FileName = "License.txt"; fileAttachment.CharSet = System.Text.Encoding.ASCII; fileAttachment.ContentType = "text/plain"; emailmessage.AddMixedAttachment(fileAttachment); //emailmessage.Send(TestAddressHelper.GetSmtpServer()); }
public void TestDocFile() { EmailMessage mail = new EmailMessage(); FileInfo fileinfo = new FileInfo(@"..\..\TestFiles\TestWord.doc"); Assert.IsTrue(fileinfo.Exists); FileAttachment fileAttachment = new FileAttachment(fileinfo); fileAttachment.ContentType = "application/msword"; //EmailAddress emailAddress = new EmailAddress(emailAddressParam); mail.TextPart = new TextAttachment("Here is your file"); mail.AddMixedAttachment(fileAttachment); mail.FromAddress = TestAddressHelper.GetFromAddress(); mail.AddToAddress("*****@*****.**"); SmtpServer smtpServer = TestAddressHelper.GetSmtpServer(); mail.Send(smtpServer); }
public void TestRelatedAndMixedAttachments() { EmailMessage emailmessage = new EmailMessage(); emailmessage.FromAddress = TestAddressHelper.GetFromAddress(); emailmessage.AddToAddress(TestAddressHelper.GetToAddress()); emailmessage.Subject = "EmailMessageTests Test HTML+Text+Graphic"; emailmessage.TextPart = new TextAttachment("This\r\nis the\r\ntext\r\npart."); emailmessage.HtmlPart = new HtmlAttachment("<html><body>This<br> \r\nis the<br><img src=\"cid:mycontentid@rhombus\"> \r\n<strong>HTML</strong><br>\r\npart.</body></html>"); FileInfo relatedfileinfo = new FileInfo(@"..\..\TestFiles\grover.jpg"); FileInfo mixedfileinfo = new FileInfo(@"..\..\TestFiles\groverUpsideDown.jpg"); Assert.IsTrue(relatedfileinfo.Exists); Assert.IsTrue(mixedfileinfo.Exists); FileAttachment relatedfileattachment = new FileAttachment(relatedfileinfo, "mycontentid@rhombus"); relatedfileattachment.ContentType = "image/jpeg"; FileAttachment mixedfileattachment = new FileAttachment(mixedfileinfo); mixedfileattachment.ContentType = "image/jpeg"; emailmessage.AddRelatedAttachment(relatedfileattachment); emailmessage.AddMixedAttachment(mixedfileattachment); //emailmessage.Send(_smtpserver); String content = emailmessage.ToDataString(); StringReader sr = new StringReader(content); int i = 0; String line = null; bool hasMimeVersion = false; bool hasMixedHeader = false; bool mixedHeaderComesFirst = false; bool hasMultipartAlternative = false; bool hasPlainText = false; bool hasHtmlText = false; bool hasAttachment = false; bool hasRelatedHeader = false; int quotedPrintableParts = 0; String expectedToAddress = TestAddressHelper.GetToAddress().Name + " <" + TestAddressHelper.GetToAddress().Email + ">"; //log.Debug("To Address is "+expectedToAddress); while ((line = sr.ReadLine()) != null) { i++; if (line.IndexOf("Content-Type: multipart/mixed") == 0) { hasMixedHeader = true; if (!hasMultipartAlternative && !hasRelatedHeader) { mixedHeaderComesFirst = true; } } if (line.IndexOf("MIME-Version: 1.0") == 0) { hasMimeVersion = true; } if (line.IndexOf("Content-Type: multipart/alternative;") == 0) { hasMultipartAlternative = true; } if (line.IndexOf("Content-Type: text/html") == 0) { hasHtmlText = true; } if (line.IndexOf("Content-Type: text/plain") == 0) { hasPlainText = true; } if (line.IndexOf("Content-Type: image/jpeg") == 0) { hasAttachment = true; } if (line.IndexOf("Content-Transfer-Encoding: quoted-printable") == 0) { quotedPrintableParts++; } if (line.IndexOf("/9j/4AAQSkZJRgABAQEASABIAAD/") == 0) { hasAttachment = true; } if (line.IndexOf("Content-Type: multipart/related") >= 0) { hasRelatedHeader = true; } //log.Debug("Line "+i+": "+line); } Assert.IsTrue(hasMixedHeader, "Missing multipart/mixed header"); Assert.IsTrue(hasMimeVersion, "Missing Mime Version header"); Assert.IsTrue(mixedHeaderComesFirst, "The mixed header should come first"); Assert.IsTrue(hasMultipartAlternative, "Missing Mime Multipart/Alternative setting"); Assert.IsTrue(hasPlainText, "Missing Plain Text"); Assert.IsTrue(hasHtmlText, "Missing HTML"); Assert.IsTrue(hasAttachment, "Missing the base64 Attachment"); Assert.IsTrue(hasRelatedHeader, "Missing the related header."); Assert.AreEqual(2, quotedPrintableParts, "Expected 2 quoted printable declarations, found " + quotedPrintableParts); }
public ReturnValue SentInvoiceEmail(int invoiceId) { ReturnValue _result = new ReturnValue(); string MailContent = ""; string _emailTo = ""; System.Globalization.NumberFormatInfo nfi = Utilities.CurrentNumberFormat; Model.TProgram_EmailTF _tProgram_Email = new TProgram_EmailTF(); try { #region generate Email Content TCustomer _tCustomer = new TCustomer(); _result = _tCustomer.getCustomerByInvoiceId(invoiceId); if (_result.Success == false) { return(_result); } _tCustomer = _result.Object as TCustomer; _result = _tProgram_Email.getEmailTemplate("ShipInvoices"); if (_result.Success == false) { return(_result); } _tProgram_Email = _result.Object as Model.TProgram_EmailTF; string _mailHtmlContent = System.Web.HttpUtility.HtmlDecode(System.Web.HttpUtility.HtmlDecode(_tProgram_Email.FullHtml)); MailContent = _mailHtmlContent.Replace("[CustomerName]", _tCustomer.FirstName); EmailMessage _mail = new EmailMessage(); _mail.HtmlPart = new HtmlAttachment(MailContent); _mail.FromAddress = new EmailAddress(_tProgram_Email.RespondTo); _mail.Subject = _tProgram_Email.Subject; #endregion #region setup EmailMessage if (Convert.ToBoolean(System.Configuration.ConfigurationSettings.AppSettings["IsTestMode"].ToString()) == true) { string[] maillist = Common.TestMailTo.Split(';'); foreach (string _item in maillist) { _mail.AddToAddress(new EmailAddress(_item)); } _emailTo = Common.TestMailTo; } else { if (string.IsNullOrEmpty(_tCustomer.Email) == true) { _result.Success = false; _result.ErrMessage = "Email To Address is empty"; return(_result); } else { string[] bcclist = _tCustomer.Email.Split(';'); foreach (string _item in bcclist) { if (string.IsNullOrEmpty(_item) == false) { _mail.AddToAddress(new EmailAddress(_item)); } } } _emailTo = _tCustomer.Email; } if (string.IsNullOrEmpty(_tProgram_Email.BccAddress) == false) { string[] bcclist = _tProgram_Email.BccAddress.Split(';'); foreach (string _item in bcclist) { if (string.IsNullOrEmpty(_item) == false) { _mail.AddBccAddress(new EmailAddress(_item)); } } } if (string.IsNullOrEmpty(_tProgram_Email.CCAddress) == false) { string[] bcclist = _tProgram_Email.CCAddress.Split(';'); foreach (string _item in bcclist) { if (string.IsNullOrEmpty(_item) == false) { _mail.AddCcAddress(new EmailAddress(_item)); } } } if (string.IsNullOrEmpty(_tCustomer.InvoiceEmail) == false) { _mail.AddCcAddress(new EmailAddress(_tCustomer.InvoiceEmail)); } if (string.IsNullOrEmpty(_tCustomer.SalesRepEmail) == false) { _mail.AddBccAddress(new EmailAddress(_tCustomer.SalesRepEmail)); } #endregion #region attached pdf string pdffilename = ""; TecnifibreInvoicePDF TecnifibreInvoicePDF = new TecnifibreInvoicePDF(); _result = TecnifibreInvoicePDF.PrintInvoice(invoiceId); if (_result.Success == false) { return(_result); } pdffilename = "Invoice/TFInvoice_" + invoiceId.ToString() + ".pdf"; FileAttachment fileAttachment = new FileAttachment(new FileInfo(pdffilename)); fileAttachment.ContentType = "application/pdf"; _mail.AddMixedAttachment(fileAttachment); #endregion DotNetOpenMail.SmtpAuth.SmtpAuthToken token = new DotNetOpenMail.SmtpAuth.SmtpAuthToken( System.Configuration.ConfigurationSettings.AppSettings["SMTPUserName"], System.Configuration.ConfigurationSettings.AppSettings["SMTPPassword"]); SmtpServer server = new SmtpServer(System.Configuration.ConfigurationSettings.AppSettings["SMTPServer"]); server.SmtpAuthToken = token; if (_mail.Send(server) == false) { _result.Success = false; } } catch (Exception ex) { _result.Success = false; _result.ErrMessage = ex.ToString(); } #region Log App_Log_Mail App_Log_Mail _app_Log_Mail = new App_Log_Mail(); // _app_Log_Mail.ProgramId = this.Owner.DefaultProgram.ProgramId; _app_Log_Mail.IsTest = Convert.ToBoolean(System.Configuration.ConfigurationSettings.AppSettings["IsTestMode"].ToString()); _app_Log_Mail.CreatedOn = System.DateTime.Now; _app_Log_Mail.AddressFrom = _tProgram_Email.RespondTo; _app_Log_Mail.AddressTo = _emailTo; _app_Log_Mail.AddressBcc = _tProgram_Email.BccAddress; _app_Log_Mail.Subject = _tProgram_Email.Subject; _app_Log_Mail.Content = MailContent; _app_Log_Mail.OId = invoiceId.ToString(); _app_Log_Mail.Success = _result.Success; _app_Log_Mail.Notes = _result.ErrMessage; _app_Log_Mail.Type = "ShippingInvoice"; _app_Log_Mail.Save(); #endregion return(_result); }
public static ReturnValue SentEmail(TProgram_Email tProgram_Email, EmailFormat emailFormat, int oId, FileAttachment fileAttachment) { ReturnValue _result = new ReturnValue(); bool IsTFTestMode = Convert.ToBoolean(System.Configuration.ConfigurationSettings.AppSettings["IsTFTestMode"].ToString()); if (tProgram_Email.ContentStatusId != 1) { return(_result); } EmailMessage _mail = new EmailMessage(); if (string.IsNullOrEmpty(tProgram_Email.RespondToName) == true) { _mail.FromAddress = new EmailAddress(tProgram_Email.RespondTo); } else { _mail.FromAddress = new EmailAddress(tProgram_Email.RespondTo, tProgram_Email.RespondToName); } _mail.Subject = tProgram_Email.Subject; if (emailFormat == EmailFormat.Html || emailFormat == EmailFormat.Both) { _mail.HtmlPart = new HtmlAttachment(tProgram_Email.FullHtml); } if (emailFormat == EmailFormat.Text || emailFormat == EmailFormat.Both) { _mail.TextPart = new TextAttachment(tProgram_Email.FullText); } string _emailto = tProgram_Email.ToAddress; if (IsTFTestMode == true) { if (string.IsNullOrEmpty(Common.TestMailTo) == false) { string[] ccList = Common.TestMailTo.Split(';'); foreach (string ccAddress in ccList) { _mail.AddToAddress(new EmailAddress(ccAddress)); } _emailto = Common.TestMailTo; } } else { if (string.IsNullOrEmpty(tProgram_Email.CCAddress) == false) { string[] ccList = tProgram_Email.CCAddress.Split(';'); foreach (string ccAddress in ccList) { if (string.IsNullOrEmpty(ccAddress.Trim()) == false) { _mail.AddCcAddress(new EmailAddress(ccAddress)); } } } if (string.IsNullOrEmpty(tProgram_Email.BccAddress) == false) { string[] BccList = tProgram_Email.BccAddress.Split(';'); foreach (string bccAddress in BccList) { if (string.IsNullOrEmpty(bccAddress.Trim()) == false) { _mail.AddBccAddress(new EmailAddress(bccAddress)); } } } if (string.IsNullOrEmpty(tProgram_Email.ToAddress) == false) { string[] ToAddressList = tProgram_Email.ToAddress.Split(';'); foreach (string toAddress in ToAddressList) { if (string.IsNullOrEmpty(toAddress.Trim()) == false) { _mail.AddToAddress(new EmailAddress(toAddress)); } } } } if (fileAttachment != null) { _mail.AddMixedAttachment(fileAttachment); } App_Log_Mail _mailLog = new App_Log_Mail(); try { SmtpServer server = new SmtpServer(System.Configuration.ConfigurationSettings.AppSettings["SMTPServer"].ToString(), 25); DotNetOpenMail.SmtpAuth.SmtpAuthToken SmtpAuthToken = new DotNetOpenMail.SmtpAuth.SmtpAuthToken(System.Configuration.ConfigurationSettings.AppSettings["SMTPUserName"].ToString(), System.Configuration.ConfigurationSettings.AppSettings["SMTPPassword"].ToString()); server.SmtpAuthToken = SmtpAuthToken; bool _success = _mail.Send(server); _mailLog.Success = _success; _mailLog.Notes = ""; } catch (Exception ex) { _mailLog.Success = false; _mailLog.Notes = ex.ToString(); } finally { _mailLog.AddressBcc = "AddressBcc:" + (string.IsNullOrEmpty(tProgram_Email.BccAddress) ? "" : tProgram_Email.BccAddress) + " CCAddress:" + (string.IsNullOrEmpty(tProgram_Email.CCAddress) ? "" : tProgram_Email.CCAddress); _mailLog.AddressFrom = tProgram_Email.RespondTo; _mailLog.AddressTo = _emailto; if (emailFormat == EmailFormat.Html || emailFormat == EmailFormat.Both) { _mailLog.Content = tProgram_Email.FullHtml; } if (emailFormat == EmailFormat.Text || emailFormat == EmailFormat.Both) { _mailLog.Content = tProgram_Email.FullText; } _mailLog.CreatedOn = System.DateTime.Now; _mailLog.Subject = tProgram_Email.Subject; _mailLog.Type = tProgram_Email.EmailTypeId.ToString(); _mailLog.OId = oId.ToString(); _mailLog.IsTest = IsTFTestMode; _mailLog.Save(); } _result.Success = _mailLog.Success.Value; _result.ErrMessage = _mailLog.Notes; if (string.IsNullOrEmpty(_mailLog.AddressTo) == true) { _result.Success = true; } return(_result); }