private SmtpMail _createMail() { //For evaluation usage, please use "TryIt" as the license code, otherwise the //"invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then //"trial version expired" exception will be thrown. //For licensed uasage, please use your license code instead of "TryIt", then the object //will never expire SmtpMail mail = new SmtpMail("TryIt"); mail.From = TextBoxFrom.Text; // if from address is different with authenticated user, change from to authenticated user // and change from address to replyto. if (string.Compare(mail.From.Address, _oauthWrapper.OauthProvider.UserEmail, true) != 0) { mail.ReplyTo = mail.From; mail.From = _oauthWrapper.OauthProvider.UserEmail; } mail.To = TextBoxTo.Text; mail.Cc = TextBoxCc.Text; mail.Subject = TextBoxSubject.Text; mail.Charset = _charsets[ComboBoxEncoding.SelectedIndex, 1]; string htmlBody = _buildHtmlBody(); htmlBody = htmlBody.Replace("[$from]", _encodeAddressToHtml(mail.From.ToString())); htmlBody = htmlBody.Replace("[$to]", _encodeAddressToHtml(mail.To.ToString())); htmlBody = htmlBody.Replace("[$subject]", _encodeAddressToHtml(mail.Subject)); mail.ImportHtml(htmlBody, Application.ExecutablePath, ImportHtmlBodyOptions.ImportLocalPictures); int count = _attachments.Count; for (int i = 0; i < count; i++) { //Add attachment mail.AddAttachment(_attachments[i] as string); } //Add digital signature and encrypt email on demanded _signAndEncryptEmail(mail); return(mail); }
public static void SendMailUsingGmailImage(string EmailAddress, string AccessToken, string ToMailAddress, string EmailSubject, string MailBody, List <string> fileName, string Path) { // Gmail SMTP server address SmtpServer oServer = new SmtpServer("smtp.gmail.com"); // enable SSL connection oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; // Using 587 port, you can also use 465 port oServer.Port = 587; // use Gmail SMTP OAUTH 2.0 authentication oServer.AuthType = SmtpAuthType.XOAUTH2; // set user authentication oServer.User = EmailAddress; // oAccountIntegration.EmailAddress; // use access token as password oServer.Password = AccessToken; // oAccountIntegration.AccessToken; SmtpMail oMail = new SmtpMail("TryIt"); // Your Gmail email address oMail.From = EmailAddress;// oAccountIntegration.EmailAddress; // Please change recipient address to yours for test oMail.To = ToMailAddress; oMail.Subject = EmailSubject; oMail.HtmlBody = MailBody; // Import html body and also import linked image as embedded images. oMail.ImportHtml(MailBody, Path, //test.gif is in c:\\my picture ImportHtmlBodyOptions.ImportLocalPictures | ImportHtmlBodyOptions.ImportCss); foreach (var item in fileName) { oMail.AddAttachment(item); } EASendMail.SmtpClient oSmtp = new EASendMail.SmtpClient(); oSmtp.SendMail(oServer, oMail); }
static void Main(string[] args) { SmtpMail oMail = new SmtpMail("TryIt"); SmtpClient oSmtp = new SmtpClient(); // Set sender email address, please change it to yours oMail.From = "*****@*****.**"; // Set recipient email address, please change it to yours oMail.To = "*****@*****.**"; // Set email subject oMail.Subject = "test html email with attachment"; // Your SMTP server address SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net"); // User and password for ESMTP authentication, if your server doesn't require // User authentication, please remove the following codes. oServer.User = "******"; oServer.Password = "******"; // If your SMTP server requires SSL connection, please add this line // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; try { // Import html body and also import linked image as embedded images. oMail.ImportHtml("<html><body>test <img src=\"test.gif\"> importhtml</body></html>", "c:\\my picture", //test.gif is in c:\\my picture ImportHtmlBodyOptions.ImportLocalPictures | ImportHtmlBodyOptions.ImportCss); Console.WriteLine("start to send email with embedded image..."); oSmtp.SendMail(oServer, oMail); Console.WriteLine("email was sent successfully!"); } catch (Exception ep) { Console.WriteLine("failed to send email with the following error:"); Console.WriteLine(ep.Message); } }
private SmtpMail _createMail() { //For evaluation usage, please use "TryIt" as the license code, otherwise the //"invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then //"trial version expired" exception will be thrown. //For licensed uasage, please use your license code instead of "TryIt", then the object //will never expire SmtpMail mail = new SmtpMail("TryIt"); // If you want to specify a reply address // mail.ReplyTo = "reply@mydomain"; // From is a MailAddress object, in c#, it supports implicit converting from string. // The syntax is like this: "*****@*****.**" or "Tester<*****@*****.**>" // The example code without implicit converting // mail.From = new MailAddress( "Tester", "*****@*****.**" ) // mail.From = new MailAddress( "Tester<*****@*****.**>" ) // mail.From = new MailAddress( "*****@*****.**" ) mail.From = TextBoxFrom.Text; // To, Cc and Bcc is a AddressCollection object, in C#, it supports implicit converting from string. // multiple address are separated with (,;) // The syntax is like this: "[email protected], [email protected]" // The example code without implicit converting // mail.To = new AddressCollection( "[email protected], [email protected]" ); // mail.To = new AddressCollection( "Tester1<*****@*****.**>, Tester2<*****@*****.**>"); mail.To = TextBoxTo.Text; // You can add more recipient by Add method // mail.To.Add( new MailAddress( "tester", "*****@*****.**")); mail.Cc = TextBoxCc.Text; mail.Subject = TextBoxSubject.Text; mail.Charset = _charsets[ComboBoxEncoding.SelectedIndex, 1]; string htmlBody = _buildHtmlBody(); htmlBody = htmlBody.Replace("[$from]", _encodeAddressToHtml(mail.From.ToString())); htmlBody = htmlBody.Replace("[$to]", _encodeAddressToHtml(mail.To.ToString())); htmlBody = htmlBody.Replace("[$subject]", _encodeAddressToHtml(mail.Subject)); mail.ImportHtml(htmlBody, Application.ExecutablePath, ImportHtmlBodyOptions.ImportLocalPictures); int count = _attachments.Count; for (int i = 0; i < count; i++) { //Add attachment mail.AddAttachment(_attachments[i] as string); } //Add digital signature and encrypt email on demanded _signAndEncryptEmail(mail); return(mail); }
// It is not recommended, most email providers will reject the email due to anti-spam policy. private SmtpMail _createMailForDirectSend(MailAddress address) { //For evaluation usage, please use "TryIt" as the license code, otherwise the //"invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then //"trial version expired" exception will be thrown. //For licensed uasage, please use your license code instead of "TryIt", then the object //will never expire SmtpMail mail = new SmtpMail("TryIt"); // If you want to specify a reply address // mail.ReplyTo = "reply@mydomain"; // From is a MailAddress object, in c#, it supports implicit converting from string. // The syntax is like this: "*****@*****.**" or "Tester<*****@*****.**>" // The example code without implicit converting // mail.From = new MailAddress( "Tester", "*****@*****.**" ) // mail.From = new MailAddress( "Tester<*****@*****.**>" ) // mail.From = new MailAddress( "*****@*****.**" ) mail.From = TextBoxFrom.Text; // To, Cc and Bcc is a AddressCollection object, in C#, it supports implicit converting from string. // multiple address are separated with (,;) // The syntax is like this: "[email protected], [email protected]" // The example code without implicit converting // mail.To = new AddressCollection( "[email protected], [email protected]" ); // mail.To = new AddressCollection( "Tester1<*****@*****.**>, Tester2<*****@*****.**>"); mail.To.Add(address); mail.Subject = TextBoxSubject.Text; mail.Charset = _charsets[ComboBoxEncoding.SelectedIndex, 1]; // To send email to the recipient directly(simulating the smtp server), // please add a Received header, // otherwise, many anti-spam filter will make it as junk email. SmtpServer server = new SmtpServer(""); System.Globalization.CultureInfo cur = new System.Globalization.CultureInfo("en-US"); string gmtDateTime = DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss zzz", cur); gmtDateTime.Remove(gmtDateTime.Length - 3, 1); string receivedHeader = string.Format("from {0} ([127.0.0.1]) by {0} ([127.0.0.1]) with SMTPSVC;\r\n\t {1}", server.HeloDomain, gmtDateTime); mail.Headers.Insert(0, new HeaderItem("Received", receivedHeader)); string htmlBody = _buildHtmlBody(); htmlBody = htmlBody.Replace("[$from]", _encodeAddressToHtml(mail.From.ToString())); htmlBody = htmlBody.Replace("[$to]", _encodeAddressToHtml(mail.To.ToString())); htmlBody = htmlBody.Replace("[$subject]", _encodeAddressToHtml(mail.Subject)); mail.ImportHtml(htmlBody, Application.ExecutablePath, ImportHtmlBodyOptions.ImportLocalPictures); int count = _attachments.Count; for (int i = 0; i < count; i++) { //Add attachment mail.AddAttachment(_attachments[i] as string); } //Add digital signature and encrypt email on demanded _signAndEncryptEmail(mail); return(mail); }
private void button1_Click(object sender, System.EventArgs e) { if( textFrom.Text.Length == 0 ) { MessageBox.Show( "Please input From!, the format can be [email protected] or Tester<*****@*****.**>" ); return; } if( textTo.Text.Length == 0 && textCc.Text.Length == 0 ) { MessageBox.Show( "Please input To or Cc!, the format can be [email protected] or Tester<*****@*****.**>, please use , or ; to separate multiple recipients" ); return; } btnSend.Enabled = false; btnCancel.Enabled = true; m_bcancel = false; //For evaluation usage, please use "TryIt" as the license code, otherwise the //"invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then //"trial version expired" exception will be thrown. //For licensed usage, please use your license code instead of "TryIt", then the object //will never expire SmtpMail oMail = new SmtpMail("TryIt"); SmtpClient oSmtp = new SmtpClient(); //To generate a log file for SMTP transaction, please use //oSmtp.LogFileName = "c:\\smtp.log"; string err = ""; try { oMail.Reset(); //If you want to specify a reply address //oMail.Headers.ReplaceHeader( "Reply-To: <reply@mydomain>" ); //From is a MailAddress object, in c#, it supports implicit converting from string. //The syntax is like this: "*****@*****.**" or "Tester<*****@*****.**>" //The example code without implicit converting // oMail.From = new MailAddress( "Tester", "*****@*****.**" ) // oMail.From = new MailAddress( "Tester<*****@*****.**>" ) // oMail.From = new MailAddress( "*****@*****.**" ) oMail.From = textFrom.Text; //To, Cc and Bcc is a AddressCollection object, in C#, it supports implicit converting from string. // multiple address are separated with (,;) //The syntax is like this: "[email protected], [email protected]" //The example code without implicit converting // oMail.To = new AddressCollection( "[email protected], [email protected]" ); // oMail.To = new AddressCollection( "Tester1<*****@*****.**>, Tester2<*****@*****.**>"); oMail.To = textTo.Text; //You can add more recipient by Add method // oMail.To.Add( new MailAddress( "tester", "*****@*****.**")); oMail.Cc = textCc.Text; oMail.Subject = textSubject.Text; oMail.Charset = m_arCharset[lstCharset.SelectedIndex,1]; //digital signature and encryption if(!_Signencrypt( ref oMail )) { btnSend.Enabled = true; btnCancel.Enabled = false; return; } string basepath = Application.ExecutablePath; int pos = basepath.LastIndexOfAny( "/\\".ToCharArray() ); if( pos != -1 ) basepath = basepath.Substring( 0, pos ); string body = m_htmlDoc.body.innerHTML; body = body.Replace( "[$from]", _EncodeAddress(oMail.From.ToString())); body = body.Replace( "[$to]", _EncodeAddress(oMail.To.ToString())); body = body.Replace( "[$subject]", _EncodeAddress(oMail.Subject) ); string htmlheader = String.Format( "<html><title>{0}</title><meta HTTP-EQUIV=\"Content-Type\" Content=\"text-html; charset={1}\"><META content=\"MSHTML 6.00.2900.2769\" name=GENERATOR><body>", oMail.Subject, oMail.Charset ); body = body.Insert( 0, htmlheader ); body += "</body></html>"; oMail.ImportHtml( body, Application.ExecutablePath, ImportHtmlBodyOptions.ImportLocalPictures ); int count = m_arAttachment.Count; for( int i = 0; i < count; i++ ) { oMail.AddAttachment( m_arAttachment[i] as string ); } SmtpServer oServer = new SmtpServer( textServer.Text ); oServer.Protocol = (ServerProtocol)lstProtocol.SelectedIndex; if( oServer.Server.Length != 0 ) { if( chkAuth.Checked ) { oServer.User = textUser.Text; oServer.Password = textPassword.Text; } if( chkSSL.Checked ) oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; } else { //To send email to the recipient directly(simulating the smtp server), //please add a Received header, //otherwise, many anti-spam filter will make it as junk email. System.Globalization.CultureInfo cur = new System.Globalization.CultureInfo("en-US"); string gmtdate = System.DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss zzz", cur); gmtdate.Remove( gmtdate.Length - 3, 1 ); string recvheader = String.Format( "from {0} ([127.0.0.1]) by {0} ([127.0.0.1]) with SMTPSVC;\r\n\t {1}", oServer.HeloDomain, gmtdate ); oMail.Headers.Insert( 0, new HeaderItem( "Received", recvheader )); } //Catching the following events is not necessary, //just make the application more user friendly. //If you use the object in asp.net/windows service or non-gui application, //You need not to catch the following events. //To learn more detail, please refer to the code in EASendMail EventHandler region oSmtp.OnIdle += new SmtpClient.OnIdleEventHandler( OnIdle ); oSmtp.OnAuthorized += new SmtpClient.OnAuthorizedEventHandler( OnAuthorized ); oSmtp.OnConnected += new SmtpClient.OnConnectedEventHandler( OnConnected ); oSmtp.OnSecuring += new SmtpClient.OnSecuringEventHandler( OnSecuring ); oSmtp.OnSendingDataStream += new SmtpClient.OnSendingDataStreamEventHandler( OnSendingDataStream ); if( oServer.Server.Length == 0 && oMail.Recipients.Count > 1 ) { //To send email without specified smtp server, we have to send the emails one by one // to multiple recipients. That is because every recipient has different smtp server. _DirectSend( ref oMail, ref oSmtp ); } else { sbStatus.Text = "Connecting ... "; pgSending.Value = 0; oSmtp.SendMail( oServer, oMail ); MessageBox.Show( String.Format( "The message was sent to {0} successfully!", oSmtp.CurrentSmtpServer.Server )); sbStatus.Text = "Completed"; } //If you want to reuse the mail object, please reset the Date and Message-ID, otherwise //the Date and Message-ID will not change. //oMail.Date = System.DateTime.Now; //oMail.ResetMessageID(); //oMail.To = "*****@*****.**"; //oSmtp.SendMail( oServer, oMail ); } catch( SmtpTerminatedException exp ) { err = exp.Message; } catch( SmtpServerException exp ) { err = String.Format( "Exception: Server Respond: {0}", exp.ErrorMessage ); } catch( System.Net.Sockets.SocketException exp ) { err = String.Format( "Exception: Networking Error: {0} {1}", exp.ErrorCode, exp.Message ); } catch( System.ComponentModel.Win32Exception exp ) { err = String.Format( "Exception: System Error: {0} {1}", exp.ErrorCode, exp.Message ); } catch( System.Exception exp ) { err = String.Format( "Exception: Common: {0}", exp.Message ); } if( err.Length > 0 ) { MessageBox.Show( err ); sbStatus.Text = err; } btnSend.Enabled = true; btnCancel.Enabled = false; }