private void WriteBody(Stream stream, SmtpMessage message) { var mg = message; if (mg.IsSigned == false) { WriteBodyData(stream, mg); } else { Stream bodyStream = new MemoryStream(); WriteBodyData(bodyStream, mg); var boundary = mg.ContentType.CharsetEncoding.GetBytes("--" + mg.ContentType.Boundary); if (mg.IsEncrypted == true) { var signedStream = new MemoryStream(); this.WriteSignedData(signedStream, mg, bodyStream.ToByteArray(), boundary); this.WriteEncryptedData(stream, mg, signedStream.ToByteArray()); } else { this.WriteSignedData(stream, mg, bodyStream.ToByteArray(), boundary); } } }
private void WriteEncryptedData(Stream stream, SmtpMessage message, Byte[] signedContent) { var mg = message; Stream mm = stream; X509Certificate2Collection encryptionCertificates = new X509Certificate2Collection(); ThrowExceptionIfValueIsNull(mg.From.EncryptionCertificate, "To send an encryted message, the sender must have an encryption certificate specified."); encryptionCertificates.Add(mg.From.EncryptionCertificate); foreach (MailAddress address in mg.To) { ThrowExceptionIfValueIsNull(address.EncryptionCertificate, "To send an encryted message, all receivers( To, CC, and Bcc) must have an encryption certificate specified."); encryptionCertificates.Add(address.EncryptionCertificate); } mm.Write(ByteData.ContentTypeApplicationXpkcs7Mime); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentTransferEncodingIsBase64); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentDispositionAttachmentFileNameIsSmimeP7m); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); Byte[] encrypted = Cryptography.EncryptMessage(signedContent, encryptionCertificates); var converter = new Base64Converter(encrypted.Length); mm.Write(converter.Encode(encrypted)); mm.Write(ByteData.NewLine); }
/// <summary> /// /// </summary> /// <param name="from"></param> /// <param name="message"></param> public SendMailCommand(String from, SmtpMessage message) { this.From = from; List <MailAddress> l = new List <MailAddress>(); l.AddRange(message.To); l.AddRange(message.Cc); l.AddRange(message.Bcc); this.RcptTo.AddRange(l); this.Text = message.GetDataText(); }
/// <summary> /// /// </summary> /// <param name="message"></param> public SendMailCommand(SmtpMessage message) { this.From = this.EnsureFrom(message.From); List <MailAddress> l = new List <MailAddress>(); l.AddRange(message.To); l.AddRange(message.Cc); l.AddRange(message.Bcc); this.RcptTo.AddRange(l); this.Text = message.GetDataText(); }
public static SmtpMessage Create(System.Net.Mail.MailMessage message, SmtpMessageCreateCondition condition) { SmtpMessage mg = new SmtpMessage(); mg.Subject = condition.SubjectFormatter.CreateSubject(message); mg.From = condition.Sender; mg.Sender = condition.Sender; mg.ContentType.CharsetEncoding = message.BodyEncoding; mg.Priority = message.Priority.Cast(); if (condition.BodyFormatter.IsHtml == true) { var html = condition.BodyFormatter.CreateBodyText(message); mg._BodyTextContent.LoadHtml(html); } else { mg.BodyText = condition.BodyFormatter.CreateBodyText(message); } AddMailAddress(message, condition.To, mg.To); AddMailAddress(message, condition.Cc, mg.Cc); AddMailAddress(message, condition.Bcc, mg.Bcc); if (condition.CopyAlternateViews == true) { foreach (var item in message.AlternateViews) { var ct = new SmtpContent(); ct.LoadData(item.ContentStream); ct.ContentTransferEncoding = item.TransferEncoding.Cast(); ct.ContentType.SetProperty(item.ContentType); mg.Contents.Add(ct); } } if (condition.CopyAttachments == true) { foreach (var item in message.Attachments) { var ct = new SmtpContent(); ct.LoadData(item.ContentStream); ct.ContentTransferEncoding = item.TransferEncoding.Cast(); ct.ContentType.SetProperty(item.ContentType); ct.ContentDisposition.SetProperty(item.ContentDisposition); mg.Contents.Add(ct); } } return(mg); }
/// <summary> /// /// </summary> /// <param name="from"></param> /// <param name="message"></param> public SendMailCommand(String from, SmtpMessage message) { this.From = new MailAddress(from); List <MailAddress> l = new List <MailAddress>(); l.AddRange(message.To); l.AddRange(message.Cc); l.AddRange(message.Bcc); this.RcptTo.AddRange(l); MemoryStream mm = new MemoryStream(); var sw = new MimeWriter(mm); sw.Write(message); this.Data = mm.ToArray(); }
public void AddDkimSignatureHeader(SmtpMessage message) { var converter = this.DkimSignatureGenerator; var mg = message; var headers = new List <SmtpMailHeader>(); mg.Headers.Remove(DkimSignatureGenerator.SignatureKey); foreach (SmtpMailHeader header in mg.Headers) { headers.Add(new SmtpMailHeader(header.Key, header.Value)); } //To { var mm = new MemoryStream(); WriteMailAddressList(mm, mg.To); var value = mg.ContentType.CharsetEncoding.GetString(mm.ToArray()); headers.Add(new SmtpMailHeader("To", value)); } //Cc { var mm = new MemoryStream(); WriteMailAddressList(mm, mg.To); var value = mg.ContentType.CharsetEncoding.GetString(mm.ToArray()); headers.Add(new SmtpMailHeader("Cc", value)); } //Content-Type { var mm = new MemoryStream(); WriteEncodedHeader(mm, mg.ContentType); var value = mg.ContentType.CharsetEncoding.GetString(mm.ToArray()); headers.Add(new SmtpMailHeader("Content-Type", value)); } { var mm = new MemoryStream(); WriteBody(mm, mg); var bodyText = mg.ContentType.CharsetEncoding.GetString(mm.ToArray()); var headerValue = converter.GenerateDkimHeaderValue(bodyText); headers.Add(new SmtpMailHeader(DkimSignatureGenerator.SignatureKey, headerValue)); var appendHeaderValue = converter.GenerateSignature(headers); mg.Headers[DkimSignatureGenerator.SignatureKey] = headerValue + appendHeaderValue; } }
private void WriteSignedData(Stream stream, SmtpMessage message, Byte[] body, Byte[] boundary) { var mg = message; Stream mm = stream; ThrowExceptionIfValueIsNull(mg.From.CryptographicKeyInfo.Base64Content, "Can't sign message unless the From property contains a privateKey."); mm.Write(ByteData.ContentTypeMultipartSignedProtocolApplicationXpkcs7Signature); mm.WriteByte(34); // " mm.Write(boundary); mm.WriteByte(34); // " mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); mm.Write(boundary); mm.Write(ByteData.NewLine); mm.Write(body); mm.Write(ByteData.NewLine); mm.Write(boundary); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentTypeApplicationXpkcs7Signature); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentTransferEncodingIsBase64); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentDispositionAttachmentFileNameIsSmimeP7s); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); var signatureBuffer = Cryptography.ToSignMessage(CryptographicBuffer.CreateFromByteArray(body), mg.From.CryptographicKeyInfo); Byte[] signature; CryptographicBuffer.CopyToByteArray(signatureBuffer, out signature); Base64Converter converter = new Base64Converter(body.Length); mm.Write(converter.Encode(signature)); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); mm.Write(boundary); mm.Write(Encoding.UTF8.GetBytes("--")); mm.Write(ByteData.NewLine); }
private void Write(Stream stream, SmtpMessage message) { var mg = message; ThrowExceptionIfValueIsNull(this.HeaderEncoding, "You must set HeaderEncoding property of MimeWriter object."); ThrowExceptionIfValueIsNull(mg.ContentType, "You must set ContentType property of SmtpMessage object."); if (mg.Contents.Count > 0 && String.IsNullOrEmpty(mg.ContentType.Boundary) == true) { mg.ContentType.Boundary = MimeWriter.GenerateBoundary(); } #if !NETFX_CORE if (this.DkimSignatureGenerator != null) { this.AddDkimSignatureHeader(mg); } #endif foreach (var header in mg.Headers) { this.WriteHeader(stream, header.Key, header.Value); } //TO WriteMailAddressList(stream, "To", mg.To); //ReplyTo WriteMailAddressList(stream, "Reply-To", mg.ReplyTo); //Cc WriteMailAddressList(stream, "Cc", mg.Cc); //Bcc never write because blind for others this.WriteEncodedHeader(stream, mg.ContentType); stream.Write(ByteData.NewLine); WriteBody(stream, mg); stream.Write(ByteData.NewLine); stream.WriteByte(46); stream.Write(ByteData.NewLine); }
private void WriteSignedData(Stream stream, SmtpMessage message, Byte[] body, Byte[] boundary) { var mg = message; Stream mm = stream; ThrowExceptionIfValueIsNull(mg.From.SigningCertificate, "Can't sign message unless the From property contains a signing certificate."); mm.Write(ByteData.ContentTypeMultipartSignedProtocolApplicationXpkcs7Signature); mm.WriteByte(34); // " mm.Write(boundary); mm.WriteByte(34); // " mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); byte[] signature = Cryptography.GetSignature(body, mg.From.SigningCertificate, mg.From.EncryptionCertificate); Base64Converter converter = new Base64Converter(signature.Length); mm.Write(ByteData.NewLine); mm.Write(boundary); mm.Write(ByteData.NewLine); mm.Write(body); mm.Write(ByteData.NewLine); mm.Write(boundary); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentTypeApplicationXpkcs7Signature); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentTransferEncodingIsBase64); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentDispositionAttachmentFileNameIsSmimeP7s); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); mm.Write(converter.Encode(signature)); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); mm.Write(boundary); mm.Write(Encoding.UTF8.GetBytes("--")); mm.Write(ByteData.NewLine); }
private void WriteBodyData(Stream stream, SmtpMessage message) { var mg = message; if (mg.Contents.Count == 0) { throw new NotImplementedException(); } else { ThrowExceptionIfValueIsNull(mg.ContentType.CharsetEncoding, "You must set CharsetEncoding property of ContentType property of SmtpMessage object."); var boundary = mg.ContentType.CharsetEncoding.GetBytes("--" + mg.ContentType.Boundary); for (int i = 0; i < mg.Contents.Count; i++) { stream.Write(boundary); stream.Write(ByteData.NewLine); Write(stream, mg.Contents[i]); } stream.Write(mg.ContentType.CharsetEncoding.GetBytes("--" + mg.ContentType.Boundary + "--\r\n")); } }
public static SmtpMessage Create(MailMessage message, SmtpMessageCreateCondition condition) { SmtpMessage mg = new SmtpMessage(); mg.Subject = condition.SubjectFormatter.CreateSubject(message); mg.From = condition.Sender; mg.Sender = condition.Sender; mg.ContentType.SetProperty(message.ContentType); mg.ContentTransferEncoding = message.ContentTransferEncoding; if (condition.BodyFormatter.IsHtml == true) { mg._BodyTextContent.LoadHtml(condition.BodyFormatter.CreateBodyText(message)); } else { mg.BodyText = condition.BodyFormatter.CreateBodyText(message); } AddMailAddress(message, condition.To, mg.To); AddMailAddress(message, condition.Cc, mg.Cc); AddMailAddress(message, condition.Bcc, mg.Bcc); if (condition.CopyContents == true) { foreach (var item in message.Contents) { var ct = new SmtpContent(); ct.LoadData(item.RawData); ct.ContentType.SetProperty(item.ContentType); ct.ContentDisposition.SetProperty(item.ContentDisposition); mg.Contents.Add(ct); } } return(mg); }
public void AddDkimSignatureHeader(SmtpMessage message) { var converter = this.DkimSignatureGenerator; var mg = message; var headers = new List<SmtpMailHeader>(); mg.Headers.Remove(DkimSignatureGenerator.SignatureKey); foreach (SmtpMailHeader header in mg.Headers) { headers.Add(new SmtpMailHeader(header.Key, header.Value)); } //To { var mm = new MemoryStream(); WriteMailAddressList(mm, mg.To); var value = mg.ContentType.CharsetEncoding.GetString(mm.ToArray()); headers.Add(new SmtpMailHeader("To", value)); } //Cc { var mm = new MemoryStream(); WriteMailAddressList(mm, mg.To); var value = mg.ContentType.CharsetEncoding.GetString(mm.ToArray()); headers.Add(new SmtpMailHeader("Cc", value)); } //Content-Type { var mm = new MemoryStream(); WriteEncodedHeader(mm, mg.ContentType); var value = mg.ContentType.CharsetEncoding.GetString(mm.ToArray()); headers.Add(new SmtpMailHeader("Content-Type", value)); } { var mm = new MemoryStream(); WriteBody(mm, mg); var bodyText = mg.ContentType.CharsetEncoding.GetString(mm.ToArray()); var headerValue = converter.GenerateDkimHeaderValue(bodyText); headers.Add(new SmtpMailHeader(DkimSignatureGenerator.SignatureKey, headerValue)); var appendHeaderValue = converter.GenerateSignature(headers); mg.Headers[DkimSignatureGenerator.SignatureKey] = headerValue + appendHeaderValue; } }
/// メールを送信し、送信結果となるSendMailListResultを取得します。 /// <summary> /// メールを送信し、送信結果となるSendMailListResultを取得します。 /// </summary> /// <param name="message"></param> /// <returns></returns> public SendMailResult SendMail(SmtpMessage message) { return(this.SendMail(new SendMailCommand(message))); }
private void WriteEncryptedData(Stream stream, SmtpMessage message, Byte[] signedContent) { //Do Nothing... }
public static SmtpMessage Create(MailMessage message, SmtpMessageCreateCondition condition) { SmtpMessage mg = new SmtpMessage(); mg.Subject = condition.SubjectFormatter.CreateSubject(message); mg.From = condition.Sender; mg.Sender = condition.Sender; mg.ContentType.SetProperty(message.ContentType); mg.ContentTransferEncoding = message.ContentTransferEncoding; if (condition.BodyFormatter.IsHtml == true) { mg._BodyTextContent.LoadHtml(condition.BodyFormatter.CreateBodyText(message)); } else { mg.BodyText = condition.BodyFormatter.CreateBodyText(message); } AddMailAddress(message, condition.To, mg.To); AddMailAddress(message, condition.Cc, mg.Cc); AddMailAddress(message, condition.Bcc, mg.Bcc); if (condition.CopyContents == true) { foreach (var item in message.Contents) { var ct = new SmtpContent(); ct.LoadData(item.RawData); ct.ContentType.SetProperty(item.ContentType); ct.ContentDisposition.SetProperty(item.ContentDisposition); mg.Contents.Add(ct); } } return mg; }
/// メールを送信し、送信結果となるSendMailListResultを取得します。 /// <summary> /// メールを送信し、送信結果となるSendMailListResultを取得します。 /// </summary> /// <param name="message"></param> /// <returns></returns> public SendMailResult SendMail(SmtpMessage message) { return this.SendMail(new SendMailCommand(message)); }
public static SmtpMessage Create(System.Net.Mail.MailMessage message, SmtpMessageCreateCondition condition) { SmtpMessage mg = new SmtpMessage(); mg.Subject = condition.SubjectFormatter.CreateSubject(message); mg.From = condition.Sender; mg.Sender = condition.Sender; mg.ContentType.CharsetEncoding = message.BodyEncoding; mg.Priority = message.Priority.Cast(); if (condition.BodyFormatter.IsHtml == true) { var html = condition.BodyFormatter.CreateBodyText(message); mg._BodyTextContent.LoadHtml(html); } else { mg.BodyText = condition.BodyFormatter.CreateBodyText(message); } AddMailAddress(message, condition.To, mg.To); AddMailAddress(message, condition.Cc, mg.Cc); AddMailAddress(message, condition.Bcc, mg.Bcc); if (condition.CopyAlternateViews == true) { foreach (var item in message.AlternateViews) { var ct = new SmtpContent(); ct.LoadData(item.ContentStream); ct.ContentTransferEncoding = item.TransferEncoding.Cast(); ct.ContentType.SetProperty(item.ContentType); mg.Contents.Add(ct); } } if (condition.CopyAttachments == true) { foreach (var item in message.Attachments) { var ct = new SmtpContent(); ct.LoadData(item.ContentStream); ct.ContentTransferEncoding = item.TransferEncoding.Cast(); ct.ContentType.SetProperty(item.ContentType); ct.ContentDisposition.SetProperty(item.ContentDisposition); mg.Contents.Add(ct); } } return mg; }
private void button2_Click(object sender, EventArgs e) { //====Smtp sample==================================// SmtpClient cl = new SmtpClient(); cl.ServerName = "mail.hundsun.com"; cl.UserName = "******"; cl.Password = "******"; cl.Port = 25; cl.Ssl = false; //cl.AuthenticateMode = SmtpAuthenticateMode.Auto; //bool t = cl.AuthenticateByLogin(); //false //bool t1 = cl.AuthenticateByPlain(); //falses //bool t2 = cl.Authenticate(); // 返回为 true //bool t3 = cl.AuthenticateByCramMD5(); // 返回为 true, 偶们邮箱是这种认证,其他都是false SmtpMessage mg = new SmtpMessage(); mg.Subject = "title"; mg.BodyText = "Hi.my mail body text!"; //Send by HTML format mg.IsHtml = true; mg.From = new MailAddress("*****@*****.**"); mg.To.Add(new MailAddress("*****@*****.**")); //Add attachment file from local disk //SmtpContent ct = new SmtpContent(); //ct.LoadFileData("C:\\setup.ini"); //mg.Contents.Add(ct); var rs = cl.SendMail(mg); //Check mail was sent or not if (rs.SendSuccessful == false) { //You can get information about send mail is success or error reason var resultState = rs.State; } }
public void WriteBody(SmtpMessage message) { WriteBody(_Stream, message); }
/// メールを送信し、送信結果となるSendMailResultを取得します。 /// <summary> /// メールを送信し、送信結果となるSendMailResultを取得します。 /// </summary> /// <param name="from"></param> /// <param name="message"></param> /// <returns></returns> public SendMailResult SendMail(String from, SmtpMessage message) { return(this.SendMail(new SendMailCommand(from, message))); }
/// メールを送信し、送信結果となるSendMailResultを取得します。 /// <summary> /// メールを送信し、送信結果となるSendMailResultを取得します。 /// </summary> /// <param name="from"></param> /// <param name="message"></param> /// <returns></returns> public SendMailResult SendMail(String from, SmtpMessage message) { return this.SendMail(new SendMailCommand(from, message)); }
private void WriteSignedData(Stream stream, SmtpMessage message, Byte[] body, Byte[] boundary) { var mg = message; Stream mm = stream; ThrowExceptionIfValueIsNull(mg.From.CryptographicKeyInfo.Base64Content, "Can't sign message unless the From property contains a privateKey."); mm.Write(ByteData.ContentTypeMultipartSignedProtocolApplicationXpkcs7Signature); mm.WriteByte(34);// " mm.Write(boundary); mm.WriteByte(34);// " mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); mm.Write(boundary); mm.Write(ByteData.NewLine); mm.Write(body); mm.Write(ByteData.NewLine); mm.Write(boundary); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentTypeApplicationXpkcs7Signature); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentTransferEncodingIsBase64); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentDispositionAttachmentFileNameIsSmimeP7s); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); var signatureBuffer = Cryptography.ToSignMessage(CryptographicBuffer.CreateFromByteArray(body), mg.From.CryptographicKeyInfo); Byte[] signature; CryptographicBuffer.CopyToByteArray(signatureBuffer, out signature); Base64Converter converter = new Base64Converter(body.Length); mm.Write(converter.Encode(signature)); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); mm.Write(boundary); mm.Write(Encoding.UTF8.GetBytes("--")); mm.Write(ByteData.NewLine); }
public void Write(SmtpMessage message) { this.Write(_Stream, message); }
private void WriteSignedData(Stream stream, SmtpMessage message, Byte[] body, Byte[] boundary) { var mg = message; Stream mm = stream; ThrowExceptionIfValueIsNull(mg.From.SigningCertificate, "Can't sign message unless the From property contains a signing certificate."); mm.Write(ByteData.ContentTypeMultipartSignedProtocolApplicationXpkcs7Signature); mm.WriteByte(34);// " mm.Write(boundary); mm.WriteByte(34);// " mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); byte[] signature = Cryptography.GetSignature(body, mg.From.SigningCertificate, mg.From.EncryptionCertificate); Base64Converter converter = new Base64Converter(signature.Length); mm.Write(ByteData.NewLine); mm.Write(boundary); mm.Write(ByteData.NewLine); mm.Write(body); mm.Write(ByteData.NewLine); mm.Write(boundary); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentTypeApplicationXpkcs7Signature); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentTransferEncodingIsBase64); mm.Write(ByteData.NewLine); mm.Write(ByteData.ContentDispositionAttachmentFileNameIsSmimeP7s); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); mm.Write(converter.Encode(signature)); mm.Write(ByteData.NewLine); mm.Write(ByteData.NewLine); mm.Write(boundary); mm.Write(Encoding.UTF8.GetBytes("--")); mm.Write(ByteData.NewLine); }
/// ユーザー名とメールアドレスをFromにセットします。 /// <summary> /// ユーザー名とメールアドレスをFromにセットします。 /// </summary> /// <param name="userName"></param> /// <param name="mailAddress"></param> public void SetFromMailAddress(String userName, String mailAddress) { this.From = SmtpMessage.CreateFromMailAddress(userName, mailAddress);; }