EMERGENCY: Need to change sender when using Outlook mailitem object in VB.NET code

Hi everybody,

I am currently using an Outlook mailitem object to send email from within VB.NET. The mailitem object has properties for (To:), (Subject:) and (Attachments), but I don't see any way to change the sender.  Is a mailitem object appropriate for this?

Please see attached code example!
Outlook-Code.png
Thanks for your help!
Eric BurtonSr.ProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try¨something like this
    Dim AccountName as String = "from@mail.com"
    Dim session As objOutlk.NameSpace = mail.Session
    Dim accounts As objOutlk.Accounts = session.Accounts
    For i As Integer = 1 To accounts.Count
        Dim account As objOutlk.Account = accounts(i)
        If account.SmtpAddress.ToLower() == AccountName.ToLower() Then
            mail.SendUsingAccount = account
            Exit For
        End If
    Next i

Open in new window

Regards
0
yo_beeDirector of Information TechnologyCommented:
you can use .SenderEmailAddress if you are using outlook 2013 library.
https://msdn.microsoft.com/en-us/library/office/ff868262.aspx
0
Rgonzo1971Commented:
@ yo_bee MailItem.SenderEmailAddress is read-only not tochange the sender
0
Jacques Bourgeois (James Burger)PresidentCommented:
I cannot say for current versions of Outlook, but way back in 2003, we found out, as you seem to have done, that there was no way to change the sender with Outlook.

We found a low cost SMTP server at www.componentsource.com that was even easier to use than Outlook from VBA code, and that had all the features we wanted. My customer used it for years without any problem.
0
yo_beeDirector of Information TechnologyCommented:
This is from a macro I have in Excel 2010  that will e-mail recipients worksheets.
In the code you will see that I am sending this out using the .SentOnBehalfOfName method.
This works for my Office 2010 so it should work in your VB.net.
Sub Email_Report(ByVal Fname As String)

    Dim OutApp As Object
    Dim oNameSpace As Object
    Dim OutMail As Object

'...other code & then...

    'check for open Outlook session & open if necessary
    'sourced from: Microsoft Office Help - Microsoft Office Discussion - Excel VBA Programming - Access Programming
    On Error Resume Next
    Set OutApp = GetObject(, "Outlook.Application")
    On Error GoTo 0

    '### may need modification
    If OutApp Is Nothing Then
        Set OutApp = CreateObject("Outlook.Application")
        Set oNameSpace = OutApp.GetNamespace("MAPI")
        oNameSpace.Logon , , True
        oNameSpace.GetDefaultFolder(olFolderInBox).Display
        'WasOutlookOpenedByCode = True
    End If

    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)

'Dim OutApp As Object
'Dim OutMail As Object
'Set OutApp = CreateObject("Outlook.Application")
'Set OutMail = OutApp.CreateItem(0)

'    On Error Resume Next
'   ' Change the mail address and subject in the macro before you run it.
   With OutMail
        .SentOnBehalfOfName = "Prolaw Reports"
        .To = "Smith, Joe"
        '.CC =
        .BCC = "Doe, John" '
        .Subject = "Weekly Reports for EC"
        .Body = "Attached is a PDF AR, WIP , by RP and CLIENT"
        .Attachments.Add (Fname)
        .Sensitivity = 2


        ' You can add other files by uncommenting the following line.
        '.Attachments.Add ("C:\test.txt")
        ' In place of the following statement, you can use ".Display" to
        ' display the mail.
        .Send
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

ActiveWorkbook.Close savechanges:=True
'Excel.Application.Quit
'Outlook.Application.Quit
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.