1

I'm trying to send a email using amazon WS SMTP, is sending with success but i never receive the email. When i send it without attachment i receive it with success.

Here's my code

// Create an SMTP client with the specified host name and port.
        using (SmtpClient client = new SmtpClient(ssHost, ssPort))
        {
            // Create a network credential with your SMTP user name and password.
            client.Credentials = new System.Net.NetworkCredential(ssSMTP_Username, ssSMTP_Password);

            // Use SSL when accessing Amazon SES. The SMTP session will begin on an unencrypted connection, and then 
            // the client will issue a STARTTLS command to upgrade to an encrypted connection using SSL.
            client.EnableSsl = true;
            // Send the email. 

            MailMessage message = new MailMessage();
            try
            {
                //creates a messages with the all data
                message.From = new MailAddress(ssFrom, ssFromName);
                //message.To.Add(ssTo);
                //To
                List<String> toAddresses = ssTo.Split(',').ToList();
                foreach (String toAddress in toAddresses)
                {
                    message.To.Add(new MailAddress(toAddress));
                }
                //cc
                List<String> ccAddresses = ssCC.Split(',').Where(y => y != "").ToList();
                foreach (String ccAddress in ccAddresses)
                {
                    message.CC.Add(new MailAddress(ccAddress));
                }
                //bcc
                List<String> BccAddresses = ssBcc.Split(',').Where(y => y != "").ToList();
                foreach (String ccAddress in ccAddresses)
                {
                    message.Bcc.Add(new MailAddress(ccAddress));
                }

                //replyTo
                if (ssReplyTo != null)
                {
                    message.ReplyToList.Add(new MailAddress(ssReplyTo));
                }

                //email
                message.Subject = ssSubject;
                message.Body = ssBody;
                message.IsBodyHtml = true;


                //Attachment
                foreach (RCAttachmentRecord attchmentRec in ssAttachmenstList){
                    System.IO.MemoryStream ms = new System.IO.MemoryStream(attchmentRec.ssSTAttachment.ssBinary);
                    Attachment attach = new Attachment(ms, attchmentRec.ssSTAttachment.ssFilename);
                    message.Attachments.Add(attach);

                    ssErrorMessage = ssErrorMessage + "||" + attchmentRec.ssSTAttachment.ssFilename+"lenght:"+attchmentRec.ssSTAttachment.ssBinary.Length;
                }
                client.Send(message);

            }

source: http://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-smtp-net.html

Luis
  • 2,500
  • 7
  • 38
  • 69
  • Can you post more code? In order to help you, we need to know how you define and populate `ssAttachmenstList` - thanks. – Taterhead Jan 06 '17 at 15:53
  • Check out this SO question, might be able to help you out? http://stackoverflow.com/questions/2825950/sending-email-with-attachments-from-c-attachments-arrive-as-part-1-2-in-thunde – Karan Shah Jan 06 '17 at 16:08
  • @Taterhead image that variable as a ArrayList – Luis Jan 06 '17 at 16:15
  • @KaranShah Thanks, but didn't work. – Luis Jan 06 '17 at 16:36

1 Answers1

1

I think your problem probably is that you are not adding your attachments correctly to your message.

I was successful sending a single message with an attachment. I started with the code that was taken directly from your source link above. I then added the code from another SO article about the missing content type problem.

The attachment is a Word Document Lebowski.docx from my Documents folder. I suggest you create a similar simple Word document and run the following code to verify you can send a simple attachment in a single email.

The following code worked successfully for me:

namespace SESTest
{
using System;
using System.Net.Mail;

namespace AmazonSESSample
{
    class Program
    {
        static void Main(string[] args)
        {
            const String FROM = "from-email";   // Replace with your "From" address. This address must be verified.
            const String TO = "to-email";  // Replace with a "To" address. If your account is still in the
                                                        // sandbox, this address must be verified.

            const String SUBJECT = "Amazon SES test (SMTP interface accessed using C#)";
            const String BODY = "This email and attachment was sent through the Amazon SES SMTP interface by using C#.";

            // Supply your SMTP credentials below. Note that your SMTP credentials are different from your AWS credentials.
            const String SMTP_USERNAME = "user-creds";  // Replace with your SMTP username. 
            const String SMTP_PASSWORD = "password";  // Replace with your SMTP password.

            // Amazon SES SMTP host name. 
            const String HOST = "your-region.amazonaws.com";

            // The port you will connect to on the Amazon SES SMTP endpoint. We are choosing port 587 because we will use
            // STARTTLS to encrypt the connection.
            const int PORT = 2587;

            // Create an SMTP client with the specified host name and port.
            using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT))
            {
                // Create a network credential with your SMTP user name and password.
                client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);

                // Use SSL when accessing Amazon SES. The SMTP session will begin on an unencrypted connection, and then 
                // the client will issue a STARTTLS command to upgrade to an encrypted connection using SSL.
                client.EnableSsl = true;

                // Send the email. 
                try
                {
                    Console.WriteLine("Attempting to send an email through the Amazon SES SMTP interface...");

                    var mailMessage = new MailMessage()
                    {
                        Body = BODY,
                        Subject = SUBJECT,
                        From = new MailAddress(FROM)
                    };
                    mailMessage.To.Add(new MailAddress(TO));

                    //REMOVE THIS CODE
                    //System.IO.MemoryStream ms = new System.IO.MemoryStream(attchmentRec.ssSTAttachment.ssBinary);
                    //Attachment attach = new Attachment(ms, attchmentRec.ssSTAttachment.ssFilename);
                    //mailMessage.Attachments.Add(attach);

                    System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
                    contentType.MediaType = System.Net.Mime.MediaTypeNames.Application.Octet;
                    contentType.Name = "Lebowski.docx";
                    mailMessage.Attachments.Add(new Attachment("C:\\users\\luis\\Documents\\Lebowski.docx", contentType));

                    client.Send(mailMessage);

                    Console.WriteLine("Email sent!");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("The email was not sent.");
                    Console.WriteLine("Error message: " + ex.Message);
                }
            }

            Console.Write("Press any key to continue...");
            Console.ReadKey();
        }
    }
}
}

Once you prove you can send an email with one attachment from your account, then you can start working on how to get the binaries from your ssAttachmentsList into an your emails and the correct content type assigned to each. But you didn't provide enough code or context for me to determine that at this time. Im hoping this code will help you get over your attachment issue.

Community
  • 1
  • 1
Taterhead
  • 4,845
  • 2
  • 23
  • 35
  • I think i found the problem. I was attaching ZIP files, but somehow when i attach ZIP the message is discard, if i rename the file to ZI or send image i always received the email. – Luis Jan 11 '17 at 11:32
  • ok - zip attachments - that is good to know. Keep in mind that many mail security filters strip and replace .zip attachments. You might have better luck sending a link to your attachment stored in a trusted cloud location (s3 bucket perhaps) for them to download. Good luck. – Taterhead Jan 11 '17 at 11:50