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
- private void button8_Click(object sender, EventArgs e)
- {
- try
- {
- if (File.Exists(logger_file_to_read + tlf))
- {
- File.Delete(logger_file_to_read + tlf);
- }
- File.Copy(logger_file_to_read + log_file_name, logger_file_to_read + tlf);
- message = new MailMessage(
- "chocolade@gmail.com",
- "bout09@bezeqint.net",
- "Log File Report",
- "See the attached log file and check for errors as the user have requested.");
- data = new Attachment(logger_file_to_read + tlf, MediaTypeNames.Application.Octet);
- ContentDisposition disposition = data.ContentDisposition;
- disposition.CreationDate = System.IO.File.GetCreationTime(tlf);
- disposition.ModificationDate = System.IO.File.GetLastWriteTime(tlf);
- disposition.ReadDate = System.IO.File.GetLastAccessTime(tlf);
- // Add the file attachment to this e-mail message.
- message.Attachments.Add(data);
- SmtpClient ss = new SmtpClient("smtp.gmail.com", 587);
- ss.EnableSsl = true;
- ss.Timeout = 10000;
- ss.DeliveryMethod = SmtpDeliveryMethod.Network;
- ss.UseDefaultCredentials = false;
- ss.Credentials = new NetworkCredential("chocolade13091972", "ynnadaniel");
- ss.SendCompleted+=new SendCompletedEventHandler(ss_SendCompleted);
- string userState = "test message1";
- ss.SendAsync(message,userState);
- label11.Enabled = true;
- label11.Visible = true;
- label11.BackColor = Color.DarkSeaGreen;
- label11.Text = "Sending email please wait";
- }
- catch (Exception errors)
- {
- Logger.Write("Error sending message :" + errors);
- }
- }
- private void ss_SendCompleted(object sender, AsyncCompletedEventArgs e)
- {
- String token = (string)e.UserState;
- if (e.Cancelled)
- {
- Logger.Write("[{0}] Send canceled."+ token);
- }
- if (e.Error != null)
- {
- label11.Enabled = true;
- label11.Visible = true;
- label11.BackColor = Color.DarkSeaGreen;
- label11.Text = "There was a problem with sending the log file please try again later and check the log file for more information";
- 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());
- }
- else
- {
- label11.Enabled = true;
- label11.Visible = true;
- label11.BackColor = Color.DarkSeaGreen;
- label11.Text = "Email have sent successfully";
- Logger.Write("Attached log file have been sent");
- }
- message.Dispose();
- File.Delete(logger_file_to_read + tlf);
- }
Thanks for helping.