Represents the raw data of the message.
public static void SESSendRawEmail() { #region SESSendRawEmail // using System.IO; var sesClient = new AmazonSimpleEmailServiceClient(); var stream = new MemoryStream( Encoding.UTF8.GetBytes("From: [email protected]\n" + "To: [email protected]\n" + "Subject: You're invited to the meeting\n" + "Content-Type: text/plain\n\n" + "Please join us Monday at 7:00 PM.") ); var raw = new RawMessage { Data = stream }; var to = new List<string>() { "*****@*****.**" }; var from = "*****@*****.**"; var request = new SendRawEmailRequest { Destinations = to, RawMessage = raw, Source = from }; sesClient.SendRawEmail(request); #endregion }
private static RawMessage ToRawMessage(MailMessage message) { var rawMessage = new RawMessage(); using (var memoryStream = message.ToMemoryStream()) { rawMessage.Data = memoryStream; } return rawMessage; }
/// <summary> /// Constructs a new SendRawEmailRequest object. /// Callers should use the properties or fluent setter (With...) methods to /// initialize any additional object members. /// </summary> /// /// <param name="rawMessage"> The raw text of the message. The client is responsible for ensuring the following: <ul> <li>Message must contain a /// header and a body, separated by a blank line.</li> <li>All required header fields must be present.</li> <li>Each part of a multipart MIME /// message must be formatted properly.</li> <li>MIME content types must be among those supported by Amazon SES. Refer to the <a /// href="http://docs.amazonwebservices.com/ses/latest/DeveloperGuide">Amazon SES Developer Guide</a> for more details.</li> <li>Content must be /// base64-encoded, if MIME requires it.</li> </ul> </param> public SendRawEmailRequest(RawMessage rawMessage) { this.rawMessage = rawMessage; }
/// <summary> /// Sets the RawMessage property /// </summary> /// <param name="rawMessage">The value to set for the RawMessage property </param> /// <returns>this instance</returns> public SendRawEmailRequest WithRawMessage(RawMessage rawMessage) { this.rawMessage = rawMessage; return this; }
public void SendEmailWithAttachment() { var client = Amazon.AWSClientFactory.CreateAmazonSimpleEmailServiceClient(EmailProcessingConfigurationManager.Section.Amazon.Key, EmailProcessingConfigurationManager.Section.Amazon.Secret); MailMessage m = new MailMessage(); var attachment = new Attachment("TextFile1.txt", "text/plain"); attachment.TransferEncoding = TransferEncoding.QuotedPrintable; m.Attachments.Add(attachment); m.Body = "hello"; m.Subject = "hello"; m.To.Add("*****@*****.**"); m.From = new MailAddress("*****@*****.**"); var messageData = ConvertMailMessageToMemoryStream(m); RawMessage message = new RawMessage(messageData); SendRawEmailRequest request = new SendRawEmailRequest(message); var response = client.SendRawEmail(request); }
public void Sendraw() { string raw = @"X-Sender: [email protected] X-Receiver: [email protected] MIME-Version: 1.0 From: [email protected] To: [email protected] Date: 9 Feb 2011 17:28:54 +0000 Subject: hello Content-Type: multipart/mixed; boundary=--boundary_0_6c21630e-f7ee-4968-97de-1940cd84094f ----boundary_0_6c21630e-f7ee-4968-97de-1940cd84094f Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable hello ----boundary_0_6c21630e-f7ee-4968-97de-1940cd84094f Content-Type: text/html; name=TextFile1.txt Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment =EF=BB=BFtext file ----boundary_0_6c21630e-f7ee-4968-97de-1940cd84094f-- "; var client = Amazon.AWSClientFactory.CreateAmazonSimpleEmailServiceClient(EmailProcessingConfigurationManager.Section.Amazon.Key, EmailProcessingConfigurationManager.Section.Amazon.Secret); MemoryStream ms = new MemoryStream(); StreamWriter sw = new StreamWriter(ms); sw.Write(raw); sw.Flush(); sw.Close(); var rawMessage = new RawMessage(ms); client.SendRawEmail(new SendRawEmailRequest(rawMessage)); }
/// <summary> /// Method to send email through cloud service. Please make sure that the attachements are of supported types. /// If MailMessage has LinkedResources associated with alternate views, they will be removed. Amazon SES currently /// requires content disposition for linked resources, which .Net does not support. /// </summary> /// <param name="mailMessage">.Net MailMessage</param> public void SendBulkEmail(MailMessage mailMessage) { //Create a temp unique folder under WorkingTempFolder string currentWorkingFolder = Path.Combine(this.cloudServiceConfigProvider.WorkingTempFolder, System.Guid.NewGuid().ToString("N")); try { if (mailMessage == null) throw new CloudServiceException("Mail message must be provided."); if (mailMessage.To.Count == 0) throw new CloudServiceException("To address is required."); if (mailMessage.From == null || string.IsNullOrEmpty(mailMessage.From.Address)) throw new CloudServiceException("From address is required."); if (!Directory.Exists(this.cloudServiceConfigProvider.WorkingTempFolder)) throw new CloudServiceConfigException("WorkingTempFolder specified does not exist."); DirectoryInfo dInfo = Directory.CreateDirectory(currentWorkingFolder); //Clear linked resources (Amazon has a glitch by requiring content-disposition for LinkedResource) foreach (AlternateView av in mailMessage.AlternateViews) { av.LinkedResources.Clear(); } //Setup content disposition for attachments //Amazon needs content disposition foreach (Attachment attachment in mailMessage.Attachments) { if (attachment.ContentDisposition != null) { attachment.ContentDisposition.Inline = false; if (attachment.ContentStream != null) attachment.ContentDisposition.Size = attachment.ContentStream.Length; } } //Create smpt client SmtpClient client = new SmtpClient(); client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory; client.PickupDirectoryLocation = currentWorkingFolder; client.Send(mailMessage); //Create AWS Client AmazonSimpleEmailService amazonSes = AWSClientFactory.CreateAmazonSimpleEmailServiceClient(this.cloudServiceConfigProvider.AWSAccessKeyId, this.cloudServiceConfigProvider.AWSSecretKey); //At this point the email is created in the working folder. Ready one at a time and send to amazon foreach (FileInfo fInfo in dInfo.GetFiles()) { byte[] emailRawContent = File.ReadAllBytes(fInfo.FullName); using (MemoryStream msEmail = new MemoryStream(emailRawContent, 0, emailRawContent.Length)) { RawMessage rawMessage = new RawMessage(msEmail); SendRawEmailRequest sendRawEmailRequest = new SendRawEmailRequest(rawMessage); SendRawEmailResponse sendRawEmailRes = amazonSes.SendRawEmail(sendRawEmailRequest); } } } catch (CloudServiceException aex) { throw aex; } catch (Exception ex) { throw new CloudServiceException(ex, "Error occured when sending bulk email."); } finally { if(!this.cloudServiceConfigProvider.IsTraceMode == !string.IsNullOrEmpty(currentWorkingFolder)) { if (Directory.Exists(currentWorkingFolder)) { foreach (string file in Directory.GetFiles(currentWorkingFolder)) { try { if (File.Exists(file)) File.Delete(file); } catch { ///TODO: Add log once moved to common library } } try { Directory.Delete(currentWorkingFolder); } catch { ///TODO: Add log once moved to common library } } } } }
//From E-mail address must be verified through Amazon /// <summary> /// Text Only for now, no attachments yet. Amazon SES is still in Beta /// </summary> /// <param name="AWSAccessKey">public key associated with our Amazon Account</param> /// <param name="AWSSecretKey">private key associated with our Amazon Account</param> /// <param name="From">Who is the e-mail from, this must be a verified e-mail address through Amazon</param> /// <param name="To">Who do you want to send the E-mail to, seperate multiple addresses with a comma</param> /// <param name="Subject">Subject of e-mail</param> /// <param name="Attachment">File Location of attachment. PDF's, text files and images supported, seperate multiple addresses with a comma</param> /// <param name="Text">Plain text for e-mail, can be null</param> /// <param name="Html">Html view for e-mail, can be null</param> /// <param name="ReplyTo ">Email address for replies, can be null</param> /// <param name="CCAddresses ">ArrayList of strings for CC'd addresses</param> /// <param name="BCCAddresses ">ArrayList of strings for BCC'd addresses</param> public bool SendEmailWithAttachments(string AWSAccessKey, string AWSSecretKey, String From, String To, String Subject, String Attachment, String Text = null, String Html = null, String ReplyTo = null, ArrayList CCAddresses = null, ArrayList BCCAddresses = null) { AlternateView plainView = null; if (Text != null) plainView = AlternateView.CreateAlternateViewFromString(Text, Encoding.UTF8, "text/plain"); AlternateView htmlView = null; if (Html != null) htmlView = AlternateView.CreateAlternateViewFromString(Html, Encoding.UTF8, "text/html"); MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress(From); List<String> toAddresses = To.Replace(", ", ",").Split(',').ToList(); foreach (String toAddress in toAddresses) { mailMessage.To.Add(new MailAddress(toAddress)); } if (CCAddresses != null) { foreach (String ccAddress in CCAddresses) { mailMessage.CC.Add(new MailAddress(ccAddress)); } } if (BCCAddresses != null) { foreach (String bccAddress in BCCAddresses) { mailMessage.Bcc.Add(new MailAddress(bccAddress)); } } mailMessage.Subject = Subject; mailMessage.SubjectEncoding = Encoding.UTF8; if (ReplyTo != null) { mailMessage.ReplyTo = new MailAddress(ReplyTo); } if (Text != null) { mailMessage.AlternateViews.Add(plainView); } if (Html != null) { mailMessage.AlternateViews.Add(htmlView); } // Attachment Fix //System.Net.Mail.Attachment a = new System.Net.Mail.Attachment(Attachment); //mailMessage.Attachments.Add(a); foreach (String attachment in Attachment.Replace(", ", ",").Split(',').ToList()) { System.Net.Mail.Attachment a = new System.Net.Mail.Attachment(attachment); mailMessage.Attachments.Add(a); } RawMessage rawMessage = new RawMessage(); using (MemoryStream memoryStream = ConvertMailMessageToMemoryStream(mailMessage)) { rawMessage.WithData(memoryStream); } SendRawEmailRequest request = new SendRawEmailRequest(); request.WithRawMessage(rawMessage); request.WithDestinations(toAddresses); request.WithSource(From); AmazonSimpleEmailService ses = AWSClientFactory.CreateAmazonSimpleEmailServiceClient(AWSAccessKey, AWSSecretKey); try { SendRawEmailResponse response = ses.SendRawEmail(request); SendRawEmailResult result = response.SendRawEmailResult; return true; } catch { return false; } }
public bool SendEmailWithAttachments(string AWSAccessKey, string AWSSecretKey, MailAddress From, MailAddress[] To, String Subject, System.Net.Mail.Attachment[] Attachments, String Text = null, String Html = null, MailAddress ReplyTo = null, MailAddress[] CCAddresses = null, MailAddress[] BCCAddresses = null) { AlternateView plainView = null; if (Text != null) plainView = AlternateView.CreateAlternateViewFromString(Text, Encoding.UTF8, "text/plain"); AlternateView htmlView = null; if (Html != null) htmlView = AlternateView.CreateAlternateViewFromString(Html, Encoding.UTF8, "text/html"); MailMessage mailMessage = new MailMessage(); mailMessage.From = From; foreach (MailAddress toAdd in To ?? Enumerable.Empty<MailAddress>()) mailMessage.To.Add(toAdd); foreach (MailAddress toCC in CCAddresses ?? Enumerable.Empty<MailAddress>()) mailMessage.CC.Add(toCC); foreach (MailAddress toBcc in BCCAddresses ?? Enumerable.Empty<MailAddress>()) mailMessage.Bcc.Add(toBcc); mailMessage.Subject = Subject; mailMessage.SubjectEncoding = Encoding.UTF8; if (ReplyTo != null) { mailMessage.ReplyTo = ReplyTo; } if (Text != null) { mailMessage.AlternateViews.Add(plainView); } if (Html != null) { mailMessage.AlternateViews.Add(htmlView); } foreach (System.Net.Mail.Attachment a in Attachments ?? Enumerable.Empty<System.Net.Mail.Attachment>()) { mailMessage.Attachments.Add(a); } RawMessage rawMessage = new RawMessage(); using (MemoryStream memoryStream = ConvertMailMessageToMemoryStream(mailMessage)) { rawMessage.WithData(memoryStream); } SendRawEmailRequest request = new SendRawEmailRequest(); request.WithRawMessage(rawMessage); request.WithDestinations(To.Select(a => a.Address)); request.WithSource(From.Address); AmazonSimpleEmailService ses = AWSClientFactory.CreateAmazonSimpleEmailServiceClient(AWSAccessKey, AWSSecretKey); try { SendRawEmailResponse response = ses.SendRawEmail(request); SendRawEmailResult result = response.SendRawEmailResult; return true; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(string.Format("There was an error sending the e-mail: {0}", ex.Message)); return false; } }
/// <summary> /// Instantiates SendRawEmailRequest with the parameterized properties /// </summary> /// <param name="rawMessage">The raw email message itself. The message has to meet the following criteria: <ul> <li> The message has to contain a header and a body, separated by a blank line. </li> <li> All of the required header fields must be present in the message. </li> <li> Each part of a multipart MIME message must be formatted properly. </li> <li> Attachments must be of a content type that Amazon SES supports. For a list on unsupported content types, see <a href="http://docs.aws.amazon.com/ses/latest/DeveloperGuide/mime-types.html">Unsupported Attachment Types</a> in the <i>Amazon SES Developer Guide</i>. </li> <li> The entire message must be base64-encoded. </li> <li> If any of the MIME parts in your message contain content that is outside of the 7-bit ASCII character range, we highly recommend that you encode that content. For more information, see <a href="http://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-email-raw.html">Sending Raw Email</a> in the <i>Amazon SES Developer Guide</i>. </li> <li> Per <a href="https://tools.ietf.org/html/rfc5321#section-4.5.3.1.6">RFC 5321</a>, the maximum length of each line of text, including the <CRLF>, must not exceed 1,000 characters. </li> </ul></param> public SendRawEmailRequest(RawMessage rawMessage) { _rawMessage = rawMessage; }
/// <summary> /// Sets the RawMessage property /// </summary> /// <param name="rawMessage">The value to set for the RawMessage property </param> /// <returns>this instance</returns> public SendRawEmailRequest WithRawMessage(RawMessage rawMessage) { this.rawMessage = rawMessage; return(this); }
/// <summary> /// <see cref="Send" /> this message. /// </summary> /// <returns> /// <see langword="true" /> if it succeeds, <see langword="false" /> if it fails. /// </returns> internal bool Send() { _email.MessageId = string.Empty; try { var mailMessage = new MailMessage { From = new MailAddress(_email.FromAddress) }; foreach (string toAddress in _email.ToAddressList) mailMessage.To.Add(new MailAddress(toAddress)); foreach (string ccAddress in _email.CcAddressList) mailMessage.CC.Add(new MailAddress(ccAddress)); foreach (string bccAddress in _email.BccAddressList) mailMessage.Bcc.Add(new MailAddress(bccAddress)); mailMessage.Subject = _email.MessageSubject; mailMessage.SubjectEncoding = Encoding.UTF8; mailMessage.AlternateViews.Add( _email.HTML ? AlternateView.CreateAlternateViewFromString(_email.MessageBody, Encoding.UTF8, "text/html") : AlternateView.CreateAlternateViewFromString(_email.MessageBody, Encoding.UTF8, "text/plain")); var attachment = new Attachment(_email.AttachmentFilePath) { ContentType = new ContentType("application/octet-stream") }; ContentDisposition disposition = attachment.ContentDisposition; disposition.DispositionType = "attachment"; disposition.CreationDate = File.GetCreationTime(_email.AttachmentFilePath); disposition.ModificationDate = File.GetLastWriteTime(_email.AttachmentFilePath); disposition.ReadDate = File.GetLastAccessTime(_email.AttachmentFilePath); mailMessage.Attachments.Add(attachment); var rawMessage = new RawMessage(); using (MemoryStream memoryStream = ConvertMailMessageToMemoryStream(mailMessage)) rawMessage.WithData(memoryStream); var request = new SendRawEmailRequest { RawMessage = rawMessage, Destinations = _email.Destination.ToAddresses, Source = _email.FromAddress }; using (var client = new Client(_email.Credentials)) { _email.MessageId = client.SendRawEmail(request); } return !_email.ErrorExists; } catch (AmazonSimpleEmailServiceException ex) { return _email.SetErrorMessage( string.Format( "AWS Simple Email Service Exception\n\nError Type: {0}\n" + "Error Code: {1}\nRequest Id: {2}\nStatus Code: {3}\n\n{4}", ex.ErrorType, ex.ErrorCode, ex.RequestId, ex.StatusCode, ex)); } catch (AmazonClientException ex) { return _email.SetErrorMessage(ex.ToString()); } catch (Exception ex) { return _email.SetErrorMessage(ex.ToString()); } }