Showing posts with label Amazon. Show all posts
Showing posts with label Amazon. Show all posts

Thursday, November 22, 2012

Send Email Attachments with Amazon-SES in C#

These two methods can be used to send emails with Attachment using Amazon-SES in C# Application.

public Boolean SendRawEmail(String from, String to, String cc, String Subject, String text, String html, String replyTo, string attachPath)

        AlternateView plainView = AlternateView.CreateAlternateViewFromString(text, Encoding.UTF8, "text/plain");
        AlternateView 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));

        List<String> ccAddresses = cc.Replace(", ", ",").Split(',').Where(y => y != "").ToList();

        foreach (String ccAddress in ccAddresses)
            mailMessage.CC.Add(new MailAddress(ccAddress));

        mailMessage.Subject = Subject;
        mailMessage.SubjectEncoding = Encoding.UTF8;

        if (replyTo != null)
            mailMessage.ReplyToList.Add(new MailAddress(replyTo));

        if (text != null)

        if (html != null)

        if (attachPath.Trim() != "")
            if (System.IO.File.Exists(attachPath))
                System.Net.Mail.Attachment objAttach = new System.Net.Mail.Attachment(attachPath);
                objAttach.ContentType = new ContentType("application/octet-stream");
                System.Net.Mime.ContentDisposition disposition = objAttach.ContentDisposition;
                disposition.DispositionType = "attachment";
                disposition.CreationDate = System.IO.File.GetCreationTime(attachPath);
                disposition.ModificationDate = System.IO.File.GetLastWriteTime(attachPath);
                disposition.ReadDate = System.IO.File.GetLastAccessTime(attachPath);

        RawMessage rawMessage = new RawMessage();

        using (MemoryStream memoryStream = ConvertMailMessageToMemoryStream(mailMessage))

        SendRawEmailRequest request = new SendRawEmailRequest();


        AmazonSimpleEmailService ses = AWSClientFactory.CreateAmazonSimpleEmailServiceClient(ConfigurationManager.AppSettings.Get("AccessKeyId"), ConfigurationManager.AppSettings.Get("SecretKeyId"));

            SendRawEmailResponse response = ses.SendRawEmail(request);
            SendRawEmailResult result = response.SendRawEmailResult;
            return true;
        catch (AmazonSimpleEmailServiceException ex)
            return false;
    public static MemoryStream ConvertMailMessageToMemoryStream(MailMessage message)
        Assembly assembly = typeof(SmtpClient).Assembly;
        Type mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");
        MemoryStream fileStream = new MemoryStream();
        ConstructorInfo mailWriterContructor = mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(Stream) }, null);
        object mailWriter = mailWriterContructor.Invoke(new object[] { fileStream });
        MethodInfo sendMethod = typeof(MailMessage).GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic);
        sendMethod.Invoke(message, BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { mailWriter, true }, null);
        MethodInfo closeMethod = mailWriter.GetType().GetMethod("Close", BindingFlags.Instance | BindingFlags.NonPublic);
        closeMethod.Invoke(mailWriter, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { }, null);
        return fileStream;

I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.
Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.

This article is related to
C#,.NET,Architect,Intermediate,VS2010,.Net,Articles,Computer Tutorials,Amazon,Amazon SES