如何对Office 365地址进行身份验证以匿名发送?

时间:2017-04-18 01:27:43

标签: powershell outlook active-directory office365

我正在尝试为Active Directory环境中的用户设置电子邮件提醒。我发现了一个非常有用的PowerShell脚本,我已经对它进行了修改,以满足我对office 365的需求。

$smtpServer="smtp.office365.com"
$EmailPassword=ConvertTo-SecureString "AnAmazingPassword123" -AsPlainText -Force
$EmailCreds=New-Object System.Management.Automation.PSCredential("anAccountICreated@mycompany.com",$EmailPassword)
$expireindays = 11
$from = "My Company IT <it@mycompany.com>"
$logging = "Enabled" # Set to Disabled to Disable Logging
$logFile = "c:\adirectory\log11.csv" # ie. c:\mylog.csv
$testing = "Disabled" # Set to Disabled to Email Users
$testRecipient = ""
$date = Get-Date -format ddMMyyyy

# Check Logging Settings
if (($logging) -eq "Enabled")
{
    # Test Log File Path
    $logfilePath = (Test-Path $logFile)
    if (($logFilePath) -ne "True")
    {
        # Create CSV File and Headers
        New-Item $logfile -ItemType File
        Add-Content $logfile "Date,Name,EmailAddress,DaystoExpire,ExpiresOn"
    }
} # End Logging Check

# Get Users From AD who are Enabled, Passwords Expire and are Not Currently Expired
Import-Module ActiveDirectory
$users = get-aduser -filter * -properties Name, PasswordNeverExpires, PasswordExpired, PasswordLastSet, EmailAddress |where {$_.Enabled -eq "True"} | where { $_.PasswordNeverExpires -eq $false } | where { $_.passwordexpired -eq $false }
$maxPasswordAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge

# Process Each User for Password Expiry
foreach ($user in $users)
{
    $Name = (Get-ADUser $user | foreach { $_.Name})
    $emailaddress = $user.UserPrincipalName
    $passwordSetDate = (get-aduser $user -properties * | foreach { $_.PasswordLastSet })
    $PasswordPol = (Get-AduserResultantPasswordPolicy $user)
    # Check for Fine Grained Password
    if (($PasswordPol) -ne $null)
    {
        $maxPasswordAge = ($PasswordPol).MaxPasswordAge
    }

    $expireson = $passwordsetdate + $maxPasswordAge
    $today = (get-date)
    $daystoexpire = (New-TimeSpan -Start $today -End $Expireson).Days

    # Set Greeting based on Number of Days to Expiry.

    # Check Number of Days to Expiry
    $messageDays = $daystoexpire

    if (($messageDays) -ge "1")
    {
        $messageDays = "in " + "$daystoexpire" + " days."
    }
    else
    {
        $messageDays = "today."
    }

    # Email Subject Set Here
    $subject="Your password will expire $messageDays"

    # Email Body Set Here, Note You can use HTML, including Images.
    $body ="
    Dear $name,
    <p> Your password is expiring.<br>
    "

    # If Testing Is Enabled - Email Administrator
    if (($testing) -eq "Enabled")
    {
        $emailaddress = $testRecipient
    } # End Testing

    # If a user has no email address listed
    if (($emailaddress) -eq $null)
    {
        $emailaddress = $testRecipient    
    }# End No Valid Email

    # Send Email Message
    if (($daystoexpire -ge "0") -and ($daystoexpire -lt $expireindays))
    {
         # If Logging is Enabled Log Details
        if (($logging) -eq "Enabled")
        {
            Add-Content $logfile "$date,$Name,$emailaddress,$daystoExpire,$expireson" 
        }
        # Send Email Message 
    Send-Mailmessage -smtpServer $smtpServer -Credential $EmailCreds -Port 587 -UseSsl -from $from -to $emailAddress -subject $subject -body $body -bodyasHTML -priority High
    } # End Send Message

} # End User Processing

# End

但是,我收到了这个错误:

The SMTP server requires a secure connection or the client was not authenticated.  The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [CY4PR17CA0043.namprd29.prod.outlook.com]

+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

我认为问题是该脚本试图在不使用密码的情况下从it@mycompany.com发送电子邮件。我更喜欢这样,但我不确定如何在办公室365中允许它。此外,我不是100%确定这是实际问题,或者如果在办公室365中这是可能的,因为我是新手office 365产品。

1 个答案:

答案 0 :(得分:0)

我通过Microsoft支持网站找到了答案。

为避免通过发送电子邮件地址进行身份验证,您需要将Send-Mailmessage更改为以下内容:

Send-MailMessage –From $from –To $emailAddress –Subject $subject –Body $body -SmtpServer $smtpServer

$smtpServer值更改为mail.protection.outlook.com地址

的位置

就我而言,这是mycompany-com.mail.protection.outlook.com

您可以在DNS MX记录或Office365 Admin Center -> Settings -> Domains -> yourDomain

中找到此值