Solved

vb.net send email "a program is trying to automatically send e-mail one your behalf"

Posted on 2011-09-22
5
343 Views
Last Modified: 2012-08-13
I am using the below code to send an email to users when they have booked a holiday
Imports Microsoft.VisualBasic
Imports System.Net.Mail

Public Class sendemailtosuperusers
    Shared Function sendemail() As String
        ' Create an Outlook application.
        Dim oApp As Outlook._Application
        oApp = New Outlook.Application()

        ' Create a new MailItem.
        Dim oMsg As Outlook._MailItem
        oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem)
        oMsg.Subject = "testing tom testing"
        oMsg.Body = "Hello World" & vbCr & vbCr

        ' TODO: Replace with a valid e-mail address.
        oMsg.To = "thomas.tyler@uk.experian.com"


        ' Send
        oMsg.Send()

        ' Clean up
        oApp = Nothing
        oMsg = Nothing
       
    End Function

End Class

Open in new window



but when i send the emil on each users computer i get the error "a program is trying to automatically send e-mail one your behalf"

is there any way to stop the error coming up via code?
0
Comment
Question by:runnerjp2005
5 Comments
 
LVL 6

Expert Comment

by:c1nmo
ID: 36580256
use the System.Net.Mail namespace instead of outlook automation
0
 

Author Comment

by:runnerjp2005
ID: 36580264
Dim message As String = "This is a test message" 

        Try
            Dim SendFrom As MailAddress = New MailAddress("ME@YOUREMIAL.com")
            Dim SendTo As MailAddress = New MailAddress("-")

            Dim MyMessage As MailMessage = New MailMessage(SendFrom, SendTo)

            MyMessage.Subject = "Sending an email through code"
            MyMessage.IsBodyHtml = True
            MyMessage.Body = message

            Dim emailClient As New SmtpClient("127.0.0.1")
            emailClient.Send(MyMessage)


        Catch ex As Exception

        End Try

Open in new window



i tired this and get no emails through!
0
 
LVL 33

Expert Comment

by:paulmacd
ID: 36580275
Use the SMTPClient in VB.Net, rather than instantiate Outlook.

      Dim oMailer As New SmtpClient '@@@ Mail object
      Dim oMailSender As New MailAddress(whatever)
      Dim oMailMessage As New MailMessage
      oMailMessage.IsBodyHtml = blnIsHTML

      strRecipient = whatever

      Try

        oMailMessage.From = oMailSender
        oMailMessage.To.Add(strRecipient)

        oMailMessage.Subject = whatever
        oMailMessage.Body = whatever


        '@@@ Build the e-mail message
        oMailer.Host = whatever
        oMailer.Send(oMailMessage)

      Catch ex As Exception
        Some error handling

      End Try
0
 
LVL 6

Expert Comment

by:c1nmo
ID: 36580319
Have you tried creating a NetworkCredential object and passing it to the SmtpClient.Credentials property?  SendTo address doesn't look right?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 36582153
You can suppress outlook messages by using this free addin

http://www.mapilab.com/outlook/security/
0

Featured Post

Do email signature updates give you a headache?

Do you spend too much time managing email signatures? Hate visiting every user’s desk to make updates? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Well, let Exclaimer give your company the email signature it deserves!

Join & Write a Comment

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.
This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…

706 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

21 Experts available now in Live!

Get 1:1 Help Now