Hi there.
At work we want to create a email applicaion that send a HTML / Plain Text Message but with or without a attachment... We created it in a windows forms application. If you send a normal HTML / Text Mail using outlook... the server auto generate a disclaimer at the botom of the message everytime. the problem begins with...
1. When we send a mail without an attachment... the HTML / Text message message display correct with the disclaimer.
2. When we send a mail with an attachment... the HTML / Text message do not display at all... also with the disclaimer.
Here follow the code we have...
try { from = "\"Lithotech Account in C# Application\" <" + txtFrom.Text + ">"; to = txtTo.Text; user = txtTo.Text; subject = txtSubject.Text; message = txtMessage.Text; SmtpClient client = new SmtpClient("155.236.11.143", 25); client.Credentials = new NetworkCredential(username, password); client.EnableSsl = false; StringBuilder mailBody = new StringBuilder("."); int n = user.IndexOf("@"); if (n != -1) { mailBody.AppendLine("Dear " + user.Substring(0, n) + ""); } mailBody.AppendLine(" "); mailBody.AppendLine("" + message + " "); MailMessage msg = new MailMessage(from, to, subject, mailBody.ToString()); msg.IsBodyHtml = true; DialogResult attachment = MessageBox.Show("Do you want to add any attachments?", "Attachments", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (attachment == DialogResult.Yes) { Cursor.Current = Cursors.WaitCursor; OpenFileDialog file = new OpenFileDialog(); file.Filter = "Text Files (*.txt)|*.txt|C Sharp Files (*.cs)|*.cs"; file.Title = "Add attachments";
if (file.ShowDialog() == DialogResult.Cancel) return; FileStream fstream; try { fstream = new FileStream(file.FileName, FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(fstream); while (reader.Peek() >= 0) { string str = reader.ReadLine(); richTextBox1.Text += str + '\n'; } Attachment attatchment = new Attachment(file.FileName); msg.Attachments.Add(attatchment); string AttFileName = new System.IO.FileInfo(@file.FileName).Name; MessageBox.Show(AttFileName + " as Attachment added", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); Cursor.Current = Cursors.WaitCursor; client.Send(msg); Cursor.Current = Cursors.WaitCursor; MessageBox.Show("Message and attachment sent successfully!", "Sent Message", MessageBoxButtons.OK, MessageBoxIcon.Information); Cursor.Current = Cursors.Default; txtTo.Clear(); txtSubject.Clear(); txtMessage.Clear(); txtTo.Focus(); } catch (Exception) { MessageBox.Show("Error opening file", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } else if (attachment == DialogResult.No) { Cursor.Current = Cursors.WaitCursor; client.Send(msg); MessageBox.Show("Message sent successfully!", "Sent Message", MessageBoxButtons.OK, MessageBoxIcon.Information); Cursor.Current = Cursors.Default; txtTo.Clear(); txtSubject.Clear(); txtMessage.Clear(); txtTo.Focus(); } } catch (Exception ex) { MessageBox.Show("Unable to send message due to the following reason: " + '\n' + ex.Message); } }
|
Suthish NairPosted Jan 21, 2011, 2:51 PM
Couple of suggestions you need to test.
1. Check the Outlook configuration, how the mails to be displayed. In html or text format.
2. Try sending the mail to other mailing server for example lotus, gmail etc and check the formats.
3. Try putting some more break lines after..
mailBody.AppendLine("
" + message + "
");mailBody.AppendLine("
");
4. Also check Our recommended articles section.
Hi Vijay,
Here we have lots of articles available. If none of it was not helpful then only share external websites.
vijayPosted Jan 21, 2011, 3:54 AM
For sending mail..i found solution in this blog plz checkout.......
http://vbdotnetaddict.blogspot.com
But there is a code for without attachment ,u can send mail......