how to send full html+images enabled mail to subscribers

126 views Asked by At

i have just created one newsletter application that send html enabled and image embedded mail to large no of subscribers. i have some small problem with received mail to my gmail account. can't get full html mail to read. here is my code :

private static void SendEmail(string fromName, string fromEmailAddress, string emailpassword, string smtphost, int smtpport, bool enableSSL, string toEmailAddress, string subject, string plainText, List<string> file_names, int templateID, bool IsUnsubscriptionLink, SmtpClient client)
    {
        MailMessage msg = new MailMessage();
        if (fromName != null)
            msg.From = new MailAddress(fromName + " <" + fromEmailAddress + ">");
        else
            msg.From = new MailAddress(fromEmailAddress);
        msg.To.Add(new MailAddress(toEmailAddress));
        msg.Subject = subject;
        msg.IsBodyHtml = true;
        client.Host = smtphost;
        System.Net.NetworkCredential basicauthenticationinfo = new System.Net.NetworkCredential(fromEmailAddress, emailpassword);
        client.Port = smtpport;
        client.EnableSsl = enableSSL;
        client.UseDefaultCredentials = false;
        client.Credentials = basicauthenticationinfo;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        msg.Priority = MailPriority.High;
        msg.Subject = subject;
        string rbody = ConvertAllString(plainText, templateID);
        string fbody = rbody.Replace("\"", "'");
        if (fbody.Contains("src='cid:"))
        {
            var htmlView = AlternateView.CreateAlternateViewFromString(msg.Body, null, "text/html");
            do
            {
                int src = fbody.IndexOf("src='cid:");
                if (src != -1)
                {
                    fbody = fbody.Remove(0, src + 9);
                    var dot = Regex.Match(fbody, @"\.(jpg|jpeg|gif|png)");
                    if (dot.Success)
                    {
                        int comma = fbody.IndexOf("'");
                        if (comma != -1)
                        {
                            if (!string.IsNullOrEmpty(fbody.Substring(0, comma)))
                            {
                                string imageSource = Path.Combine(HttpRuntime.AppDomainAppPath, "EmailImages\\", fbody.Substring(0, comma));
                                var leftImageLink = (dynamic)null;
                                switch (Path.GetExtension(imageSource).ToLower())
                                {
                                    case ".jpg":
                                        leftImageLink = new LinkedResource(imageSource, "image/jpg")
                                        {
                                            ContentId = fbody.Substring(0, comma),
                                            TransferEncoding = System.Net.Mime.TransferEncoding.Base64
                                        };
                                        break;
                                    case ".jpeg":
                                        leftImageLink = new LinkedResource(imageSource, "image/jpeg")
                                        {
                                            ContentId = fbody.Substring(0, comma),
                                            TransferEncoding = System.Net.Mime.TransferEncoding.Base64
                                        };
                                        break;
                                    case ".gif":
                                        leftImageLink = new LinkedResource(imageSource, "image/gif")
                                        {
                                            ContentId = fbody.Substring(0, comma),
                                            TransferEncoding = System.Net.Mime.TransferEncoding.Base64
                                        };
                                        break;
                                    case ".png":
                                        leftImageLink = new LinkedResource(imageSource, "image/png")
                                        {
                                            ContentId = fbody.Substring(0, comma),
                                            TransferEncoding = System.Net.Mime.TransferEncoding.Base64
                                        };
                                        break;
                                }
                                htmlView.LinkedResources.Add(leftImageLink);
                            }
                        }
                    }
                }
            }
            while (fbody.Contains("src='cid:"));
            msg.AlternateViews.Add(htmlView);
        }
        if (templateID > 0)
        {
            //do smething else
        }
        if (file_names.Count > 0)
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                for (int i = 0; i < file_names.Count; i++)
                {
                    Attachment mailAttachment = new Attachment(Path.Combine(HttpRuntime.AppDomainAppPath, "EmailFiles/" + file_names[i].ToString()));
                    msg.Attachments.Add(mailAttachment);
                }
            }
        }
        if (IsUnsubscriptionLink.Equals(1))
        {
            string pathToApp = ConfigurationManager.AppSettings["EASYMAIL_PathToApplication"];
            string unsubscribeText = "<br/>\r\n\r\n---------------------------\r\n<br/>" +
                                     "To unsubscribe, please click this link:\r\n" +
                                     pathToApp + "Unsubscribe.aspx";
            msg.Body = plainText + unsubscribeText;
        }
        else
        {
            msg.Body = plainText + string.Empty;
        }
        client.Send(msg);
    }

here is my gmail output for getting mail to my account :

enter image description here

how ever this my original mail generated from gmail is :

enter image description here

how ever i can't see full email rich text. guys please help me what's going wrong here...

1

There are 1 answers

0
Lalit Lakhotia On

few things about creating mail templates.

  • Always thinks in terms of table . your whole HTML is a table structure.
  • To send images as attachments , use cid.

Attached sample HTML code to show what i mean.

<!DOCTYPE html>
<head>
    <title>Sample*emphasized text*</title>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="table-layout:fixed;">
    <tr>
        <td align="left" valign="top">
            <table cellspacing="0" cellspacing="0" width="100%" align="left" valign="top">
                <tr>
                    <td align="left" valign="top">
                        <div >
                            <table>
                                <tbody>
                                <tr>
                                    <td style=" text-align: right;min-width:60px;" align="left" valign="top">
                                        <font>line1
                                            :</font>
                                    </td>
                                    <td>
                                        <font>
                                            lin2</font>
                                    </td>
                                </tr>
                                <tr style="vertical-align: top;">
                                    <td style=" text-align: right;min-width:70px;" align="left" valign="top">
                                        <font>line3
                                            :</font>
                                    </td>
                                    <td style="hyphens: auto" align="left" valign="top">
                                        <font>
                                            how to show atachements inplace </font>
                                        <div style="overflow: scroll">
                                            <img src="cid:va" alt="">
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2" height="10" align="left" valign="top"></td>
                                </tr>
                                </tbody>
                            </table>
                            <div>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
</body> </html>
  • HTML max size can be exactly 102KB.

example


var v = []
    v.push({filename: "file_name_to_show", path: 'path_to_image', cid:'va'});

var mail = {
        from: "from_id",
        to: "to_id",
        subject: "adasda",
        text: "sample format",
        html: 'attach html object',
        attachments: v

    };

in HTML just define image source as

<img src="cid:va">