Error when sending email through outlook in vb?

0

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in myprogram.exe

Additional information: Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))

The following code is what caused the error:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim AppOutlook As New outlook.Application
        Dim OutlookMessage As outlook.MailItem = AppOutlook.CreateItem(outlook.OlItemType.olMailItem)
        AppOutlook = CreateObject("Outlook.Application")
        Dim Recipents As outlook.Recipients = OutlookMessage.Recipients
        Recipents.Add("oliverbroomhall1712@hotmail.co.uk")
        OutlookMessage.Subject = "Sending through Outlook"
        OutlookMessage.Body = "Testing outlook Mail"
        OutlookMessage.Send()
        OutlookMessage = Nothing
        AppOutlook = Nothing

    End Sub

The error was found in line 7 where it says:

Dim Recipents As outlook.Recipients = OutlookMessage.Recipients

If it's not too complicated, is there a way to do this without outlook? Because what happens when the user doesnt have outlook installed? I need a way to send an email from my application if anyone can help me :)

vb.net
email
error-handling
outlook
send
asked on Stack Overflow Feb 14, 2012 by user1196604 • edited Feb 14, 2012 by user1196604

1 Answer

0

I have never tried to do what you are doing, but shouldn't your lines:

   Dim OutlookMessage As outlook.MailItem = AppOutlook.CreateItem(outlook.OlItemType.olMailItem)
   AppOutlook = CreateObject("Outlook.Application")

be reversed? So that your OutlookMessage isn't based on that new instance? Of course, yeah you have that issue with what if they don't have outlook installed.

I would think that short of getting email setting information from your user it may be somewhat difficult to get email to send in a way that will always work. Considering Port 25 blocking, and SMTP filtering etc. Even if you used settings that the user has entered for mail as listed in the control panel, so many people use web based email now I can't imagine that would work. I suppose you could use an email account of your own that you know will work to send.

    Dim Smtp As New SmtpClient()
    Smtp.Credentials = New Net.NetworkCredential("user@gmail.com", "password")
    Smtp.EnableSsl = True
    Smtp.Port = 587
    Smtp.Host = "smtp.gmail.com"
    Dim Email As New MailMessage()
    Email.From = New MailAddress("user@gmail.com")
    Email.To.Add("oliverbroomhall1712@gmail.com")
    Email.Subject = "Test Mail"
    Email.Body = "SMTP server test"
    Smtp.Send(Email)

But that feels a little bit wrong to me. I think I might try one of these solutions and give the user the opportunity to give you some settings if it isn't the case for them.

answered on Stack Overflow Feb 14, 2012 by Jay

User contributions licensed under CC BY-SA 3.0