Reputation: 117
I have searched A LOT and can only find how to obtain inbox messages from a shared/alternate email account/mailbox in Exchange but I cannot find how to send FROM a shared mailbox.
The basic need is that it appears to the recipient of the email that the email came from '[email protected]' (which is a shared mailbox I have access to) and not my primary account I login to ('[email protected]'). Also, the sent email should be saved in the shared mailbox's sent folder.
The way exchange works is that you sign into your primary account and you can open other shared mailboxes from within that main account (provided you have rights to it). If I use the code below, it still sends from my [email protected].
service = New ExchangeService(ExchangeVersion.Exchange2013)
service.Credentials = New NetworkCredential(username, _pw)
service.TraceEnabled = True
service.TraceFlags = TraceFlags.All
service.AutodiscoverUrl("[email protected]", AddressOf ValidateRedirectionUrlCallback)
Dim message As New EmailMessage(service)
message.Subject = subject
message.Body = body
For Each Item In _to
message.ToRecipients.Add(Item)
Next
For Each Item In AttachmentPaths
message.Attachments.AddFileAttachment(Item)
Next
message.SendAndSaveCopy()
Im starting to think maybe the message object has to have certain properties set to look like it is coming from email1 but is actually sent from my main account, but is that the official/best way? If so, what properties do I set to make it appear that it is coming from another mailbox?
I would assume I would then save the message manually to the saved folder of the shared mailbox.
Thanks.
Upvotes: 1
Views: 3241
Reputation: 117
I was just coming here to post my resolution with the final code. Since the server doesn't allow impersonation, I use the following code (which I found here). What messed me up for so long is that the example in that link uses more than just the email of where you want to save the document. I didn't think to just provide the email itself, since the example the author gave had a lot extra in it.
Public Function Send(ByRef subject As String, ByRef body As String, ByRef _to As List(Of String), ByRef AttachmentPaths As List(Of String), ByRef from As String) As Boolean
Try
Dim message As New EmailMessage(service)
message.From = New EmailAddress(from)
message.ReplyTo.Add(from)
message.Subject = subject
message.Body = body
For Each Item In _to
message.ToRecipients.Add(Item)
Next
For Each Item In AttachmentPaths
' Add a file attachment by using a stream.
Dim theStream As New FileStream(Item, FileMode.OpenOrCreate)
Dim pathparts As String() = Item.Split("\")
' The streamed file attachment is named FourthAttachment.txt.
message.Attachments.AddFileAttachment(pathparts(pathparts.Count - 1), theStream)
Next
message.Save(New FolderId(WellKnownFolderName.Drafts, New Mailbox(from)))
message.SendAndSaveCopy(New FolderId(WellKnownFolderName.SentItems, New Mailbox(from)))
Return True
Catch ex As Exception
Return False
End Try
End Function
Upvotes: 0
Reputation: 14956
Try the following code if you wish to save the message to the sent folder for the shared account (in C#):
var folderId = new FolderId(WellKnownFolderName.SentItems, new Mailbox("[email protected]"));
message.SendAndSaveCopy(folderId);
I have not tried the above code but let me know how it works out for you.
Upvotes: 3
Reputation: 117
I have found a solution to my own issue by just setting the 'From' field to what I want the recipient to see the email coming from and adding the same address to the list of reply to list. I also had to get receive/send access on the shared folder, I originally was only given read access which cause an exception in the saveandsendcopy
method.
Code below.
Public Function Create(ByRef emailaddress As String, ByRef username As String, ByRef _domain As String, ByRef _pw As String) As Boolean
Try
service = New ExchangeService(ExchangeVersion.Exchange2013)
service.Credentials = New NetworkCredential(username, _pw)
service.TraceEnabled = True
service.TraceFlags = TraceFlags.All
service.AutodiscoverUrl(emailaddress, AddressOf ValidateRedirectionUrlCallback)
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Function Send(ByRef subject As String, ByRef body As String, ByRef _to As List(Of String), ByRef AttachmentPaths As List(Of String), ByRef from As String) As Boolean
Try
Dim message As New EmailMessage(service)
message.From = New EmailAddress(from)
message.ReplyTo.Add(from)
message.Subject = subject
message.Body = body
For Each Item In _to
message.ToRecipients.Add(Item)
Next
For Each Item In AttachmentPaths
' Add a file attachment by using a stream.
Dim theStream As New FileStream(Item, FileMode.OpenOrCreate)
Dim pathparts As String() = Item.Split("\")
' The streamed file attachment is named FourthAttachment.txt.
message.Attachments.AddFileAttachment(pathparts(pathparts.Count - 1), theStream)
Next
message.SendAndSaveCopy()
Return True
Catch ex As Exception
Return False
End Try
End Function
I still need to find out how to save the message to the sent folder but have not found that out yet.
Upvotes: 0