2017-01-01 5 views
1

테스트 중입니다 .NetKorea는 Mailkit 만 사용하는 유일한 대안 인 System.Net.Mail을 지원하지 않지만 잘 작동하지만 첨부 파일을 보내는 방법을 알아낼 수 없습니다. 데이터베이스에 바이너리로 저장했습니다. MVC 5에서 다음을 사용했습니다..net Core Mailkit은 배열에서 첨부 파일을 전송합니다.

 var mail = new MailMessage(); 
    mail.Attachments.Add(new Attachment(new MemoryStream(attachment), 
    attachmentName, attachmentType)); 

어떻게 MailKit에서 할 수 있습니까?

+0

그래서 무엇이 문제인가? 아니면 문제가 있습니까? –

+0

MVC 5 및 System.Net.Mail에서 작동합니다. Mailkit을 사용하고 싶습니다. – hncl

+0

Mailkit을 사용하는 현재 코드 표시 – haim770

답변

3

빌더를 만든 다음 첨부 파일을 IFromFile 또는 Binary 문자열로 첨부해야합니다. 희망이 도움이됩니다.

public async void SendEmail(string mailTo, string mailFrom, string cc, string subject, string message) 
{ 
    var emailMessage = new MimeMessage(); 
    if (cc != null) 
    { 
     emailMessage.Cc.Add(new MailboxAddress(cc)); 
    } 

    emailMessage.From.Add(new MailboxAddress("SenderName", "UserName")); 
    emailMessage.To.Add(new MailboxAddress("mailTo")); 
    emailMessage.Subject = subject; 
    var builder = new BodyBuilder { TextBody = message }; 

    //Fetch the attachments from db 
    //considering one or more attachments 
    if (attachments != null) 
    { 
     foreach (var attachment in attachments.ToList()) 
     { 
      builder.Attachments.Add(attachmentName, attachment, ContentType.Parse(attachmentType)); 
     } 
    } 
    emailMessage.Body = builder.ToMessageBody(); 
    using (var client = new SmtpClient()) 
    { 
     var credentials = new NetworkCredential 
     { 
      UserName = "USERNAME", 
      Password = "PASSWORD" 
     }; 
     await client.AuthenticateAsync(credentials); 
     await client.ConnectAsync("smtp.gmail.com", 587).ConfigureAwait(false); 
     await client.SendAsync(emailMessage).ConfigureAwait(false); 
     await client.DisconnectAsync(true).ConfigureAwait(false);    
    } }  
+1

첨부 생성자는 인수를 하나만 사용합니다. jstedfast 답변에 기초 https://github.com/jstedfast/MailKit/issues/445. 내가 사용했습니다 builder.Attachments.Add (attachmentName, attachment, ContentType.Parse (attachmentType)); – hncl