By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,165 Members | 1,916 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,165 IT Pros & Developers. It's quick & easy.

Sending email using System.Net.Mail worked ok untill this morning exception popup

P: 69
Hi,

Im using System.Net.Mail to send email in my application it was working great without any problems untill this morning after like 20-30 tries it was sending the email ok then suddenly this morning i got an exception and i cant figure out what/where the problem is and how to fix it.

The problem say i have some invalid char:

invalid character was found in the mail header: 'י'.
Now this char 'י' is an hebrew letter. But i searched over and over my code all day and this char dosent exist anywhere! I cant understadn why and where from this came up. It worked perfect for the last two days then this morning this exception.

I tried to clear the log file im sending wich is txt file i tried to add some encodinds in my code for the header didnt work. Something unf8 encoding. I tried to change the email address im sending to but didnt work.
I cant figure out how come it worked perfect and this morning suddenly it dosent work.

Maybe something with the gmail smtp address and port changed in the last few hours? Im sending the email from my gmail account to my regular account and it worked no problems!
I tried google all day long and tried what ppl said with encoding but it didnt help maybe i didnt do it right but it didnt help. This is crazy since it worked so good and now it dosent.

Here is my email sending code:

Expand|Select|Wrap|Line Numbers
  1. private void button8_Click(object sender, EventArgs e)
  2.          {
  3.              try
  4.              {
  5.                  if (File.Exists(logger_file_to_read + tlf))
  6.                  {
  7.                      File.Delete(logger_file_to_read + tlf);
  8.                  }
  9.                      File.Copy(logger_file_to_read + log_file_name, logger_file_to_read + tlf);
  10.                  message = new MailMessage(
  11.                    "chocolade@gmail.com",
  12.                    "bout09@bezeqint.net",
  13.                    "Log File Report",
  14.                    "See the attached log file and check for errors as the user have requested.");
  15.                  data = new Attachment(logger_file_to_read + tlf, MediaTypeNames.Application.Octet);
  16.                  ContentDisposition disposition = data.ContentDisposition;
  17.                  disposition.CreationDate = System.IO.File.GetCreationTime(tlf);
  18.                  disposition.ModificationDate = System.IO.File.GetLastWriteTime(tlf);
  19.                  disposition.ReadDate = System.IO.File.GetLastAccessTime(tlf);
  20.                  // Add the file attachment to this e-mail message.
  21.                  message.Attachments.Add(data);
  22.                  SmtpClient ss = new SmtpClient("smtp.gmail.com", 587);
  23.                  ss.EnableSsl = true;
  24.                  ss.Timeout = 10000;
  25.                  ss.DeliveryMethod = SmtpDeliveryMethod.Network;
  26.                  ss.UseDefaultCredentials = false;
  27.                  ss.Credentials = new NetworkCredential("chocolade13091972", "ynnadaniel");
  28.                  ss.SendCompleted+=new SendCompletedEventHandler(ss_SendCompleted);
  29.                  string userState = "test message1";
  30.                  ss.SendAsync(message,userState);
  31.                  label11.Enabled = true;
  32.                  label11.Visible = true;
  33.                  label11.BackColor = Color.DarkSeaGreen;
  34.                  label11.Text = "Sending email please wait"; 
  35.              }
  36.              catch (Exception errors)
  37.              {
  38.                  Logger.Write("Error sending message :" + errors);
  39.              }
  40.          }
  41.          private void ss_SendCompleted(object sender, AsyncCompletedEventArgs e)
  42.          {
  43.              String token = (string)e.UserState;
  44.              if (e.Cancelled)
  45.              {
  46.                  Logger.Write("[{0}] Send canceled."+ token);
  47.              }
  48.              if (e.Error != null)
  49.              {
  50.                  label11.Enabled = true;
  51.                  label11.Visible = true;
  52.                  label11.BackColor = Color.DarkSeaGreen;
  53.                  label11.Text = "There was a problem with sending the log file please try again later and check the log file for more information";
  54.                  Logger.Write("There was a problem with sending the log file please try again later and check the log file for more information :" + e.Error.ToString());
  55.              }
  56.              else
  57.              {
  58.                  label11.Enabled = true;
  59.                  label11.Visible = true;
  60.                  label11.BackColor = Color.DarkSeaGreen;
  61.                  label11.Text = "Email have sent successfully";
  62.                  Logger.Write("Attached log file have been sent");
  63.              }
  64.              message.Dispose();
  65.              File.Delete(logger_file_to_read + tlf);
  66.          }
  67.  

