MailMessage with a large body

G

Guest

Hi There.

I have a small c# problem. I am trying to create a dynamic email body. The
body may be larger than the maximum number of characters a string can have
(256?). I build the string like this:
private void SendEmail()
{
string body = "";

foreach(row in a dataset)
{
body += "First Name: " + dr["FNAME"].ToString();
body += "Last Name: " + dr["LNAME"].ToString();
....a number of different fields to follow.....

body +=" <br><hr>";//add some divider and go to the next record
}

MailMessage msg = new MailMessage();
msg.Body = body; //add the generated text to the email body

SmtpServer smtpServer = new SmtpServer();
smtpServer.Send(msg);
}

Now the email gets sent without any problems, but the body of the email is
cut-off after a certain number of characters.

How can I create an email body that can grow the way I need it to?

Thanks in advance.



Thanks,

John Scott.
 
J

John Timney \(ASP.NET MVP\)

I believe the max size of a string in .NET is 2147483647 characters,
although you would be mad to use a string of this size when you could use
stringbuilder which is optimized for large string handling.

This type of restriction normally occurs because your mail server imposes a
restriction on the length of individual lines in a message body. The SMTP
specifications suggests 76 characters per line, so ideally you'll need to
add carriage returns into your string at these lenghts.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
 
G

Guest

Thanks for the quick reply John.....

So...becuase I'm trying to generate an HTML email should I be trying to
include a new line like this?

body += "<strong>First Name:</strong>" + dr["FIRSTNAME"] + "<BR>\n";


....is that what you mean by adding carriage returns to the string?

On a side note, if I used this:
StringBuilder builder = new StringBuilder();
builder.Append("<strong>First Name:</strong>" + dr["FIRSTNAME"] + "<BR>";

msg.Body = builder.ToString();
SmtpServer.Send(msg);

would that accomplish what I'm trying to do with this large string?

Thanks again for your help.

John.



John Timney (ASP.NET MVP) said:
I believe the max size of a string in .NET is 2147483647 characters,
although you would be mad to use a string of this size when you could use
stringbuilder which is optimized for large string handling.

This type of restriction normally occurs because your mail server imposes a
restriction on the length of individual lines in a message body. The SMTP
specifications suggests 76 characters per line, so ideally you'll need to
add carriage returns into your string at these lenghts.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director


John Scott said:
Hi There.

I have a small c# problem. I am trying to create a dynamic email body. The
body may be larger than the maximum number of characters a string can have
(256?). I build the string like this:
private void SendEmail()
{
string body = "";

foreach(row in a dataset)
{
body += "First Name: " + dr["FNAME"].ToString();
body += "Last Name: " + dr["LNAME"].ToString();
....a number of different fields to follow.....

body +=" <br><hr>";//add some divider and go to the next record
}

MailMessage msg = new MailMessage();
msg.Body = body; //add the generated text to the email body

SmtpServer smtpServer = new SmtpServer();
smtpServer.Send(msg);
}

Now the email gets sent without any problems, but the body of the email is
cut-off after a certain number of characters.

How can I create an email body that can grow the way I need it to?

Thanks in advance.



Thanks,

John Scott.
 
G

Guest

Hi again,

I did end up using the StringBuilder and on each line where I include a
"<br>" tag I also included a \n ---> "<br>\n"

This seemed to fix my problem and I was able to generate a larger email
body...

Thanks again for the help.


John.




John Timney (ASP.NET MVP) said:
I believe the max size of a string in .NET is 2147483647 characters,
although you would be mad to use a string of this size when you could use
stringbuilder which is optimized for large string handling.

This type of restriction normally occurs because your mail server imposes a
restriction on the length of individual lines in a message body. The SMTP
specifications suggests 76 characters per line, so ideally you'll need to
add carriage returns into your string at these lenghts.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director


John Scott said:
Hi There.

I have a small c# problem. I am trying to create a dynamic email body. The
body may be larger than the maximum number of characters a string can have
(256?). I build the string like this:
private void SendEmail()
{
string body = "";

foreach(row in a dataset)
{
body += "First Name: " + dr["FNAME"].ToString();
body += "Last Name: " + dr["LNAME"].ToString();
....a number of different fields to follow.....

body +=" <br><hr>";//add some divider and go to the next record
}

MailMessage msg = new MailMessage();
msg.Body = body; //add the generated text to the email body

SmtpServer smtpServer = new SmtpServer();
smtpServer.Send(msg);
}

Now the email gets sent without any problems, but the body of the email is
cut-off after a certain number of characters.

How can I create an email body that can grow the way I need it to?

Thanks in advance.



Thanks,

John Scott.
 
J

John Timney \(ASP.NET MVP\)

glad to have helped :0)

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

John Scott said:
Hi again,

I did end up using the StringBuilder and on each line where I include a
"<br>" tag I also included a \n ---> "<br>\n"

This seemed to fix my problem and I was able to generate a larger email
body...

Thanks again for the help.


John.




John Timney (ASP.NET MVP) said:
I believe the max size of a string in .NET is 2147483647 characters,
although you would be mad to use a string of this size when you could use
stringbuilder which is optimized for large string handling.

This type of restriction normally occurs because your mail server imposes a
restriction on the length of individual lines in a message body. The SMTP
specifications suggests 76 characters per line, so ideally you'll need to
add carriage returns into your string at these lenghts.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director


John Scott said:
Hi There.

I have a small c# problem. I am trying to create a dynamic email body. The
body may be larger than the maximum number of characters a string can have
(256?). I build the string like this:
private void SendEmail()
{
string body = "";

foreach(row in a dataset)
{
body += "First Name: " + dr["FNAME"].ToString();
body += "Last Name: " + dr["LNAME"].ToString();
....a number of different fields to follow.....

body +=" <br><hr>";//add some divider and go to the next record
}

MailMessage msg = new MailMessage();
msg.Body = body; //add the generated text to the email body

SmtpServer smtpServer = new SmtpServer();
smtpServer.Send(msg);
}

Now the email gets sent without any problems, but the body of the email is
cut-off after a certain number of characters.

How can I create an email body that can grow the way I need it to?

Thanks in advance.



Thanks,

John Scott.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top