Sending Email in Vb.net with body text

yugandhar

Member
Joined
Jun 3, 2007
Messages
6
Programming Experience
Beginner
hi all,
can any one solve my problem that will be appreciate,
my problem is I need to create a button,
when the user press the button out look should open with some body text,
in that body I need to display the date wich is first monday of every month,
I am able to open outlook, and i am able to display data in body,
but i am not able to replace tha date in the body,
please any one help me out from this problem,

thanks in advance,

yogi
 
Probably because you haven't set the bodytype first.

Dim oApplication As New Outlook.Application
Dim oItem
oItem = oApplication.CreateItem(0)

With oItem
.Subject =
.To =
.BodyFormat = OlBodyFormat.olFormatHTML
.HTMLBody = 'Put your body text here
.Send() 'or .Display() if you want to show the message to then manually send

EDIT - You need to reference Outlook or MS Office in your project and then on your forms you must have Imports Outlook before your public class starts. I assume you've done this as you can get Outlook to open...;)
 
how to format the Email body

Hi all,
I am attaching a file to email body,
using this code:
Dim olApp As Outlook.ApplicationClass = New Outlook.ApplicationClass
'Create th Namespace Object
''Dim olNS As NameSpaceClass = olApp.GetNamespace("Mapi")
'Logon to Outlook if it's not running
'The Profilename is "Outlook", password ""
''olNS.Logon("Outlook", "", False, False)
'Create a new Mailitem (in the Draftsfolder)
Dim objMail As MailItemClass = olApp.CreateItem(OlItemType.olMailItem)
' The mail to line is not required as the client wants to enter the email address

' manually.

'objMail.To = ""
objMail.Subject = "IT/Phone Walk through sign off / Phone Training"
objMail.BodyFormat = OlBodyFormat.olFormatHTML
objMail.Body = ltr()
'objMail.Save()
objMail.Display(objMail)
'Logoff from Session
''olNS.Logoff()
'Release Marshal-Com Objects
System.Runtime.InteropServices.Marshal.ReleaseComObject(objMail)
''System.Runtime.InteropServices.Marshal.ReleaseComObject(olNS)
System.Runtime.InteropServices.Marshal.ReleaseComObject(olApp)

I just want to format the body of the mail,
how to do that,
any one pls help...

Thanks.
yugandhar
 
I'd already told you.

.HTMLBody = ltr() not .Body = ltr()

You've set the BodyType to be HTML, so you need to use .HTMLBody.

EDIT - what is ltr() as it's not in your code above.

You can easily add things to an email body. In my app I have something like;

VB.NET:
dim strEmail as string = "Hello" & me.txtName.text & ", how are you today?"
Dim oApplication As New Outlook.Application
Dim oItem
oItem = oApplication.CreateItem(0)

With oItem
.Subject = 
.To = 
.BodyFormat = OlBodyFormat.olFormatHTML
.HTMLBody = strEmail
.Display()
End With

so adding a date field isn't a problem.

You need to create your body first before setting it. This is why I set BodyFormat to HTML, as I can then use HTML tags in string.
I.E.

VB.NET:
dim strEmail as string = "['b']Hello John,['/b']" & "[br][br] How are you today?"
(Because of the formatting of this forum - the ['b'] should be without the ' ' when you do your app)

Will actually show in the email body as

Hello John,

How are you today?

[br] is a linebreak, so 2 of this means there is the gap line.


To add a date field you simply add something like
VB.NET:
dim strEmail as string = Today.Date() & "[br][br] rest of what you want in the body here."

From searching on the net, to get the first monday of the CURRENT month (so it will use your system clock) you do the following:

VB.NET:
Dim FirstMondayoftheMonth As Date = DateAdd(DateInterval.Weekday, DateDiff(DateInterval.Weekday, Date.MinValue, DateAdd(DateInterval.Day, 6 - DatePart(DateInterval.Day, Today()), Today())), Date.MinValue)   


' so now put that in an email

dim strEmail as string = FirstMondayoftheMonth & "[br][br] rest of what you want in the body here."
 
Last edited:
Back
Top