Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ASP.NET   Sending email as HTML and Text simultaneously

Posted on 2004-10-04
9
Medium Priority
?
621 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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/…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
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…

610 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