Solved

ASP.NET   Sending email as HTML and Text simultaneously

Posted on 2004-10-04
9
594 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
  • 4
  • 3
  • 2
9 Comments
 
LVL 20

Expert Comment

by:jitganguly
Comment Utility
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
Comment Utility
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 200 total points
Comment Utility
>>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
 
LVL 15

Expert Comment

by:justinbillig
Comment Utility
<!-- 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 15

Expert Comment

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

Author Comment

by:pmacmann
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now