?
Solved

ASP.NET   Sending email as HTML and Text simultaneously

Posted on 2004-10-04
9
Medium Priority
?
614 Views
Last Modified: 2008-02-20
I need to send email  in ASP.NET in HTML and also in Text simultaneously so that the email client can detect which version should be displayed.  

I know how to send in either format - just not sure how to send in both simultaneously.

This is my code - -

Thanks!

Function SendMailMessage(ByVal EmailMsgID As Integer, ByVal strTemp As String, ByVal EmailSendTo As String, ByRef Message As String, ByRef StackTrace As String, ByRef EID As Integer)

Dim strEmailSubject As String
Dim strEmailBody As String
Dim strEmailSendTo As String
Dim strEmailSendFrom As String

Message = ""
StackTrace = ""
EID = 0
Dim objMM As New MailMessage
Try
      Dim dr As SqlDataReader = SqlHelper.ExecuteReader(cn, CommandType.StoredProcedure, "GetEmailMsgByID", New SqlParameter("@EmailMsgID", EmailMsgID))
      dr.Read()
      strEmailSubject = dr.Item("EmailMsgSubject")
      strEmailBody = dr.Item("EmailMsgBody")
      strEmailSendFrom = dr.Item("EmailSendFrom")
      strEmailSendTo = dr.Item("EmailSendTo")
      dr.Close()
Catch ex As Exception
      'Redirect user to error screen with customized message
      Message = ex.Message
      StackTrace = ex.StackTrace
      EID = 226
End Try

      'Use this as the default mail server
      SmtpMail.SmtpServer = ConfigurationSettings.AppSettings("EmailSMTPServer")

      objMM.To = strEmailSendTo
      objMM.From = strEmailSendFrom
      objMM.BodyFormat = MailFormat.Text
               'objMM.BodyFormat = MailFormat.Html

      objMM.Priority = MailPriority.Normal
      objMM.Subject = strEmailSubject
      objMM.Body = strEmailBody & vbCrLf & strTemp
      SmtpMail.Send(objMM)
End Function
0
Comment
Question by:pmacmann
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 20

Expert Comment

by:jitganguly
ID: 12216971
How can you have it in both the formats ?

BodyFormat tag doesn't support both, it can be either one
You have to execute yoru code twice

But whats the use ? would you want send me 2 same mails to each user ?
0
 

Author Comment

by:pmacmann
ID: 12217065
I have a requirement that an email be sent in both formats to the user and that the email client on the user's machine could detect which format to display to the user. I haven't done this before....


when you say to execute the code twice - wouldn't that generate two separate emails?
0
 
LVL 20

Accepted Solution

by:
jitganguly earned 600 total points
ID: 12217141
>>client on the user's machine could detect which format to display to the user

You cannot do it from your side . If you want to send me mails, would you know what kind of formattings are supported in my e mail client ? Nope. Its security breach.

>>when you say to execute the code twice - wouldn't that generate two separate emails?
It will and thats why I said whats the point ?

My suggestion : Use HTML formatted. These days most fo the email client like MS Outlook, Lotus Notes support HTML format

Send it in HTML and let people complain you. If yes ( i.e. unsupported HTML), keep a flag for each mail user in your db and whiel sending read it and change BodyFormat  tag acordingly
e.g.
if strEmailSendTo = "John@mail.com" Then
   BodyFormat  =MailFormat.Text
else
BodyFormat  =MailFormat.Html
end if



0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 15

Expert Comment

by:justinbillig
ID: 12217424
<!-- please no points for this post -->

jitganguly is right, you can't send both simeltaniously ... though i would disagree on sending it html all the time.

yes most mail clients now support html, but the first tech support call you get is from someone who has a client that doenst support html

just use text, because ALL ( old and new ) support text.

or like jitganguly suggest store in the db the body format for each user
0
 
LVL 15

Expert Comment

by:justinbillig
ID: 12217440
or you could send it as text, with an attachment of the html formatted email
0
 

Author Comment

by:pmacmann
ID: 12217875
OK - thanks for the comments... I may need to look into this further since my client thinks this can be done... more later...
0
 
LVL 15

Expert Comment

by:justinbillig
ID: 12220510
one more thing, i know it might be a requirement of the business or whatnot .. but look at this

http://dustman.net/andy/HTMLMail
0
 

Author Comment

by:pmacmann
ID: 12276762
I've been reading about the Content-type:multipart/alternative which is suppossed to allow one email to send both HTML and text... still haven't been able to get this to work though. I've tried some examples to do it just through a stored proc: Still no luck - any ideas?


CREATE PROCEDURE SendCDOSysMailTest


AS
Declare @iMsg int
Declare @hr int
Declare @source varchar(255)
Declare @description varchar(500)
Declare @output varchar(8000)

Declare @contenttype1 varchar(100)
Declare @boundary varchar(50)
Declare @boundaryname varchar(50)
Declare @contenttype2 varchar(100)
Declare @charset varchar(100)
Declare @content1 varchar(50)
Declare @content2 varchar(50)
Declare @contenttype3 varchar(100)
Declare @twodashes char(2)
Declare @From varchar(100)
Declare @Subject varchar(100)
Declare @msgbodyhtml varchar(8000)
Declare @msgbodytext varchar(8000)
Declare @msgbody varchar(8000)
Declare @emailto varchar(100)

