Sending HTML using SMTP relay

I want to be able to read an html page that is created with classic ASP (dynamic) and send that data using my SMTP relay account (Sendgrid). Sendgrid requires that the email complies with RFC 5321 which limits line length to 78 characters.

I've built all of the dynamic aspects of this solution and tested Sendgrid and JangoMail. I usually end up with line breaks in the email that cause problems with it rendering correctly as html.

The following is from my header

This is a multi-part message in MIME format.

------=_NextPart_000_01A5_01CEB2FF.440117D0
Content-Type: text/plain;
	charset="utf-8"
Content-Transfer-Encoding: 7bit

Open in new window


Based on other articles I've read, it seems like I need to change the "content-type" to "html/text", not "text/plain", but I've been unsuccessful.

Can you help provide a solution?
skimoxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
You've given no code to know what you are using but I will assume CDOSYS
When building the body the syntax is

email_component_name.HTMLBody

email_component_name being the name you assigned to the control
You are probably using TextBody
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I use sendgrid for a half dozen accounts and send cdo mail just using sendgrid as the smtp server.  

I have html mail I have always sent out without issue directly from the server using dynamically generated html from asp code.  Once I moved  to sendgrid, I had some issues with line breaks in odd places.  I spent a lot of time racking my head on this and the very last thing I tried was getting rid of white space and that did the trick.

In other words change
<p>Stuff to say</p> <p>More stuff</p> 

Open in new window

to
<p>Stuff to say</p><p>More stuff</p> 

Open in new window

0
skimoxAuthor Commented:
Here's the code that I'm using for CDO...

  Public Sub setBody_Template_html(SM_body)
    Dim Content, regEx, regExSub, SubMatch, Matches, Match, FullUrl, theFullPath
    ' Use file as body
    Content = ""
    contentType = "html"
    
    if InStr(SM_body, "://") = 0 then
      SM_body = getCurrentPath() & SM_body
    end if
    
    Content = getUrl(SM_body)
    theFullPath = getPath(SM_body)
    
    Set regEx = New RegExp   ' Create a regular expression.
    regEx.Pattern = "[=""]\/?([^""\s]*(\.gif|\.jpg|\.jpeg|\.png|\.css|\.js))[""\s]"   ' Set pattern.
    regEx.IgnoreCase = True   ' Set case insensitivity.
    regEx.Global = True   ' Set global applicability.
    
    Set regExSub = New RegExp   ' Create a regular expression.
    regExSub.Pattern = regEx.Pattern
    regExSub.IgnoreCase = True   ' Set case insensitivity.
    regExSub.Global = True   ' Set global applicability.
    
    Set Matches = regEx.Execute(Content)   ' Execute search.    
    For Each Match in Matches   ' Iterate Matches collection.
      if InStr(Match, "://") = 0 and  InStr(LCase(Match), "mailto:") = 0 and InStr(LCase(Match), "javascript:") = 0 then
        SubMatch = regExSub.Replace(Match,"$1")   ' Execute submatch search. 
        FullUrl = theFullPath & SubMatch        
        addHtmlImage SubMatch, FullUrl
      end if
    Next
    
    regEx.Pattern = "href\s*=\s*""\/?([^""]*)"""   ' Set pattern.
    regExSub.Pattern = regEx.Pattern
    
    Set Matches = regEx.Execute(Content)   ' Execute search.    
    For Each Match in Matches   ' Iterate Matches collection.
      if InStr(Match, "://") = 0 and  InStr(LCase(Match), "mailto:") = 0 and InStr(LCase(Match), "javascript:") = 0 then
        SubMatch = regExSub.Replace(Match,"$1")   ' Execute submatch search. 
        if SubMatch <> "#" then
          FullUrl = theFullPath & SubMatch
          addHtmlLink SubMatch, FullUrl
        end if
      end if
    Next
    m_Content = Content
    
    FixUpHtmlContent
  End Sub

Open in new window

0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

skimoxAuthor Commented:
Thanks for the suggestion to remove white space. I noticed an improvement a few months ago when I removed white spaces, but it didn't completely eliminate the problem.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
It looks like you are using a file and I don't see how you are actually creating the html.  Can you provide your rendered html and a sample of what the content from m_Content will look like?
0
skimoxAuthor Commented:
Below is the rendered html. Notice the break on lines 141-142.

