Friday, June 16, 2006

Using .NET Outlook to send formatted email

Imports Microsoft.Office
Imports Microsoft.Office.Core
Imports System.Runtime.InteropServices
Imports Outlook = Microsoft.Office.Interop.Outlook


Public Class TestOutlookMail
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
Private Sub InitializeComponent()

End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Private app As Outlook.ApplicationClass

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
app = New Outlook.ApplicationClass
End Sub

'Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' CreateEmailMessage()
'End Sub 'button1_Click


'Private Sub CreateEmailMessage()
' 'Initialize the envelope values.
' Dim strTo As String = "yourname@yourdomain.com"
' Dim strBCC As String = "yourname@yourdomain.com"
' Dim strCC As String = "yourname@yourdomain.com"
' Dim strSubject As String = "Outlook Automation"
' Dim strBody As String = "Hello World"

' 'Automate the Outlook mail item.
' app = New Outlook.ApplicationClass
' Dim mItem As Outlook.MailItemClass = CType(app.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItemClass)
' 'Dim As Outlook.MailItemClass = CType(doc.MailEnvelope.Item, Outlook.MailItemClass)
' 'Outlook.MailItemClass mi = (Outlook.MailItemClass)app.CreateItem
' mItem.To = strTo
' mItem.BCC = strBCC
' mItem.CC = strCC
' mItem.Subject = strSubject
' mItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML
' mItem.HTMLBody = strBody
' 'mItem.ItemEvents_Event_Close += New Outlook.ItemEvents_CloseEventHandler(Me.wApp_Close)

' 'wApp.Visible = True

' ' Loop until there are no more references to release.
' While Marshal.ReleaseComObject(mItem) > 0
' End While
' mItem = Nothing

' ' Invoke the .NET garbage collector.
' GC.Collect()
' GC.WaitForPendingFinalizers()
'End Sub 'CreateEmailMessage

Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Subject As String = "This is my Subject"
Dim Body As String = "This is my Body"


Dim mi As Outlook.MailItemClass = CType(app.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItemClass)
mi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML
mi.HTMLBody = Body
mi.Subject = Subject
mi.Display(New Object)

Dim ni As Outlook.NoteItemClass = CType(app.CreateItem(Outlook.OlItemType.olNoteItem), Outlook.NoteItemClass)
ni.Body = "This is my Body again"
ni.Display(New Object)

Dim pi As Outlook.PostItemClass = CType(app.CreateItem(Outlook.OlItemType.olPostItem), Outlook.PostItemClass)
pi.Subject = Subject
pi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML
pi.Body = Body

pi.Display(New [Object])
End Sub 'button1_Click


End Class

9 comments:

Christian Gnoth said...

Hello,
do you know how to change the Font Style?

Alex said...

In the world there is one tool which I use for work out with outlook files-recover outlook 2007 pst file,because it is actually reliable,second feature it has free status as is known,besides that tool can help to extract data from corrupted files in *.ost and *.pst format and avoid losing of critical contacts, calendars and messages,tool is compatible with all supported versions of Microsoft Windows, besides Microsoft Outlook 2007 you can use any other version of this email client,extract data from corrupted files in *.ost and *.pst format and avoid losing of critical contacts, calendars and messages,can preview the results and make sure, that all emails were repaired successfully.

Alexis said...
This comment has been removed by the author.
Alexis said...

Yesterday at work with Outlook I had an error and after most of my data were damaged...I didn't know what to do,but I used google and found-how to restore an ost file.It was perfectly,program recover all my data in 30 seconds.Moreover tool is free as is known and it could extract a list of files in *.eml, *.txt and *.vcf formats.

Anonymous said...

Hi there!
I would like to burn a theme at here. There is such a nicey, called HYIP, or High Yield Investment Program. It reminds of financial piramyde, but in rare cases one may happen to meet a company that really pays up to 2% daily not on invested money, but from real profits.

For several years , I earn money with the help of these programs.
I'm with no money problems now, but there are heights that must be conquered . I make 2G daily, and my first investment was 500 dollars only.
Right now, I managed to catch a guaranteed variant to make a sharp rise . Turn to my blog to get additional info.

http://theblogmoney.com

Anonymous said...

Hello everyone!
I would like to burn a theme at this forum. There is such a nicey, called HYIP, or High Yield Investment Program. It reminds of ponzy-like structure, but in rare cases one may happen to meet a company that really pays up to 2% daily not on invested money, but from real profits.

For quite a long time, I make money with the help of these programs.
I'm with no money problems now, but there are heights that must be conquered . I get now up to 2G a day , and my first investment was 500 dollars only.
Right now, I'm very close at catching at last a guaranteed variant to make a sharp rise . Visit my web site to get additional info.

[url=http://theinvestblog.com] Online investment blog[/url]

Anonymous said...

Hello!
You may probably be very curious to know how one can make real money on investments.
There is no initial capital needed.
You may commense earning with a money that usually goes
for daily food, that's 20-100 dollars.
I have been participating in one company's work for several years,
and I'll be glad to share my secrets at my blog.

Please visit my pages and send me private message to get the info.

P.S. I make 1000-2000 per day now.

http://theinvestblog.com [url=http://theinvestblog.com]Online Investment Blog[/url]

Alex said...

As for me my experience in computer sphere is quite little. But I had one obnoxious problem, which I could resolve only with the help of my friend. He suggested me suitable software, which finished my trobles and perhaps it to able to work out this question not less well - .ost converter.

Anonymous said...

This website was... how do you say it? Relevant!
! Finally I've found something that helped me. Cheers!
Also see my webpage - exchange server OST file Recover