-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #522 from shortstuffsushi/update-add-attachment-in…
…terface Update add attachment interface
- Loading branch information
Showing
6 changed files
with
410 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,10 +28,11 @@ namespace Example | |
{ | ||
private static void Main() | ||
{ | ||
Execute().Wait(); | ||
ExecuteManualAttachmentAdd().Wait(); | ||
ExecuteStreamAttachmentAdd().Wait(); | ||
} | ||
|
||
static async Task Execute() | ||
static async Task ExecuteManualAttachmentAdd() | ||
{ | ||
var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY"); | ||
var client = new SendGridClient(apiKey); | ||
|
@@ -45,6 +46,23 @@ namespace Example | |
msg.AddAttachment("file.txt", file); | ||
var response = await client.SendEmailAsync(msg); | ||
} | ||
|
||
static async Task ExecuteStreamAttachmentAdd() | ||
{ | ||
var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY"); | ||
var client = new SendGridClient(apiKey); | ||
var from = new EmailAddress("[email protected]"); | ||
var subject = "Subject"; | ||
var to = new EmailAddress("[email protected]"); | ||
var body = "Email Body"; | ||
var msg = MailHelper.CreateSingleEmail(from, to, subject, body, ""); | ||
|
||
using (var fileStream = File.OpenRead("/Users/username/file.txt")) | ||
{ | ||
msg.AddAttachment("file.txt", fileStream); | ||
var response = await client.SendEmailAsync(msg); | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.IO; | ||
|
||
namespace SendGrid.Tests.Helpers.Mail | ||
{ | ||
|
||
public class NonReadableStream : Stream | ||
{ | ||
public override bool CanRead => false; | ||
|
||
public override bool CanSeek => throw new System.NotImplementedException(); | ||
|
||
public override bool CanWrite => throw new System.NotImplementedException(); | ||
|
||
public override long Length => throw new System.NotImplementedException(); | ||
|
||
public override long Position { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); } | ||
|
||
public override void Flush() | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public override int Read(byte[] buffer, int offset, int count) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public override long Seek(long offset, SeekOrigin origin) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public override void SetLength(long value) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public override void Write(byte[] buffer, int offset, int count) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
} | ||
} |
Oops, something went wrong.