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.
<img src=cid:logo><font face="Arial" size=2><B><BR><BR>abcdef</B></font>
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", "<BR>");
sendMessage = "<BR><BR>" + 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("<From Email>");
//MailAddress to = new MailAddress("<To Email");
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("<img src=cid:logo><font face=\"Arial\" size=2><B>" + sendMessage + "</B></font>", System.Text.Encoding.Unicode, "text/html");
//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("<smtp server name>", 25);
client.Timeout = 600000;
client.UseDefaultCredentials = false;
// Add credentials
System.Net.NetworkCredential nc = new System.Net.NetworkCredential("<username", "<password>");
client.Credentials = nc;
// send message
client.Send(bccMsg);
return "Message sent to " + sendTo + " at " + DateTime.Now.ToString() + ".";
}
catch (Exception ex)
return ex.Message.ToString();