Comments (11)
Have you experienced a problem? If so, can you share the code that caused the problem? What SMTP transport are you using to send your messages? Most of them should handle this for you. Thanks
from smtpapi-csharp.
Have you experienced a problem?
Not yet..., currently I'm testing with 5 existing email addresses which belongs to me.
Five email addresses does not exceed the limit of 1'000 line characters.
But according to the sendgrid and RFC 821 documentation no line can be longer than 1,000 characters. I'm using the smtpapi-csharp library https://github.com/sendgrid/smtpapi-csharp. This is my code:
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.sendgrid.net";
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("", "");
MailMessage mail = new MailMessage();
mail.To.Add(new MailAddress("[email protected]"));
mail.From = new MailAddress("[email protected]");
mail.Subject = "this is a test email.";
mail.Body = "this is my test email Body sent to: %name%";
var header = new Header();
var recipients = new List<String> { "[email protected]", "[email protected]", "[email protected]" };
header.SetTo(recipients);
var subs = new List<String> { "A", "B", "C" };
header.AddSubstitution("%name%", subs);
string x = header.JsonString();
mail.Headers.Add("X-SMTPAPI", x);
client.SendAsync(mail, null);
from smtpapi-csharp.
Okay, you shouldn't have to worry about wrapping the lines yourself. Our library (smtpapi-csharp) doesn't contain a transport mechanism, it just helps build the custom SendGrid headers so they can be easily used with any transport. The System.Net.SmtpClient transport you're using will handle wrapping the lines to be RFC compliant.
You might also want to take a look at the sendgrid-csharp library unless you have a specific need for SMTP. It uses this library to build the headers, but then uses HTTP for the transport by calling our web API. For most cases this is faster and requires less work.
from smtpapi-csharp.
Thank you for you explanation. This is very cool and save a lot of work...
Yes I saw the sendgrid-csharp library and I've tested it with five email recipients. The following code is only a sample:
private void SendTo()
{
List<Persons> xx = new List<Persons>();
for (int i = 0; i < 10000; i++)
{
xx.Add(new Persons("FirstName " + i, "LastName " + i, "Mr.", i.ToString(), "customer" + i + "@test.xyz", "Company " + i, i.ToString()));
}
MailAddress[] sReplayTo = new MailAddress[] { new MailAddress("[email protected]") };
MailAddress[] sTo = new MailAddress[] { new MailAddress("[email protected]") };
// Create the email object first, then add the properties.
SendGrid myMessage = SendGrid.GetInstance();
myMessage.From = new MailAddress("[email protected]", "MyCompany");
myMessage.ReplyTo = sReplayTo;
myMessage.To = sTo;
myMessage.Subject = "test";
string body = "Dear {$Title} {$FirstName} {$LastName},<br/>" +
"Your Id: {$Id}<br/>" +
"Your Email: {$Email}<br/>" +
"You are working at: {$Hospital}<br/>" +
"Your Plz: {$Plz}<br/>" +
"Your sincerely,<br/>" +
"{$FirstName} {$LastName}";
myMessage.Html = body;
// Create credentials, specifying your user name and password.
var credentials = new NetworkCredential("", "");
// Create an SMTP transport for sending email depends on bSmtp
SMTP transportSmtp = SMTP.GetInstance(credentials, "smtp.sendgrid.net", 587);
// split into 1000 To recipients
int k = 1000;
var res = Enumerable.Range(0, (xx.Count - 1) / k + 1)
.Select(i => xx.GetRange(i * k, Math.Min(k, xx.Count - i * k)))
.ToList();
foreach (var item in res)
{
myMessage.ClearHeaders(); // this is my own implementation, not contained in provided assembly
myMessage.RemoveSubVal("{$FirstName}"); // this is my own implementation, not contained in provided assembly
myMessage.RemoveSubVal("{$LastName}");
myMessage.RemoveSubVal("{$Title}");
myMessage.RemoveSubVal("{$Id}");
myMessage.RemoveSubVal("{$Email}");
myMessage.RemoveSubVal("{$Hospital}");
myMessage.RemoveSubVal("{$Plz}");
myMessage.Header.AddTo(item.Select(x => x.Email));
myMessage.AddSubVal("{$FirstName}", item.Select(x => x.FirstName).ToList());
myMessage.AddSubVal("{$LastName}", item.Select(x => x.LastName).ToList());
myMessage.AddSubVal("{$Title}", item.Select(x => x.Title).ToList());
myMessage.AddSubVal("{$Id}", item.Select(x => x.Id).ToList());
myMessage.AddSubVal("{$Email}", item.Select(x => x.Email).ToList());
myMessage.AddSubVal("{$Hospital}", item.Select(x => x.Hospital).ToList());
myMessage.AddSubVal("{$Plz}", item.Select(x => x.Plz).ToList());
transportSmtp.Deliver(myMessage);
}
}
Could you please take a look at my code? Thank you very much.
I've realised that the assembly I use is in the old Version 1.2.1.
from smtpapi-csharp.
Hi there. I've looked at your code. You should really use the latest version of the sendgrid-csharp library. There are a number of changes and enhancements, and you'll be able to upgrade more easily for bug fixes.
from smtpapi-csharp.
it works, thank you!
from smtpapi-csharp.
I was trying to follow this thread -- can someone clarify whether or not we need limit the number of characters in the SMTP header? Or is limiting the number of recipients per batch good enough?
Since I have about 10 substitution values, my header goes over the 1,000-character limit with about 10 recipients. To avoid the limit, I'll pretty much be forced to send these in batches of ~5.
Thanks in advance π
from smtpapi-csharp.
Hi @djiang,
Hopefully this helps: https://sendgrid.com/docs/API_Reference/SMTP_API/using_the_smtp_api.html#-Requirements-and-Limitations
Thanks!
from smtpapi-csharp.
That's the document I was confused about -- specifically:
Ensure that the header is limited to a maximum total line length of 1,000 characters. Failure to do this can cause intermediate MTAβs to split the header on non-space boundaries, which will cause spaces to be inserted in the final resulting e-mail. Additionally, if your e-mail is going through another MTA before reaching SendGrid, it is likely to have an even lower setting for maximum header length and may truncate the header.
"Maximum total line length of 1,000 characters"
from smtpapi-csharp.
Hi @djiang,
That is the best practice. I think the solution you mentioned should work. Do you have an issue with avoiding the limit?
from smtpapi-csharp.
Hi @thinkingserious,
I was wondering if you can clarify the mean of "line length" for the header. Is it referring to the total number of character in the header, or the length of each header value? I'm currently using https://github.com/sendgrid/sendgrid-ruby and am wondering if I need to manage header line length myself.
Thanks π
from smtpapi-csharp.
Related Issues (20)
- Hacktoberfest: SendGrid Current Status
- chore: Add Code Review to Contributing.md
- Create a Use Cases Directory
- Warning: 3 References not resolved HOT 2
- (Mono) WARNING: The runtime version supported by this application is unavailable. HOT 3
- NetStandard support HOT 5
- Extra space in the substitution variable values HOT 7
- Implement auto-deploy to package manager
- Require conformance to style standards HOT 1
- Run our *.md documents in this repo through the Grammer.ly service and update HOT 2
- Create first time contributor documentation HOT 1
- Auto-generate the CHANGELOG.md file upon release HOT 1
- Document new Git workflow in CHANGELOG.md HOT 1
- How to configure everything? HOT 1
- Auto-generate the release notes upon release HOT 1
- Add our Developer Experience Engineer career opportunity to the README HOT 3
- Sign the next release assembly with a strong name HOT 1
- Concurrency Bugs HOT 1
- Not able to send multiple email to multiple recipient with same cc email address. HOT 1
- Emails getting deduped with different personalizations HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from smtpapi-csharp.