Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook MailItem From Field 2

Status
Not open for further replies.

sifitz

Programmer
Feb 27, 2002
47
0
0
GB
Hi

I have created an email from a VB project that works fine apart from one thing. I want to be able to change the address that the email is sent from.

You can do this is Outlook manually but I want to do it in code.

How can this be done?

Thanks

SiFitz [afro]
 
use Recpients type = 0

Set oOL = CreateObject("Outlook.Application")
Set oMI = oOL.CreateItem(0)
Set oRec = oMI.Recipients.Add("walk4823")
oRec.Type = 0
 
I have tried this code and it it seems to be ok until an attempt is made to send the email. This is the error I got:-

-834207657 Could not complete the operation. One or more parameter values are not valid.

The above error occurred both when I used the Send method as well as when using the Display method and then clicking on the Send button.

Why would this happen? I can't find any good help on this.

Si [afro]
 
SiFitz, Try this one:

'========================
'Private Sub AddReplyRecip(objMsg As MailItem, strName As String)
' Dim colReplyRecips As Recipients
' Dim objReplyRecip As Recipient
' Dim strPrompt As String
' Dim intRes As Integer
' Dim strRecipName As String
'
' Set colReplyRecips = objMsg.ReplyRecipients
' Set objReplyRecip = colReplyRecips.Add(strName)
' objReplyRecip.Resolve
' If Not objReplyRecip.Resolved Then
' objReplyRecip.Delete
' strPrompt = strName & " could not be resolved as " & _
' "a valid Outlook address. Do you want to try " & _
' "a different name?"
intRes = MsgBox(strPrompt, _
' vbYesNo + vbQuestion + vbDefaultButton1, _
' "Try Again?")
' If intRes = vbYes Then
' strRecipName = GetReplyAddress()
' If strRecipName <> &quot;&quot; Then
' Call AddReplyRecip(objMsg, strRecipName)
' End If
' End If
' End If

' Set colReplyRecips = Nothing
' Set objReplyRecip = Nothing
'End Sub
'========================
Sandy
 
Thanks for the help guys. Turns out that there is an easy way of doing what I was after.

The actually code I needed was:-

objMail.SentOnBehalfOfName = <from address>

Now when the MailItem is viewed, the From address is filled in.

Security to check whether the user can send email as somebody else is checked within the mail object.

SiFitz [afro]
 
SiFitz,

What property exposes the delegate option so you could turn it on or off if you wanted to send an email with a generic from (eg info@blahblahblah.com)?

Thanks and have a great day!
 
From what I understand, you can't send an email in this manner. You need permission to send an email from the address supplied. For example, I have permission for the inbox/account of it@company.com although my personal email account is si@company.com. That's how my code manages to work in this case.

If you want to send email from another address you may want to use CDONTS (if possible). I prefer this anyway, but you need to have IIS installed on the PC and may need a smart host setting up.

I hope this helps [afro]
 
sifitz,

If I understand you correctly, modifying the &quot;from&quot; field can indeed be a security issue with your System Administrators.

So, the Option adjustment I provided above revises the &quot;Have replies sent to:&quot; field, to ensure the respondents, when they hit the &quot;Reply to:&quot; button have the appropriate address placed therein.

Sandy
 
Thanks Sandy, the code that you have written is helpful in understanding the email object a bit more. However, we needed the same address to be seen in the user's inbox and the SendOnBehalfOfName property works perfectly for this irrespective of who uses the application. The ReplyRecipients still uses the user's email address.

I know that the only people that are going to use the application will have access to this email account.

The security issues you mentioned may very well be a problem for other people but it is ok in our case.

Si [afro]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top