Solved

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

Posted on 2011-09-22
5
349 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

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…
Easy CSR creation in Exchange 2007,2010 and 2013
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
To show how to generate a certificate request 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 Servers >> Certificates…

730 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