我搜索了A LOT,但只能找到如何从Exchange中的共享/备用电子邮件帐户/邮箱获取收件箱邮件,但我找不到如何从共享邮箱发送邮件。
基本需求是,电子邮件的收件人看来电子邮件来自' email2@domain.com' (这是我有权访问的共享邮箱)而不是我登录的主要帐户(' email1@domain.com')。此外,发送的电子邮件应保存在共享邮箱的已发送文件夹中。
交换方式的工作方式是您登录主帐户,并且可以从该主帐户中打开其他共享邮箱(前提是您拥有该帐户的权限)。如果我使用下面的代码,它仍然会从我的email1@domain.com发送。
service = New ExchangeService(ExchangeVersion.Exchange2013)
service.Credentials = New NetworkCredential(username, _pw)
service.TraceEnabled = True
service.TraceFlags = TraceFlags.All
service.AutodiscoverUrl("email2@domain.com", 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()
我开始认为消息对象可能必须将某些属性设置为看起来像来自email1但实际上是从我的主帐户发送的,但这是官方/最好的方式吗?如果是这样,我设置了哪些属性使其看起来来自另一个邮箱?
我认为我会手动将邮件保存到共享邮箱的已保存文件夹中。
感谢。
答案 0 :(得分:2)
如果您希望将邮件保存到共享帐户的已发送文件夹(在C#中),请尝试以下代码:
var folderId = new FolderId(WellKnownFolderName.SentItems, new Mailbox("email2@domain.com"));
message.SendAndSaveCopy(folderId);
我没有尝试过上面的代码,但让我知道它是如何为你工作的。
答案 1 :(得分:0)
我找到了解决我自己问题的方法,只需将“发件人”字段设置为我希望收件人看到来自的电子邮件,并将相同的地址添加到回复列表中。我还必须获得共享文件夹的接收/发送访问权限,我最初只获得了读访问权限,这导致了saveandsendcopy
方法中的异常。
以下代码。
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
我仍然需要了解如何将邮件保存到已发送的文件夹,但尚未找到。
答案 2 :(得分:0)
我刚来这里用最终代码发布我的决议。由于服务器不允许模拟,我使用以下代码(我发现here)。令我困惑的是,该链接中的示例使用的不仅仅是您要保存文档的电子邮件。我不认为只提供电子邮件本身,因为作者给出的例子中有很多额外的内容。
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