如何使用SendGrid Api在单个电子邮件中发送多个附件?

时间:2017-08-25 12:14:45

标签: android sendgrid-api-v3

我能够通过单个附件发送电子邮件但我找不到任何可以通过一封邮件发送多个附件

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

2 个答案:

答案 0 :(得分: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);
    

答案 1 :(得分:0)

$files = ['/home/pawr/testAttach1.txt','/home/pawr/testAttach2.txt'];
foreach($files as $f) {
    // create attachment and add to email
    $fn = $this->createSendGridAttachment($f);
    $email->addAttachment($fn);
}
// code in custom function createSendGridAttachment($file_path)
{
    // Note, you may need to use different encoding methods for setContent() and
    // MIME types with setType() depending on your use case.
    $content = file_get_contents($file_path);
    $content = base64_encode($content);
    $attachment = new \SendGrid\Mail\Attachment();
    $attachment->setContent($content);
    $attachment->setFilename(basename($file_path));

    return $attachment;
}