dineshkummarc / sendgrid-csharp

i liked the project to such an extent i also copied their issues to be solved. so as i can work on them.
MIT License
0 stars 0 forks source link

Getting 'Invalid URI: The Uri string is too long.' exception from library when using the WebAPI #3

Open dineshkummarc opened 11 years ago

dineshkummarc commented 11 years ago

Hi, I'm using the Web transport for sending complex HTML emails and from time to time, the library throws this exception: Message: Invalid URI: The Uri string is too long. StackTrace: at SendGridMail.Transport.Web.CheckForErrors(IRestResponse response) at SendGridMail.Transport.Web.Deliver(ISendGrid message) Does anyone know how to work around this problem? Thanks in advance.

dineshkummarc commented 11 years ago

comments as they published :

Looks like you've uncovered a bug. Thanks!

On this line: https://github.com/dineshkummarc/sendgrid-csharp/blob/master/SendGrid/SendGridMail/Transport/Web.cs#L101

XmlReader.Create(String) expects a URI. It seems to work in most cases because the error messages are usually < 2000 chars. It should be using a TextReader (or a subclass, such as StreamReader).

using (var reader = XmlReader.Create(new StreamReader(stream))

I'll try to make this change and get it pushed soon, but that should let you workaround it in the mean time. psegalen commented 23 days ago

Wow! Now that's what I call reactivity! :)

Many thanks! psegalen commented 23 days ago

FYI: the SMTP transport works just fine with my code but I presume it's slower than the Web one, isn't it? brandonmwest commented 23 days ago

Generally speaking web/HTTP has less latency because there are fewer requests and responses than SMTP. At high volume, the best combination of reliability and speed is probably using a local mail server in combination with SendGrid.

The best way to reduce the number of connections being made from within your code is to send your emails in batches of 1000 using the X-SMTPAPI header or parameter rather than one SMTP or HTTP call per recipient. You essentially reduce the number of connections by 3 orders of magnitude this way. psegalen commented 23 days ago

Basically, I have a batch that runs every hour lauching ~15 threads sending up to 1500 emails each. Each email body is unique, it's generated according to the user's configuration. Are you saying that I can send 1000 emails with different bodies within one single HTTP or SMTP call? (Yes, I'm a SendGrid noob! :)) brandonmwest commented 23 days ago

Yes indeed! Check out the SMTP API overview. You can pass the JSON isntructions in the X-SMTPAPI header for SMTP, or in the x-smtpapi parameter of your web API call.

To handle the varying bodies, you would use substitution tags and section tags. psegalen commented 23 days ago

Ok, thanks! One more thing: what are the size limitations for these techniques? The email body can be up to 100Ko of HTML and I presume that sending a 100Mo X-SMTPAPI header could be bad for both the client and the server? psegalen commented 23 days ago

Wow! I have a massive number of exceptions with the SMTP transport:

Exception message: "Failure sending mail." or "The operation has timed out." StackTrace: at System.Net.Mail.SmtpClient.Send(MailMessage message) at SendGridMail.Transport.SMTP.SmtpWrapper.Send(MailMessage mime) at SendGridMail.Transport.SMTP.Deliver(ISendGrid message)

It happens when I have a huge number of emails to send on many Threads, it doesn't seem to appreciate sending many emails in parallel ... I guess I'll have to do it your way ... theycallmeswift commented 9 days ago

What's the status on this issue? Resolved or no? Looking to get this closed out. psegalen commented 6 days ago

Hi, I don't think it has been resolved, I did some tests with Brandon's workaround but it throws exception the same way for large email bodies. I've just limited the max size of my email bodies for now. brandonmwest commented 2 days ago

Thanks for reporting back. Will have to do some digging and see what might be going on here. psegalen commented 2 days ago

Thanks to you for your support! Like I said, I've limited the max size and it's OK for what I have to do. Do you want me to close this?