Thanks for helping.
Oct 25 '10 #1
Share this Question
Share on Google+
6 Replies


Plater
Expert 5K+
P: 7,872
HAve you verified which line of the code throws the error, or does it just happen on the SendAsync?
Also, if you cast to the actually exception type, you can probably get more details about where the error occured. (Like if it happened during the DATA section or the RCPT TO section, etc)
Oct 25 '10 #2

P: 69
Plater it dosent show wich line.
Here is the exception i copied from the error:

25/10/2010 22:51:51 -> There was a problem with sending the log file please try again later and check the log file for more information :System.Net.Mail.SmtpException: Failure sending mail. ---> System.FormatException: An invalid character was found in the mail header: 'י'.
at System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult)
at System.Net.Mail.SmtpClient.SendMessageCallback(IAs yncResult result)
--- End of inner exception stack trace ---

I will try to find wich line is it i think its on the sending line: ss.SendAsync(message,userState);
Cuz i used a breakpoint and moved line by line and the error came up after the SendAsync not before.

Plater here is the full exception on the send line:

Expand|Select|Wrap|Line Numbers
  1. 25/10/2010 22:59:51 -> error :System.Net.Mail.SmtpException: Failure sending mail. ---> System.FormatException: An invalid character was found in the mail header: 'י'.
  2.    at System.Net.Mime.MailBnfHelper.GetTokenOrQuotedString(String data, StringBuilder builder)
  3.    at System.Net.Mime.ContentDisposition.ToString()
  4.    at System.Net.Mime.ContentDisposition.PersistIfNeeded(HeaderCollection headers, Boolean forcePersist)
  5.    at System.Net.Mime.MimePart.Send(BaseWriter writer)
  6.    at System.Net.Mime.MimeMultiPart.Send(BaseWriter writer)
  7.    at System.Net.Mail.SmtpClient.Send(MailMessage message)
  8.    --- End of inner exception stack trace ---
  9.    at System.Net.Mail.SmtpClient.Send(MailMessage message)
  10.    at WindowsFormsApplication1.Form1.button8_Click(Object sender, EventArgs e) in D:\C-Sharp\Download File\Downloading-File-Project-Version-010\Downloading File\Downloading File\Form1.cs:line 1664
  11.  
And line 1664 in my code is the sending line: ss.SendAsync(message,userState);

Any ideas what cuz make this exception?
I tried also instead sendasync just: ss.Send(message);
But same exception.

Thanks.
Oct 25 '10 #3

P: 2
Hi,

I am encoutring the same problem.

Did you find a solution for this?

Thanks.
Apr 12 '11 #4

P: 69
Eyalk Hi ill post here my code wich is working since then if im not mistaken i changed almost the whole code of sending the email.

