Save attachment with incoming email sender's name or email address

穿精又带淫゛_ 提交于 2020-01-07 03:07:26

问题


I want to add the sender's name of every incoming email to the saved attachment item, by storing into a variable so I can use it later, to return the email to that name or email address.

The code below first creates a counter for every item on a folder and rename the file with the date and the original attachment as follows: "2016-01-29 1026 1 POCreation" - the number 1 before the "POCreation" is the counter.

Then I save the attachment by running a rule in Outlook to run below script - as you might be aware - and save the attachment name by using the objAtt.DisplayName

So basically I want to get the name of the sender or the email of the sender stored on a variable. All the forums that I visit, even here, explained that they go to the "MAPI" folder to read all the emails in there but I am thinking that perhaps I can get it straight just like using the .displayname.

I tried to use mailitem.sendername but this throws an error of object not found, I guess is not reading it from the incoming email. I am running this into a module of Outlook.

Public Sub pdf(itm As Outlook.MailItem)
Dim FolderPath As String, path As String, count As Integer
FolderPath = "C:\Users\esacahui\Documents\POS\received"

path = FolderPath & "\*.xlsm"

FileName = Dir(path)

Do While FileName <> ""
    count = count + 1
    FileName = Dir()
Loop
' that was the counter, now is the save attachment 

Dim objAtt As Outlook.Attachment
Dim saveFolder As String
    saveFolder = "C:\Users\esacahui\Documents\POS\received"
Dim dateFormat As String
    dateFormat = Format(itm.ReceivedTime, "yyyy-mm-dd Hmm")

For Each objAtt In itm.Attachments
    objAtt.SaveAsFile saveFolder & "\" & dateFormat & " " & count & " " & objAtt.DisplayName
Next

End Sub

回答1:


itm.senderEmailAddress will get you the email address of the sender.




回答2:


You can use the following properties of the MailItem class:

  • SenderEmailAddress - a string that represents the e-mail address of the sender of the Outlook item.
  • SenderName - a string indicating the display name of the sender for the Outlook item.

See How to: Get the SMTP Address of the Sender of a Mail Item for more information.



来源:https://stackoverflow.com/questions/35090714/save-attachment-with-incoming-email-senders-name-or-email-address

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!