Coder Social home page Coder Social logo

Comments (11)

brandonmwest avatar brandonmwest commented on July 21, 2024

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.

kzimny avatar kzimny commented on July 21, 2024

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.

brandonmwest avatar brandonmwest commented on July 21, 2024

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.

kzimny avatar kzimny commented on July 21, 2024

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.

brandonmwest avatar brandonmwest commented on July 21, 2024

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.

kzimny avatar kzimny commented on July 21, 2024

it works, thank you!

from smtpapi-csharp.

djiang avatar djiang commented on July 21, 2024

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.

thinkingserious avatar thinkingserious commented on July 21, 2024

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.

djiang avatar djiang commented on July 21, 2024

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.

thinkingserious avatar thinkingserious commented on July 21, 2024

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.

djiang avatar djiang commented on July 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.