2017-01-06 16 views
1

amazon WS SMTPを使用して電子メールを送信しようとしていますが、成功して送信していますが、電子メールは受信しません。私は添付ファイルなしでそれを送るとき、私はそれを成功と受け取る。Amazon AWS SMTPを使用して添付ファイル付き電子メールを送信

+0

もっとコードを投稿できますか?あなたを助けるために、あなたは 'ssAttachmenstList'をどのように定義し、どのように設定するのかを知る必要があります。 – Taterhead

+0

この質問をチェックして、あなたを助けることができるかもしれませんか? http://stackoverflow.com/questions/2825950/sending-email-with-attachments-from-c-attachments-arrive-as-part-1-2-in-thunde –

+0

ArrayListの変数@taterheadイメージ – Luis

答えて

1

http://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-smtp-net.htmlは、私はあなたの問題は、おそらくあなたのメッセージに正しく添付ファイルを追加していないことだと思う:

は、ここに私のコード

// 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); 

      } 

ソースです。

添付ファイル付きの1つのメッセージを送信できました。 your source link aboveから直接取得したコードから始めました。私はanother SO article about the missing content type problemからコードを追加しました。

添付ファイルは、マイドキュメントフォルダのWord文書Lebowski.docxです。同様のシンプルなWord文書を作成し、次のコードを実行して単一の電子メールで簡単な添付ファイルを送信できることを確認することをお勧めします。

は、次のコードは私のために首尾よく働いた:

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(); 
     } 
    } 
} 
} 

あなたは、あなたがにあなたのssAttachmentsListからバイナリを取得する方法で作業を開始することができ、あなたの口座から1つの添付ファイル付きの電子メールを送信できることを証明したらあなたの電子メールと、それぞれに割り当てられた正しいコンテンツタイプ。しかし、あなたは私がこの時点でそれを判断するのに十分なコードやコンテキストを提供していませんでした。私はこのコードがあなたの添付ファイルの問題を解決するのを助けることを望んでいます。

+0

私は問題を見つけたと思う。私はZIPファイルを添付していましたが、何らかの理由でZIPを添付すると、ファイルをZIに名前変更したり、イメージを送信したりするとメッセージが破棄されます。 – Luis

+0

ok - zip添付ファイル - 知っておきたいことです。多くのメールセキュリティフィルタは.zip添付ファイルを削除して置き換えます。信頼できるクラウドロケーション(おそらくs3バケット)に格納された添付ファイルへのリンクを送信して、ダウンロードすることをお勧めします。がんばろう。 – Taterhead

関連する問題