///////////////////////////////////////////////////////////////////// // Example Two: Secure transmission and no password authentication. // Message content is HTML. // The HTML has an embedded image. The image is sent as an inline // attachment. This message is a two parts message. It has two related // parts, the html part and the inline image attachment part. // The two parts are separated by SecureSmtpMultipart boundary. ///////////////////////////////////////////////////////////////////// private void ExampleSecureHtml ( string Host, string UserName, string UserPassword, string FromName, string FromAddress, string ToName, string ToAddress, string MessageSubject, string MessageHtml ) { try { // create smtp client if (ConnectionSecureHtml == null) { ConnectionSecureHtml = new SecureSmtpClient(ConnectMethod.Secure, Host, UserName, UserPassword); } // create mail message object SecureSmtpMessage Message = new SecureSmtpMessage(); // Set subject Message.Subject = MessageSubject; // Set mail from address and display name. Message.From = new MailAddress(FromAddress, FromName); // Add minimum one or more recipients. // in addition you can add CC or BCC recipients. Message.To.Add(new MailAddress(ToAddress, ToName)); // This message has two related parts, the html part and the inline image attachment part. // The two parts are separated by SecureSmtpMultipart boundary. // the multipart boundary is the root part. SecureSmtpMultipart Related = new SecureSmtpMultipart(MultipartType.Related); Message.RootPart = Related; // The first part is the html content. // It is added to array of part content children of the multipart parent. SecureSmtpContent HtmlContent = new SecureSmtpContent(ContentType.Html, MessageHtml); Related.AddPart(HtmlContent); // The second part is the inline image attachement. SecureSmtpAttachment ImageAttachment = new SecureSmtpAttachment(AttachmentType.Inline, "EmailImage.png", "IMAGE001"); ImageAttachment.MediaType = "image/png"; Related.AddPart(ImageAttachment); // send mail ConnectionSecureHtml.SendMail(Message); MessageBox.Show("Email was successfully sent"); } // catch exceptions catch (Exception Exp) { MessageBox.Show(Exp.Message); } return; }
///////////////////////////////////////////////////////////////////// // Example Three: Secure transmission and password authentication. // This example is specific to GMail. // The message is a multi-part message. At the root level it is a // mixed part message with two mixed parts: content and file // attachment. // The content part is divided into two alternate parts: HTML content // and plain text content. // The HTML part is divided into two related parts: the HTML content // and an embedded image. The image is sent as an inline attachment. ///////////////////////////////////////////////////////////////////// private void ExampleGMail ( string Host, string UserName, string RefreshToken, string FromName, string FromAddress, string ToName, string ToAddress, string MessageSubject, string MessageHtml ) { try { // create smtp client if (ConnectionGMail == null) { ConnectionGMail = new SecureSmtpClient(Host, UserName, new SecureSmtpOAuth2(RefreshToken)); } // create mail message object SecureSmtpMessage Message = new SecureSmtpMessage(); // Set subject Message.Subject = MessageSubject; // Set mail from address and display name. Message.From = new MailAddress(FromAddress, FromName); // Add minimum one or more recipients. // in addition you can add CC or BCC recipients. Message.To.Add(new MailAddress(ToAddress, ToName)); // This message is made of two alternative methods for content plus a attachment file. // create mixed multipart boundary SecureSmtpMultipart Mixed = new SecureSmtpMultipart(MultipartType.Mixed); Message.RootPart = Mixed; // create alternative boundary // there are two alternatives to the content: plain text and HTML. // NOTE: The recipient of the message will display the last part it knows how to display. // For email program that can handle both HTML and text make sure the HTML part is last. SecureSmtpMultipart Alternative = new SecureSmtpMultipart(MultipartType.Alternative); Mixed.AddPart(Alternative); // Add plain text mail body contents. SecureSmtpContent PlainTextContent = new SecureSmtpContent(ContentType.Plain, PlainTextView); Alternative.AddPart(PlainTextContent); // The HTML alternative has two related parts. The HTML content and an inline image attachement // create related boundary SecureSmtpMultipart Related = new SecureSmtpMultipart(MultipartType.Related); Alternative.AddPart(Related); // Add html mail body content. SecureSmtpContent HtmlContent = new SecureSmtpContent(ContentType.Html, HtmlView); Related.AddPart(HtmlContent); // Add inline image attachment. // NOTE image id is set to IMAGE001 this id must match the html image id in HtmlView text. SecureSmtpAttachment ImageAttachment = new SecureSmtpAttachment(AttachmentType.Inline, "EmailImage.png", "IMAGE001"); ImageAttachment.MediaType = "image/png"; Related.AddPart(ImageAttachment); // Add file attachment to the email. // The recipient of the email will be able to save it as a file. SecureSmtpAttachment PdfAttachment = new SecureSmtpAttachment(AttachmentType.Attachment, "rfc2045.pdf"); Mixed.AddPart(PdfAttachment); // send mail ConnectionGMail.SendMail(Message); MessageBox.Show("Email was successfully sent"); } // catch exceptions catch (Exception Exp) { MessageBox.Show(Exp.Message); } return; }
///////////////////////////////////////////////////////////////////// // Send Mail prototype. // This method should be used as a prototype for your email // sending method. ///////////////////////////////////////////////////////////////////// private void OnSendEmail(object sender, EventArgs e) { try { // read screen parameters TestParam Param = ReadScreen(); // create one of three possible connection classes // the SecureSmtpClient is re-useable. If you send more than one email, // you can reuse the class. It is of real benefit for gmail. if (Connection != null && Param.CompareTo(LastParam) != 0) { Connection = null; } if (Connection == null) { switch (Param.Type) { case 0: Connection = new SecureSmtpClient(Param.Host, Param.UserName, new SecureSmtpOAuth2(Param.RefreshToken)); break; case 1: Connection = new SecureSmtpClient(ConnectMethod.Secure, Param.Host, Param.UserName, Param.UserPassword); break; case 2: Connection = new SecureSmtpClient(ConnectMethod.Unsecure, Param.Host, Param.UserName, Param.UserPassword); break; } Connection.PortNo = Param.Port; Connection.Timeout = Param.Timeout; LastParam = Param; } // create mail message object SecureSmtpMessage Message = new SecureSmtpMessage(); // Set subject Message.Subject = Subject; // Set mail from address and display name. Message.From = new MailAddress(Param.FromAddress, Param.FromName); // Add minimum one or more recipients. Message.To.Add(new MailAddress(Param.ToAddress, Param.ToName)); // create mixed multipart boundary SecureSmtpMultipart Mixed = new SecureSmtpMultipart(MultipartType.Mixed); Message.RootPart = Mixed; // create alternative boundary SecureSmtpMultipart Alternative = new SecureSmtpMultipart(MultipartType.Alternative); Mixed.AddPart(Alternative); // Add plain text mail body contents. SecureSmtpContent PlainTextContent = new SecureSmtpContent(ContentType.Plain, PlainTextView); Alternative.AddPart(PlainTextContent); // create related boundary SecureSmtpMultipart Related = new SecureSmtpMultipart(MultipartType.Related); Alternative.AddPart(Related); // add html mail body content SecureSmtpContent HtmlContent = new SecureSmtpContent(ContentType.Html, HtmlView); Related.AddPart(HtmlContent); // add inline image attachment. // NOTE image id is set to IMAGE001 this id must match the html image id in HtmlView text. SecureSmtpAttachment ImageAttachment = new SecureSmtpAttachment(AttachmentType.Inline, "EmailImage.png", "IMAGE001"); ImageAttachment.MediaType = "image/png"; Related.AddPart(ImageAttachment); // add file attachment to the email. // The recipient of the email will be able to save it as a file. SecureSmtpAttachment PdfAttachment = new SecureSmtpAttachment(AttachmentType.Attachment, "rfc2045.pdf"); Mixed.AddPart(PdfAttachment); // send mail Connection.SendMail(Message); MessageBox.Show("Email was successfully sent"); } // catch exceptions catch (Exception Exp) { MessageBox.Show(Exp.Message); } return; }