2017-08-25 165 views
2

我能夠與單個附件發送電子郵件,但我不能在單個郵件如何使用SendGrid Api在單個電子郵件中發送多個附件?

 try { 
      SendGrid sendgrid = new SendGrid(SENDGRID_USERNAME, SENDGRID_PASSWORD); 

      SendGrid.Email email = new SendGrid.Email(); 

      // Get values from edit text to compose email 
      // TODO: Validate edit texts 
      email.addTo(mTo); 
      email.setFrom(mFrom); 
      email.setSubject(mSubject); 
      email.setText(mText); 

      // Attach image 
      if (mUri != null) { 
       email.addAttachment(mAttachmentName, mAppContext.getContentResolver().openInputStream(mUri)); 
      } 

      // Send email, execute http request 
      SendGrid.Response response = sendgrid.send(email); 
      mMsgResponse = response.getMessage(); 

      Log.d("SendAppExample", mMsgResponse); 

     } catch (SendGridException | IOException e) { 
      Log.e("SendAppExample", e.toString()); 
     } 

     return null; 
    } 

回答

0

這是我的代碼修改:

  1. 從流增加附件創建方法

    private static async Task AddAttachmentAsync(List<Attachment> ls, string filename, Stream contentStream, string type = null, string disposition = null, string content_id = null, CancellationToken cancellationToken = default(CancellationToken)) 
    { 
        // Stream doesn't want us to read it, can't do anything else here 
        if (contentStream == null || !contentStream.CanRead) 
        { 
         return; 
        } 
    
        var contentLength = Convert.ToInt32(contentStream.Length); 
        var streamBytes = new byte[contentLength]; 
    
        await contentStream.ReadAsync(streamBytes, 0, contentLength, cancellationToken); 
    
        var base64Content = Convert.ToBase64String(streamBytes); 
    
        ls.Add(AddAttachment(filename, base64Content, type, disposition, content_id)); 
    } 
    
    private static Attachment AddAttachment(string filename, string base64Content, string type = null, string disposition = null, string content_id = null) 
    { 
        if (string.IsNullOrWhiteSpace(filename) || string.IsNullOrWhiteSpace(base64Content)) 
        { 
         return null; 
        } 
    
        var attachment = new Attachment 
        { 
         Filename = filename, 
         Content = base64Content, 
         Type = type, 
         Disposition = disposition, 
         ContentId = content_id 
        }; 
    
        return attachment; 
    } 
    
  2. 如何從數據流中添加附件SendGridMessage

    //var msg = new SendGridMessage(); 
    //your another settings addContent(body), subject, To etc 
    //HttpFileCollection fuAttachment --> list attachments from http file collection 
    //dont forget : msg.Attachments = new List<Attachment>(); 
    for (var i = 0; i < fuAttachment.Count; i++) 
    { 
        string FileName = Path.GetFileName(fuAttachment[i].FileName); 
        await AddAttachmentAsync(msg.Attachments, FileName, 
        fuAttachment[i].InputStream); 
    } 
    //var apiKey = _SendGridAPIKey; 
    //var client = new SendGridClient(apiKey); 
    //var response = await client.SendEmailAsync(msg); 
    
+0

兄弟,我想在android中完成它。但我很感謝你的努力,謝謝你 –

+0

哦,對不起,我沒有看到..上面的代碼是爲asp.net:D 我應該刪除這個? –

+0

不需要老兄,也許它會幫助別人。 –

相關問題