I am using three classes to send email but i cant to combine text email with image, or just to send image. When i get email i see empty image. Help me to change my code so i can to send email with:
- text
- image
and style
public class SendService : IDistributionProvider { public int Send(System.Xml.Linq.XDocument recipientsData, string subject, string fromName, string fromAccount) { foreach (XElement element in recipientsData.Root.Elements()) { string email = element.Element("email").Value; string name = element.Element("name").Value; string message = element.Element("message").Value; bool result = EmailUtils.SendEmail(fromAccount, fromName, email, name, subject, message.Replace("\n", "<br/>")); } return 1; } public interface IDistributionProvider { int Send(XDocument recipientsData, string subject, string fromName, string fromAccount); } public static class EmailUtils { private static string sendHostName; private static int sendPort; private static string userName; private static string password; private static string defaultFromEmail; private static string defaultFromName; static EmailUtils() { sendHostName = ConfigurationManager.AppSettings["sendHostName"]; sendPort = int.Parse(ConfigurationManager.AppSettings["sendPort"]); defaultFromEmail = ConfigurationManager.AppSettings["fromEmail"]; defaultFromName = ConfigurationManager.AppSettings["fromName"]; string credential = Utils.DecryptString(ConfigurationManager.AppSettings["credential"]); if (!string.IsNullOrEmpty(credential) && credential.Split(";".ToCharArray()).Length > 1) { userName = credential.Split(";".ToCharArray())[0]; password = credential.Split(";".ToCharArray())[1]; } } public static bool SendEmail(string toEmail, string toName, string subject, string body) { return SendEmail(defaultFromEmail, defaultFromName, toEmail, toName, subject, body); } public static bool SendEmail(string fromEmail, string fromName, string toEmail, string toName, string subject, string body) { try { if (string.IsNullOrEmpty(toEmail)) { return false; } if (string.IsNullOrEmpty(toName)) { toName = toEmail.Substring(0, toEmail.IndexOf("@")); } if (string.IsNullOrEmpty(fromEmail)) { fromEmail = defaultFromEmail; } if (string.IsNullOrEmpty(fromName)) { fromName = defaultFromName; } Message message = new Message(); message.Charset = "UTF-8"; message.Subject = Codec.RFC2047Encode(subject, "UTF-8"); message.From = new Address(fromEmail, fromName); message.To.Add(toEmail, toName); message.BodyHtml.Format = BodyFormat.Html; message.BodyHtml.Charset = "UTF-8"; message.BodyHtml.Text = body; return ActiveUp.Net.Mail.SmtpClient.SendSsl(message, sendHostName, sendPort, userName, password, SaslMechanism.Login); } catch { return false; } }
}
In this way I send email- just text:
string bodyEmail = "<h2>Welcome to website</h2></br><div><p>Thank for using website</p></div>";
EmailUtils.SendEmail("[email protected]","xxxx","Contact",bodyEmail);
I've actually ran into this same problem and what really helped me was this. In my case I had the html and I had to parse it out using HtmlUtilityPack. I would not recommend using the encoded string as It is not fully supported, and it makes your message bloated. The cid way is also how outlook adds images to an email. I'd add code but I think the example was good enough in my case.