MIME-Version: 1.0
Content-Type: multipart/alternative;
	boundary="----=_NextPart_000_011F_01CEB2F5.8272DB20"
X-Mailer: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.1.7601.17609
X-SG-EID: gxxhOZmamIeAWFKbfBDHIIOIQJj5F0dUzKUAnSYpE0KNl72xDQcZtIrAkvkUaoMjW01y2kxH6pCSwOKI6RYzmBJxlBHw/NjG8gBK74VThcQB5u3dQBuXWcCZeH0iw1aua0ne+8nCzpkM6CuTAMr8bCVbbTmd4w1JYEtkQLQkeOg=

This is a multi-part message in MIME format.
------=_NextPart_000_011F_01CEB2F5.8272DB20
Content-Type: text/plain;
	charset="utf-8"
Content-Transfer-Encoding: 7bit

	Email not displaying correctly? View it in your browser.
<http://secure.pledgemine.com/emails/etmp_24.asp?id_prf=301826&secure=19
8550> 
 <http://secure.pledgemine.com/SPC/682/usr/web/682-Note_Top.jpg> 

Dear Todd,

This month, we welcomed 10,600 students to campus to begin their fall
classes.

Our students come to William Paterson expecting that the education they
receive will open doors, challenge them to be their best, and train
them to be tomorrow's leaders. Alumni gifts to the Fund for WP play an
integral part in creating those experiences, and we thank you for your
past support! To renew your support again this year, please visit
www.wpunj.edu/giving?ref=FALL14A.

Sincerely,
Nancy Norville
Director, Annual Giving
	 <http://secure.pledgemine.com/SPC/img/Note_Side.jpg> 	
 <http://secure.pledgemine.com/SPC/682/usr/web/682-WP_Email_v1.jpg> 	
5 THINGS TO DO THIS FALL


1.	Make a gift: www.wpunj.edu/giving. Now you can set up monthly
recurring gifts!
2.	Update your contact information to receive news and invitations
here <https://secure.wpunj.edu/wpblue/alumni/newaddress.cfm> . 
3.	Alumni entrepreneurs can join our free online business
directory <https://webapps.wpunj.edu/alumni/directory/register.cfm> ,
and all alumni are eligible for select discounts at participating
businesses.
4.	Attend Homecoming weekend <http://www.wpunj.edu/homecoming/> ,
September 28th-29th.
5.	Show your Pioneer pride at an upcoming athletic event
<http://wpupioneers.com/> .

Connect with William Paterson University
<http://www.facebook.com/wpunj>    <https://twitter.com/wpunj_alumni>
<http://www.youtube.com/channel/UCSfH6lgeDREB4xQoWDa1mpw>
<http://www.linkedin.com/groups?home=&gid=82728&trk=anet_ug_hm>   	
Contact the Office of Annual Giving
<mailto:annualfund@wpunj.edu?subject=5 Things Email>  with any
questions regarding this email. Unsubscribe
<mailto:annualfund@wpunj.edu?subject=Unsubscribe> 

------=_NextPart_000_011F_01CEB2F5.8272DB20
Content-Type: text/html;
	charset="utf-8"
Content-Transfer-Encoding: 7bit




<html> 
<head> 
<title>The Fund for WP</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0">