Expand|Select|Wrap|Line Numbers
  1. private void SendLogFile_Click(object sender, EventArgs e)
  2.         {
  3.             try
  4.             {
  5.                 MailAddress from = new MailAddress("chocolade@gmail.com", "User " + (char)0xD8 + " Name",
  6.                 System.Text.Encoding.UTF8);
  7.                 MailAddress to = new MailAddress("here you put your own original email or another email to get the email");
  8.                 message = new MailMessage(from, to);
  9.                 message.Body = "Please check the log file attachment i have some bugs.";
  10.                 string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
  11.                 message.Body += Environment.NewLine + someArrows;
  12.                 message.BodyEncoding = System.Text.Encoding.UTF8;
  13.                 message.Subject = "Log File For Checking Bugs" + someArrows;
  14.                 message.SubjectEncoding = System.Text.Encoding.UTF8;
  15.                 Attachment myAttachment = new Attachment(logger_file_to_read + tlf, MediaTypeNames.Application.Octet);
  16.                 message.Attachments.Add(myAttachment);
  17.                 SmtpClient ss = new SmtpClient("smtp.gmail.com", 587);
  18.                 ss.SendCompleted += new SendCompletedEventHandler(ss_SendCompleted);
  19.                 ss.EnableSsl = true;
  20.                 ss.Timeout = 10000;
  21.                 ss.DeliveryMethod = SmtpDeliveryMethod.Network;
  22.                 ss.UseDefaultCredentials = false;
  23.                 ss.Credentials = new NetworkCredential("password if you need autheniticate password username for your email", "same here if autheniticate is needed");
  24.                 string userState = "test message1";
  25.                 ss.SendAsync(message, userState);
  26.                 label3.Enabled = true;
  27.                 label3.Visible = true;
  28.                 label3.BackColor = Color.DarkSeaGreen;
  29.                 label3.Text = "Sending email please wait";
  30.             }
  31.  
  32.             catch (Exception errors)
  33.             {
  34.                 Logger.Write("Error sending message :" + errors);
  35.             }
  36.         }
  37.  
Tell me if its working for you or not.

Thanks.
Apr 12 '11 #5

P: 69
Eyalk and here is the code for the ss.SendCompleted += new SendCompletedEventHandler(ss_SendCompleted);

Wich is when the file/email seding sinished completed.

Expand|Select|Wrap|Line Numbers
  1. private void ss_SendCompleted(object sender, AsyncCompletedEventArgs e)
  2.         {
  3.             timerdelay = 0;
  4.             timer1.Start();
  5.             String token = (string)e.UserState;
  6.             if (e.Cancelled)
  7.             {
  8.                 Logger.Write("[{0}] Send canceled." + token);
  9.                 SendLogFile.Enabled = true;
  10.             }
  11.             if (e.Error != null)
  12.             {
  13.                 label3.Enabled = true;
  14.                 label3.Visible = true;
  15.                 label3.BackColor = Color.DarkSeaGreen;
  16.                 label3.Text = "There was a problem with sending the log file please try again later and check the log file for more information";
  17.                 Logger.Write("There was a problem with sending the log file please try again later and check the log file for more information :" + e.Error.ToString());
  18.                 SendLogFile.Enabled = true;
  19.             }
  20.             else
  21.             {
  22.                 message.Dispose();
  23.                 label3.Enabled = true;
  24.                 label3.Visible = true;
  25.                 label3.BackColor = Color.DarkSeaGreen;
  26.                 label3.Text = "Email have been sent successfully";
  27.                 Logger.Write("Attached log file have been emailed successfully");
  28.                 File.Delete(logger_file_to_read + log_file_name);
  29.                 SendLogFile.Enabled = false;
  30.             }
  31.         }
  32.  
I added a timer in the completed event so when im getting any message for example if the email sent successfully the message will move after few seconds i think 5 seconds or so...so it wont stay there all the time say the message sent successfully it might be confusing.
Apr 12 '11 #6

P: 2
Thanks.
It works.
The reason that it didn't work is because that the region & language format was set to 'Hebrew'.
Once you removed the following code You solved the problems.
The other solution is to change the regional settings format to English.

Expand|Select|Wrap|Line Numbers
  1. ContentDisposition disposition = data.ContentDisposition; 
  2.                  disposition.CreationDate = System.IO.File.GetCreationTime(tlf); 
  3.                  disposition.ModificationDate = System.IO.File.GetLastWriteTime(tlf); 
  4.                  disposition.ReadDate = System.IO.File.GetLastAccessTime(tlf);

Thanks for the help!
Apr 12 '11 #7

Post your reply

Sign in to post your reply or Sign up for a free account.