473,418 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

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

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
6 9181
Plater
7,872 Expert 4TB
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
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
eyalk
2
Hi,

I am encoutring the same problem.

Did you find a solution for this?

Thanks.
Apr 12 '11 #4
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
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
eyalk
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

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

Similar topics

8
by: Nick Li | last post by:
Hi, I am trying to writing a c program to send email using system() function call on Unix(Sun Solaris). I tried the following: #include <stdio.h> #include <stdio.h> int main(void) {
6
by: Robert Pettersson | last post by:
Hi, I have built an webapp in C# that uses SMTPMail to send mail to users. When I installed it on the prodserver everything worked fine for me, but the users can not send mail. They get Access...
2
by: Ray5531 | last post by:
I'm trying to attach a file into an email message and send through System.Web.Mail,but apparently I have to write the attachment in the File system first and then attach it.I'd like to use the...
2
by: ChrisA | last post by:
I'm sending email from my ASP.NET 1.1 app, and it works fine providing the BodyFormat of the System.Web.Mail.MailMessage is set to Text. If I change to Html format, it breaks. Here's the code: ...
6
by: lalit26011983 | last post by:
Can any1 help me... How to send emails using asp.net.. I m using windows 2000 server and visual studio 2003(.net framework 1.1) I m not getting any error while i m sending email using...
0
by: Sister Ray | last post by:
I'm trying to create a simple form that sends an email using my company's exchange server. I'm using the System.Net.Mail Namespace of the .net framework 2.0. I've googled everywhere and i think my...
3
by: =?Utf-8?B?UGhpbGlw?= | last post by:
I switched from System.Web.Mail to System.Net.Mail, however I am now reconsidering that move because if I send an email to an email address in the form john.doe@yahoo.com, I receive an error...
0
by: trivedimca2005 | last post by:
I am using below code for sending Email using .net. Its not working on client machine. My server IP is:- 192.168.10.10 and also uses proxy and user-name and password for proxy settings are...
0
by: =?Utf-8?B?amZiZXJtZWpvcA==?= | last post by:
How can I send an email using System.Net.Mail digitally signed? -- Jose
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.