<table width="100%" cellpadding="0" cellspacing="0" bgcolor="#DEDED6" style="background-color:#DEDED6;">
	<tr>
		<td valign="top" align="center">
			<table width="550" border="0" cellpadding="0" cellspacing="0">
				<tr>
					<td></td>
					<td style="padding:6px 0"><div align="right" style="font-size:10px;color:#000000;line-height:200%;font-family:verdana;">Email not displaying correctly? <a href="http://secure.pledgemine.com/emails/etmp_24.asp?id_prf=301826&secure=198550" target="_blank">View it in your browser.</a></div></td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td valign="top" align="center">
			<table width="550" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFF" style="background-color:#FFF;">
				<tr>
					<td width="340" valign="bottom" style="paddi
 ng-top:6px; background-color:#ffffff;"><img style="display:block" src="http://secure.pledgemine.com/SPC/682/usr/web/682-Note_Top.jpg" width="340" height="60"><table width="340" valign="bottom" border="0" cellpadding="0" cellspacing="0" id="yellow_note" style="background-color: #fffff0;">
							<tr>
								<td width="340" align="left" valign="top" style="padding: 0px 0px 0px 29px; margin:0;"><p style="font-size:13px;font-family: Helvetica, Arial, sans-serif;line-height:19px;color:#666;margin:0px;"><br />Dear Todd,<br /><br />This month, we welcomed 10,600 students to campus to begin their fall classes.<br><br>Our students come to William Paterson expecting that the education they receive will open doors, challenge them to be their best, and train them to be tomorrow's leaders. Alumni gifts to the Fund for WP play an integral part in creating those experiences, and we thank you for your past support! To renew your support again this year, please visit www.wpunj.edu/giving?re
 f=FALL14A.<br><br>Sincerely,<br>Nancy Norville<br>Director, Annual Giving<br /></td>
								<td valign="bottom" style="vertical-align: bottom;"><img width="39" height="406" style="display:block" src="http://secure.pledgemine.com/SPC/img/Note_Side.jpg"></td>
							</tr>
						</table>
					</td>
				<td width="210" align="right" style="vertical-align: bottom;" valign="bottom"><img width="210" height="287" border="0" alt="" style="display:block" src="http://secure.pledgemine.com/SPC/682/usr/web/682-WP_Email_v1.jpg"></td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td valign="top" align="center">
			<table style="background-color:#e55302;border-top:2px solid #000000;border-bottom:1px solid #FFF;" width="550" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td width="550" height="45" align="left" valign="top" colspan="2" style="padding:25px 20px;"><div style="font-size:12px;color:#FFFFFF;font-family:Verdana, Helvetica, Arial, sans-serif;line-height:70%;"><div style
 ="padding-left:20px;font-weight:bold;">5 THINGS TO DO THIS FALL</div><br><ol style="line-height: 130%;list-style:decimal;">
  <li style="padding-bottom: 6px;"> Make a gift: <a href="http://www.wpunj.edu/giving" style="color:#DEDED6;">www.wpunj.edu/giving</a>. <em>Now you can set up monthly recurring gifts!</em></li>
  <li style="padding-bottom: 6px;"> Update your contact information to receive news and invitations <a href="https://secure.wpunj.edu/wpblue/alumni/newaddress.cfm" style="color:#DEDED6;">here</a>. </li>
  <li style="padding-bottom: 6px;"> Alumni entrepreneurs can join our free <a href="https://webapps.wpunj.edu/alumni/directory/register.cfm" style="color:#DEDED6;">online business directory</a>, and all alumni are eligible for select discounts at participating businesses.</li>
  <li style="padding-bottom: 6px;"> Attend <a href="http://www.wpunj.edu/homecoming/" style="color:#DEDED6;">Homecoming weekend</a>, September 28th-29th.</li>
  <li style="padding-bottom: 6px;"> Show your Pioneer pride at an upcoming <a href="http://wpupioneers.com/" style="color:#DEDED6;">athletic event</a>.</li>
</ol></div>
					</td>
				</tr>
		
			</table>
		</td>
	</tr>
	<tr>
		<td valign="top" align="center">
			<table width="550" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td align="center" bgcolor="#888">
						<table border="0" cellspacing="0" cellpadding="0" style="background-color:#888">
							<tr>
								<td width="550" align="center" style="padding:8px 0;font-size:12px;color:#FFFFFF;line-height:120%;font-family:verdana;text-decoration:none;vertical-align:middle;">Connect with William Paterson University&nbsp;&nbsp;
							
								<a href="http://www.facebook.com/wpunj" target="_blank">
								<img src="http://secure.pledgemine.com/SPC/img/facebook.png" height="18" width="18" border="0"></a>&nbsp;
								
								<a href="https://twitter.com/wpunj_alumni" target="_blank">
								<img src="http://secure.pledgemine.com/SPC/img/twitter.png" height="18" width="18" border="0"></a>&nbsp;
								
								<a href="http://www.youtube.com/channel/UCSfH6lgeDREB4
 xQoWDa1mpw" target="_blank">
								<img src="http://secure.pledgemine.com/SPC/img/youtube.png" height="18" width="45" border="0"></a>&nbsp;
								
								<a href="http://www.linkedin.com/groups?home=&gid=82728&trk=anet_ug_hm" target="_blank">
								<img src="http://secure.pledgemine.com/SPC/img/linkedin.png" height="18" width="64" border="0"></a>&nbsp;
								
								</td>
							</tr>
					</table>
				</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td valign="top" align="center">
		<table width="550" border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td style="padding:10px;"><div align="center" style="font-size:10px;color:#666666;line-height:100%;font-family:Verdana, Helvetica, Arial, sans-serif;text-decoration:none;">Contact the <a href="mailto:annualfund@wpunj.edu?subject=5 Things Email" >Office of Annual Giving</a> with any questions regarding this email.  <a href="mailto:annualfund@wpunj.edu?subject=Unsubscribe" >Unsubscribe</a></div>
               
  </td>
			</tr>
		</table>
		</td>
	</tr>
