Hi,
I am sending an email with an embedded JPEG image from a C# windows application. I have made two views, one html view and one plain view for different html/non-html clients. I tested it with my own email and configured my outlook to receive email in plain text mode. The problem is that when the email is received, it is not showing the text defined in plain view. Rather it is showing the html tags for the picture defined in html view. The plain text view is getting totally ignored.
Is there any way of putting a condition for checking if the customer's email client supports html so as to pass on the right view? Can anyone please help?
This is the output i am getting when viewing it on outlook in plain text mode.
abcdef
Sample of my SendEmail method....
public static string SendMessageWithAttachment(string sendTo, string sendFrom,
string sendSubject, string sendMessage, ArrayList attachments)
{
try
{
sendMessage = sendMessage.Replace("\r\n", "
");
sendMessage = "
" + sendMessage;
// validate email address
//bool bTest = ValidateEmailAddress(sendTo);
//if (bTest == false)
// return "Invalid recipient email address: " + sendTo;
// Create the basic message
MailAddress from=new MailAddress("
//MailAddress to = new MailAddress(" MailAddress to = new MailAddress("[email protected]"); MailMessage bccMsg = new MailMessage(from, to); bccMsg.ReplyTo = new MailAddress("[email protected]"); bccMsg.Bcc.Add(sendTo); // bccMsg.To = ""; bccMsg.Subject = sendSubject; bccMsg.Body = sendMessage; //MailMessage message = new MailMessage( // sendFrom, // sendTo, // sendSubject, // sendMessage); // The attachments arraylist should point to a file location where // the attachment resides - add the attachments to the message //foreach (string attach in attachments) //{ // Attachment attached = new Attachment(attach, MediaTypeNames.Application.Octet); // bccMsg.Attachments.Add(attached); //} AlternateView plainView = AlternateView.CreateAlternateViewFromString("If you are not able to view the bulletin, please save the attachment to your computer open it.", System.Text.Encoding.ASCII, "text/plain"); AlternateView htmlview = AlternateView.CreateAlternateViewFromString(" //AlternateView htmlview=new AlternateView(attachments[0].ToString()); ContentType type=new ContentType(MediaTypeNames.Text.Plain); plainView.ContentType = type; for (int a = 0; a < attachments.Count; a++) bccMsg.Attachments.Add(new Attachment(attachments[a].ToString())); LinkedResource res = new LinkedResource(attachments[0].ToString()); res.ContentId = "logo"; //htmlview.LinkedResources.Add(res); bccMsg.AlternateViews.Add(plainView); bccMsg.AlternateViews.Add(htmlview); bccMsg.IsBodyHtml = true ; // create smtp client at mail server location // SmtpClient client = new SmtpClient(Properties.Settings.Default.SMTPAddress); SmtpClient client = new SmtpClient(" client.Timeout = 600000; client.UseDefaultCredentials = false; // Add credentials System.Net.NetworkCredential nc = new System.Net.NetworkCredential(" client.Credentials = nc; // send message client.Send(bccMsg); return "Message sent to " + sendTo + " at " + DateTime.Now.ToString() + "."; } catch (Exception ex) { return ex.Message.ToString(); } }" + sendMessage + "", System.Text.Encoding.Unicode, "text/html");
Suthish NairPosted Mar 12, 2011, 12:57 PM
Sam HobbsPosted Mar 10, 2011, 3:12 PM
Typically the user decides whether to get HTML format or plain text format. You need to send out one of the two formats based on whAt the user has selected. Many users will choose to receive plain text format for many reasons even when their email client supports it. Plain text is smaller and safer and has limited support for commercial messages. Smaller is better for slow connections. Plain text is safer because it is impossible to execute a script in a plain text message.
When a message is sent using HTML format, there is typically (but not always?) a copy of the message in plain text format also. I don't think it is called views but it is like a view. So you need to become familiar with lookiing at the "raw" messages; it is sometimes called the message source. In your email client, look at a message and then look at the View menu; there is probably a View menu and in that is probably a way to look at the raw message.
Look at the headers. See a "Content-Type" header? For HTML format messages it is probably "multipart/mixed". For plain text messages it is "text/plain". For multipart messages there will also be a "boundary" that separates the parts. Become familiar with all that. I hope you learn about it enough that you can explain some of it to me when I need help.
Suthish NairPosted Mar 10, 2011, 2:31 PM
Raja KrishnamurthyPosted Mar 10, 2011, 11:52 AM
HTH.
Happy Programming!!!
Regards,
Raja
Soumyajit DasguptaPosted Mar 10, 2011, 11:23 AM
So you say that I need to keep just one view going out with the emails? I can see that the HTML view is taking precedence over the plain text view. Are multiple views not allowed for 1 email message then?
Raja KrishnamurthyPosted Mar 10, 2011, 7:40 AM
HTH.
Happy Programming!!!
Regards,
Raja