472,965 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,965 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 9152
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
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.