</table>



</body>
</html>

------=_NextPart_000_011F_01CEB2F5.8272DB20--

Open in new window

0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
There is a space for some reason between the 4 and x

<a href="http://www.youtube.com/channel/UCSfH6lgeDREB4
 xQoWDa1mpw" target="_blank">

Open in new window


What is generating the youtube code?  Or is it static?  

What happens if you remove all white space?
Convert this sample
<html> 
<head> 
<title>The Fund for WP</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0">


<table width="100%" cellpadding="0" cellspacing="0" bgcolor="#DEDED6" style="background-color:#DEDED6;">

Open in new window

to this
<html><head><title>The Fund for WP</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0"><table width="100%" cellpadding="0" cellspacing="0" bgcolor="#DEDED6" style="background-color:#DEDED6;">

Open in new window

and do the same for the rest of the code.
0
GaryCommented:
The code you supplied isn't the CDO code, that is just a function building the body of the email.
0
skimoxAuthor Commented:
I found the solution on my own. I am posting it here for the benefit of others. First I created the following function to insert a line break 'VbCrLf' nearest to the maximum allowable characters per line as determined by my SMTP Relay provider--SendGrid in my case.

Function InsertReturns(strTxt, nCount)
    Dim nInsert
    Dim nBreak
    
    nInsert = nCount
    Do While nInsert < Len(strTxt)
        nBreak = InStrRev(strTxt, ">", nInsert)
        If nInsert - nBreak > nCount Then
            strTxt = Left(strTxt, nInsert) &  VbCrLf  & Mid(strTxt, nInsert + 1)
        Else
            nInsert = nBreak
            strTxt = Left(strTxt, nInsert) &  VbCrLf  & Mid(strTxt, nInsert + 1)
        End If
        nInsert = nInsert + nCount + 2
    Loop
    InsertReturns = strTxt
End Function

Open in new window


Then, used the following to read my ASP page and assign it to a variable. I also removed all of the CR, LF and TAB characters from the html.

Set objHttp = CreateObject("Msxml2.ServerXMLHTTP")
    objHttp.Open "GET", "http://www.yourwebsite.com/page.asp?val=1&val=2, False

    objHttp.Send
    rawText = replace(replace(replace(cStr(objHttp.ResponseText),chr(13), ""),chr(10),""),chr(9),"")
    Set objHttp = Nothing

Open in new window


Next, I processed the function to create line breaks only after closing a tag: '>'. I found that Sendgrid will automatically insert a return at 990 characters, so I set my limit to 900 characters.

nText = InsertReturns(rawText, 900)

Open in new window


Finally, I sent my email using CDO.

myMail.HTMLBody = nText

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I wonder if it is because you are sending it as an attachment.   I send html emails much larger without issue other then what I described.
0
skimoxAuthor Commented:
I found multiple sources and my personal trial and error to arrive at this solution. Removing white space as suggested by one of the experts was heading toward a solution, but the real important piece was figuring out why Sendgrid seemed to force a line break seemingly randomly. I eventually tested a long string of about 6000 characters and found that Sendgrid created an line break every 990 characters. Once I knew this, it was pretty easy to force a line break before SendGrid and that the line break works best after closing an html tag. This way, I wouldn't end up with any broken html in the email.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Thank you. I don't remember having this issue but will go back and look at what I have done.  I  am sending out newsletters via sendgrid without  issue.  I did see this link from send grid that supports what you found.  http://support.sendgrid.com/hc/en-us/articles/200182068-HTML-Formatting-Issues and that points to http://tools.ietf.org/html/rfc5322#section-2.1.1
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft IIS Web Server

From novice to tech pro — start learning today.