2017-04-20 8 views
0

イメージを暗号化しました。 これでイメージを読み取り、電子メールに添付して解読する必要があります。イメージファイルをMemoryStreamに挿入して電子メールに添付する方法

最初のステップでは、イメージファイルを入れて電子メール に添付しようとしましたが、電子メールを受信すると、添付イメージが破損しています。

私はさまざまな方法を試していますが、成功することはありません。 (私はテスト用のWindowsアプリケーションプロジェクトを作成し、最終的に私はMVC Webアプリケーションプロジェクトでソリューションを使用する必要があります)

private void btnSend_Click(object sender, EventArgs e) 
{ 
    var filePath = "D:\\3.jpg"; // path to none encrypted image file 

    var ms = new MemoryStream(File.ReadAllBytes(filePath)); 

    // Create attachment 
    var attach = new Attachment(ms, new ContentType(MediaTypeNames.Image.Jpeg)); 
    attach.ContentDisposition.FileName = "sample.jpg"; 

    // Send Email 
    IMailSender mailSender = new MailSender(); 
    var isSuccess = mailSender.Send(
     "sample email title", 
     "[email protected]", 
     "sample subject", 
     "sample body", 
     new Attachment[] { attach }); 

    MessageBox.Show(isSuccess ? "Email sent successfully" : mailSender.ErrorMessage); 

} 
+0

を役に立てば幸い正しいポジションを探しますか? ms.Seek(0、SeekOrigin.Begin);あなたの助けをいただきありがとうございます。 – Digvijay

答えて

0
using (MailMessage Message = new MailMessage()) 
{ 
    Message.From = new MailAddress("[email protected]"); 
    Message.Subject = "My Subject"; 
    Message.Body = "My Body"; 
    Message.To.Add(new MailAddress("[email protected]")); 

    //Attach more file 
    foreach (var item in Attachment) 
    { 
     MemoryStream ms = new MemoryStream(File.ReadAllBytes(filePath)); 

     Attachment Data = new Attachment(ms, "FileName"); 
     ContentDisposition Disposition = Data.ContentDisposition; 
     Disposition.CreationDate = DateTime.UtcNow.AddHours(-5); 
     Disposition.ModificationDate = DateTime.UtcNow.AddHours(-5); 
     Disposition.ReadDate = DateTime.UtcNow.AddHours(-5); 
     Data.ContentType = new ContentType(MediaTypeNames.Application.Pdf); 
     Message.Attachments.Add(Data); 
    } 

    SmtpClient smtp = new SmtpClient("SmtpAddress", "SmtpPort"); 
    smtp.Credentials = new NetworkCredential("SmtpUser", "SmtpPassword"); 
    await smtp.SendMailAsync(Message); 
} 

私はあなたがストリームにイメージをロードした後、ユーザーが設定した、これは

+0

私は 'ContentDisposition Disposition = Data.ContentDisposition;を追加します。 Disposition.CreationDate = DateTime.UtcNow.AddHours(-5); Disposition.ModificationDate = DateTime.UtcNow.AddHours(-5); Disposition.ReadDate = DateTime.UtcNow.AddHours(-5); '私のコードで、正常に動作します。どうもありがとう – Moradof

関連する問題