Solved

send email in outlook using vb.net

Posted on 2004-09-02
18
618 Views
Last Modified: 2008-06-04
hello anyone know how to send an email in vb.net through outlook using the outlook 9.0 reference.  i dont want to use any dlls, just the outlook refernece... it is to send html formated mail.  thanks
0
Comment
Question by:christampa
  • 8
  • 5
  • 2
  • +2
18 Comments
 
LVL 5

Expert Comment

by:thenrich
ID: 11964294
It would be easier to prolly use the .NET mail provider. The reason being is if you have the latest service patches you'll get warnigs form the OS when sending mail. It's already there no additional dll's

example of FTP sending a document

  Private mailServer As System.Web.Mail.SmtpMail
Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
        Try
            Dim strDocPath As String = GetDocPath()
            Dim strSendTo As String
            Dim strMessage As String

            If Not strDocPath Is Nothing Then
                Dim frmSendTo As New frmSendTo(PAR_ProjectManager)

                frmSendTo.GetSendToInfo()
                strSendTo = frmSendTo.arrInfo(0)
                strMessage = frmSendTo.arrInfo(1)
                frmSendTo.Close()

                If Not strSendTo = "" Then
                    Cursor.Current = Cursors.WaitCursor
                 
                    Dim newAtt As New System.Web.Mail.MailAttachment(strDocPath)
                    Dim newMail As New System.Web.Mail.MailMessage
                    mailServer.SmtpServer = "MailServer"
                    newMail.Subject = "Subject text"
                    'newMail.From = SystemInformation.UserName & "test@something.com"
                    'newMail.To = strSendTo
                    newMail.From = "test@something.com"
                    newMail.To = "test@something.com"

                    newMail.Body = strMessage
                    newMail.Attachments.Add(newAtt)
                    mailServer.Send(newMail)
                End If
            Else
                MsgBox("File " & strErrorPath & " could not be found." & Chr(13) & _
                "Make sure project has not been archived and verify file path exists .", _
                MsgBoxStyle.Exclamation)
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Cursor.Current = Cursors.Default
        StatusBar1.Panels(0).Text = "   Ready"
    End Sub
0
 
LVL 3

Author Comment

by:christampa
ID: 11964380
will that work for html based emails?  also the number of characters will go to 50000.  will that be ok?
0
 
LVL 5

Expert Comment

by:thenrich
ID: 11964419
I've never used it for html emails but I can't see why it wouldn't work.
0
 
LVL 35

Expert Comment

by:YZlat
ID: 11964429
set mail body fromat to HTML

newMail.BodyFormat = MailFormat.Html
0
 
LVL 5

Expert Comment

by:thenrich
ID: 11964448
try this
Mail.BodyFormat = MailFormat.Html
0
 
LVL 5

Expert Comment

by:thenrich
ID: 11964460
Sorry YZlat - I did refresh before you posted.
0
 
LVL 3

Author Comment

by:christampa
ID: 11964561
do i need any references.  i get a lot of errors with that code.
0
 
LVL 5

Expert Comment

by:thenrich
ID: 11964604
what errors and where.
It wasn't meant to be a 'copy and paste' snippet rather an example.
0
 
LVL 3

Author Comment

by:christampa
ID: 11964685
System.Web.Mail.SmtpMail not defined
GetDocPath not declared
frmSendTo not defined
System.Web.Mail.MailAttachment not defined
System.Web.Mail.MailMessage not defined
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 500 total points
ID: 11964697
I agree with using the .net provider. But many way here is the outlook version

        Dim olApp As Outlook.Application
        Dim olMail As Outlook.MailItem
        olApp = New Outlook.Application

        olMail = olApp.CreateItem(Outlook.OlItemType.olMailItem)
        olMail.Recipients.Add("bla.bla@bla.com")
        olMail.Subject = "Your subject"
        olMail.HTMLBody = "Your HTML text"
        olMail.Attachments.Add("c:\ttt.txt", Outlook.OlAttachmentType.olByValue)
        olMail.Send()
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11964707
But many way here is the outlook version

should ofcourse be

But anyway here is the outlook version

0
 
LVL 5

Expert Comment

by:thenrich
ID: 11964735
reference system.web

 mailServer.SmtpServer = "Mailserver"
                    newMail.From = "someone@somplace.com"
                    newMail.To = "someone@somplaceelse.com.com"
                    newMail.Subject = "Test Subject"
                    newMail.Body = "bla, bla bla"
                    newMail.BodyFormat = Web.Mail.MailFormat.Html
                    mailServer.Send(newMail)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11964781
You need to add a System.Web reference to your project.

Bob
0
 
LVL 5

Expert Comment

by:thenrich
ID: 11964808
I said that .... :-)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11964835
And, yes you did.  :)

Bob
0
 
LVL 3

Author Comment

by:christampa
ID: 11965335
ronald i am trying your way and get the error
'Send' is ambiguous across the inherited interfaces 'Outlook._MailItem' and 'Outlook.ItemEvents_Event'.
0
 
LVL 5

Expert Comment

by:thenrich
ID: 11965398
yea you'll get that cause there is a send method and a send a event handle. try late binding your mailitem. You'll still run into the securtiy issue using outlook...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_ol2003_ta/html/odc_olsendemailsprogrammatically.asp
0
 
LVL 3

Author Comment

by:christampa
ID: 11996887
thank you for all your help, but I am going to use  RonaldBiemans answer.  thanks

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 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

18 Experts available now in Live!

Get 1:1 Help Now