Select @twodashes = '--'
Select @contenttype1 = 'Content-type: multipart/alternative;'
Select @boundary = 'boundary="----=_NextPart_000_004C_01BFABBF.4A7D6BA0"'
Select @boundaryname=            '----=_NextPart_000_004C_01BFABBF.4A7D6BA0'
Select @contenttype2 = 'Content-type: text/plain;'
Select @charset = 'charset="iso-8859-1"'
Select @content1 = 'Content-Transfer-Encoding: 8bit'
Select @content2 = 'Content-Transfer-Encoding: quoted printable'
Select @contenttype3 = 'Content-type: text/html;'
SELECT @From = 'me@companyi.com'
SELECT @Subject ='Test Email Subject!'
Select @msgbodytext = 'This is a text body message.'
Select @msgbodyhtml = '<html><head></head><body><font color=red>This is red HTML text</font></body></html>'

   EXEC @hr = sp_OACreate 'CDO.Message', @iMsg OUT
   EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2'
   EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', 'SMTPServerName'
   EXEC @hr = sp_OAMethod @iMsg, 'Configuration.Fields.Update', null


Select @msgbody = @contenttype1
Select @msgbody = @msgbody + @boundary + char(13) + char(10)
Select @msgbody = @msgbody + @twodashes + @boundaryname + char(13) + char(10)
Select @msgbody = @msgbody + @contenttype2 + char(13) + char(10)
Select @msgbody = @msgbody + @charset + char(13) + char(10)
Select @msgbody = @msgbody + @content1 + char(13) + char(10)
Select @msgbody = @msgbody + @msgbodytext + char(13) + char(10)
Select @msgbody = @msgbody + ';'
Select @msgbody = @msgbody + @twodashes + @boundaryname + char(13) + char(10)
Select @msgbody = @msgbody + @contenttype3 + char(13) + char(10)
Select @msgbody = @msgbody + @charset + char(13) + char(10)
Select @msgbody = @msgbody + @content2 + char(13) + char(10)
Select @msgbody = @msgbody + @msgbodyhtml + char(13) + char(10)
Select @msgbody = @msgbody + @boundaryname + @twodashes + char(13) + char(10)

      EXEC @hr = sp_OASetProperty @iMsg, 'To', 'me@company.com'
        EXEC @hr = sp_OASetProperty @iMsg, 'From', @From
        EXEC @hr = sp_OASetProperty @iMsg, 'Subject', @Subject
      EXEC @hr = sp_OASetProperty @iMsg, 'MultipartBody', @msgbody
--print(@msgbody)

        EXEC @hr = sp_OAMethod @iMsg, 'Send', NULL

GO
0
 

Author Comment

by:pmacmann
ID: 12277748
Well, I found out how to do this.... thanks for your input - I'll split the points -appreciate the feedback.

CREATE PROCEDURE SendCDOSysMailTest2
AS
Declare @To varchar(50)
Declare @From varchar(100)
Declare @Subject varchar(100)
SELECT @From = 'me@company.com'
SELECT @Subject ='Test Email Subject!'
SELECT @To = 'me@company.com'

Declare @msgbodyhtml varchar(8000)
Declare @msgbodytext varchar(8000)
Select @msgbodytext = 'This is a text body message.'
Select @msgbodyhtml = '<html><head></head><body><font color=red>This is red HTML text</font></body></html>'

Declare @iMsg int
Declare @iBp int
Declare @iBp1 int
Declare @iBp2 int
Declare @hr int

EXEC @hr = sp_OACreate 'CDO.Message', @iMsg OUT
EXEC @hr = sp_OACreate 'CDO.IBodyPart', @iBp
EXEC @hr = sp_OACreate 'CDO.IBodyPart', @iBp1
EXEC @hr = sp_OACreate 'CDO.IBodyPart', @iBp2

EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2'
EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', 'SMTPServerName'
EXEC @hr = sp_OAMethod @iMsg, 'Configuration.Fields.Update', null

EXEC @hr = sp_OASetProperty @iMsg, 'To', @To
EXEC @hr = sp_OASetProperty @iMsg, 'From', @From
EXEC @hr = sp_OASetProperty @iMsg, 'Subject', @Subject

EXEC @hr = sp_OASetProperty @iBp, @iMsg
EXEC @hr = sp_OASetProperty @iBp,'ContentMediaType','multipart/alternative'

EXEC @hr = sp_OAMethod @iBp,'AddBodyPart', @iBp1
EXEC @hr = sp_OASetProperty @iBp1,'ContentMediaType','text/plain'
EXEC @hr = sp_OASetProperty @iBp1,'ContentTransferEncoding','7bit'
EXEC @hr = sp_OASetProperty @iMsg,'TextBody',@msgbodytext

EXEC @hr = sp_OAMethod @iBp,'AddBodyPart',@iBp2
EXEC @hr = sp_OASetProperty  @iBp2,'ContentMediaType','text/html'
EXEC @hr = sp_OASetProperty  @iBp2,'ContentTransferEncoding','quoted-printable'
EXEC @hr = sp_OASetProperty  @iMsg,'HTMLBody',@msgbodyhtml


EXEC @hr = sp_OAMethod @iMsg, 'Send', NULL
GO
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question