Mail Subject gets chopped

J

jp2msft

I've got some code that is chopping my subject line on the messages I
receive, and I do not know why.

Our server is running Active Directory and SQL Server 2000 Enterprise.

The email is sent, but all we see in the subject line is "S".

Here is what I am doing:
Code:
private bool SendMessage(string fcn)
{
MailMessage email = new MailMessage(fcn, fcn);
email.SubjectEncoding = Encoding.Unicode;
email.BodyEncoding = Encoding.Unicode;
email.Subject = string.Format("SystemID {0} - {1}", m_systemId,
m_coil.SerialNumber);
string fmt = "<html><body bgcolor={0}><div><h3>Coil {1}<br/><font
color=\"red\">{2}</font> Approved Override For {3}</h3><hr><table border=1>";
string body1 = string.Format(fmt, split.Panel1.BackColor.Name,
m_coil.SerialNumber, m_dbase.Supervisor.FullName, m_dbase.Employee.FullName);
string body2 = string.Empty;
foreach (ListViewItem i in ListView1.Items)
{
if (i.ImageKey == "stop.ico")
{
body2 += string.Format("<tr><td>{0} Issue:</td><td><b><font
color=\"green\">{1}</font></b></td></tr>", i.SubItems[0].Text,
i.SubItems[1].Text);
}
}
if (0 < body2.Length)
{
string body3 = "</table><hr></div><div>End Of File</div></body></html>";
email.IsBodyHtml = true;
email.Body = body1 + body2 + body3;
SmtpClient Client = new SmtpClient("172.16.8.200");
try
{
Client.Send(email);
}
catch (Exception err)
{
Console.WriteLine(err.Message);
}
finally
{
email.Dispose();
}
}
 
M

Matt

I've got some code that is chopping my subject line on the messages I
receive, and I do not know why.

Our server is running Active Directory and SQL Server 2000 Enterprise.

The email is sent, but all we see in the subject line is "S".

Here is what I am doing:
Code:
private bool SendMessage(string fcn)
{
  MailMessage email = new MailMessage(fcn, fcn);
  email.SubjectEncoding = Encoding.Unicode;
  email.BodyEncoding = Encoding.Unicode;
  email.Subject = string.Format("SystemID {0} - {1}", m_systemId,
m_coil.SerialNumber);
  string fmt = "<html><body bgcolor={0}><div><h3>Coil {1}<br/><font
color=\"red\">{2}</font> Approved Override For {3}</h3><hr><table border=1>";
  string body1 = string.Format(fmt, split.Panel1.BackColor.Name,
m_coil.SerialNumber, m_dbase.Supervisor.FullName, m_dbase.Employee.FullName);
  string body2 = string.Empty;
  foreach (ListViewItem i in ListView1.Items)
  {
    if (i.ImageKey == "stop.ico")
    {
      body2 += string.Format("<tr><td>{0} Issue:</td><td><b><font
color=\"green\">{1}</font></b></td></tr>", i.SubItems[0].Text,
i.SubItems[1].Text);
    }
  }
  if (0 < body2.Length)
  {
    string body3 = "</table><hr></div><div>End Of File</div></body></html>";
    email.IsBodyHtml = true;
    email.Body = body1 + body2 + body3;
    SmtpClient Client = new SmtpClient("172.16.8.200");
    try
    {
      Client.Send(email);
    }
    catch (Exception err)
    {
      Console.WriteLine(err.Message);
    }
    finally
    {
      email.Dispose();
    }}

I don't see anything obviously wrong with your formatting of the
subject. What is the type
of MailMessage.Subject?

Matt
 
J

jp2msft

Basic stuff. I made some mods so it would fill a string var first, and the
input was this: "SystemID AA_Water_08 - C189335 1107 94"

After spending time with it, I found a MS example on MSDN where they created
just about the entire email in the initialization:

email = new MailMessage(fromName, toName, subjectValue, bodyValue);

The bodyValue is Text only, so I wrote there that the document was formatted
for HTML, then filled the body with HTML and set the IsHtml flag.

Now it works. I don't really get what the problem was, but I'm up and running.

Matt said:
I've got some code that is chopping my subject line on the messages I
receive, and I do not know why.

Our server is running Active Directory and SQL Server 2000 Enterprise.

The email is sent, but all we see in the subject line is "S".

Here is what I am doing:
Code:
private bool SendMessage(string fcn)
{
MailMessage email = new MailMessage(fcn, fcn);
email.SubjectEncoding = Encoding.Unicode;
email.BodyEncoding = Encoding.Unicode;
email.Subject = string.Format("SystemID {0} - {1}", m_systemId,
m_coil.SerialNumber);
string fmt = "<html><body bgcolor={0}><div><h3>Coil {1}<br/><font
color=\"red\">{2}</font> Approved Override For {3}</h3><hr><table border=1>";
string body1 = string.Format(fmt, split.Panel1.BackColor.Name,
m_coil.SerialNumber, m_dbase.Supervisor.FullName, m_dbase.Employee.FullName);
string body2 = string.Empty;
foreach (ListViewItem i in ListView1.Items)
{
if (i.ImageKey == "stop.ico")
{
body2 += string.Format("<tr><td>{0} Issue:</td><td><b><font
color=\"green\">{1}</font></b></td></tr>", i.SubItems[0].Text,
i.SubItems[1].Text);
}
}
if (0 < body2.Length)
{
string body3 = "</table><hr></div><div>End Of File</div></body></html>";
email.IsBodyHtml = true;
email.Body = body1 + body2 + body3;
SmtpClient Client = new SmtpClient("172.16.8.200");
try
{
Client.Send(email);
}
catch (Exception err)
{
Console.WriteLine(err.Message);
}
finally
{
email.Dispose();
}}

I don't see anything obviously wrong with your formatting of the
subject. What is the type
of MailMessage.Subject?

Matt
 
B

Ben Voigt [C++ MVP]

jp2msft said:
I've got some code that is chopping my subject line on the messages I
receive, and I do not know why.

Our server is running Active Directory and SQL Server 2000 Enterprise.

The email is sent, but all we see in the subject line is "S".

Sounds like a unicode encoding issue. If you want to use non-ASCII
characters in e-mail, you have to specify the encoding in the MIME headers.
If you want to use Unicode, you have to use UTF-8 encoding. Windows'
default encoding of UTF-16 results in NUL bytes in the stream which would
explain the truncation.

At least this is all true if you use SMTP. If your e-mails are local to an
Exchange Server (you did mean Exchange, not SQL, right?) then different
rules might apply.
Here is what I am doing:
Code:
private bool SendMessage(string fcn)
{
MailMessage email = new MailMessage(fcn, fcn);
email.SubjectEncoding = Encoding.Unicode;
email.BodyEncoding = Encoding.Unicode;
email.Subject = string.Format("SystemID {0} - {1}", m_systemId,
m_coil.SerialNumber);
string fmt = "<html><body bgcolor={0}><div><h3>Coil {1}<br/><font
color=\"red\">{2}</font> Approved Override For {3}</h3><hr><table
border=1>"; string body1 = string.Format(fmt,
split.Panel1.BackColor.Name, m_coil.SerialNumber,
m_dbase.Supervisor.FullName, m_dbase.Employee.FullName); string
body2 = string.Empty; foreach (ListViewItem i in ListView1.Items)
{
if (i.ImageKey == "stop.ico")
{
body2 += string.Format("<tr><td>{0} Issue:</td><td><b><font
color=\"green\">{1}</font></b></td></tr>", i.SubItems[0].Text,
i.SubItems[1].Text);
}
}
if (0 < body2.Length)
{
string body3 = "</table><hr></div><div>End Of
File</div></body></html>"; email.IsBodyHtml = true;
email.Body = body1 + body2 + body3;
SmtpClient Client = new SmtpClient("172.16.8.200");
try
{
Client.Send(email);
}
catch (Exception err)
{
Console.WriteLine(err.Message);
}
finally
{
email.Dispose();
}
}
 
Top