Solved

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

Posted on 2011-09-22
5
348 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 34

Expert Comment

by:Paul MacDonald
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Footer for each row on Gridview 2 32
Enabling exchange 2013 read receipt 7 20
VB.Net. Reading xml value 6 29
VB.net Open video relating to control 2 19
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Granting full access permission allows users to access mailboxes present in their database. By giving full access permission one can open and read the content of any mailbox but cannot send emails from that mailbox.
In this video we show how to create a Contact 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 Recipients >> Contact ta…
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…

792 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