Solved

send email in outlook using vb.net

Posted on 2004-09-02
18
612 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

11 Experts available now in Live!

Get 1:1 Help Now