使用.batch脚本

时间:2017-03-30 18:50:11

标签: powershell

我想使用TELNET使用我的OVH邮件帐户(OVH上托管的noreply@clement.business)向gmail地址发送内容为log.txt的电子邮件。由于我的社会安全政治,我无法在服务器上安装任何东西,而是操作系统本身。

所以我尝试使用批处理和PowerShell但没有成功,我甚至尝试手动,我可以telnet OVH,但后来不知道如何登录。

这是我尝试过的一些事情:

$subject = $args[0]

# Create from/to addresses
$from = New-Object system.net.mail.MailAddress "noreply@clement.business"
$to = New-Object system.net.mail.MailAddress "random@gmail.com"

# Create Message
$message = new-object system.net.mail.MailMessage $from, $to
$message.Subject = $subject
$message.Body = @"
Am I even suppose to write here
"@

# Set SMTP Server and create SMTP Client
$server = "I have no idea how to OVH here"
$client = new-object system.net.mail.smtpclient $server... Server what, POP3 ?

# Do
"Sending an e-mail message to {0} by using SMTP host {1} port {2}." -f $to.ToString(), $client.Host, $client.Port
try {
$client.Send($message)
}
catch {
"Exception caught in CreateTestMessage: {0}" -f $Error.ToString()
}

我无法弄清楚,处理这是一场如此艰难的斗争......

PS:我确实使用了关于POP3 / SMTP设置的OVH guide,但没有用,或者我不知道我在做什么。 Useful link here as well

1 个答案:

答案 0 :(得分:0)

您知道其中的区别:POP3用于接收电子邮件,SMTP用于发送电子邮件。你不能互换使用它们。

快速google for'ovh smtp服务器'显示ovh help pages将SMTP服务器列为:

Server name: ns0.ovh.net
Port of the outgoing server: 587
Username: identical to the email address
Password: your password for your account, as defined in the creation of the email address

我无法测试这些因为我没有ovh帐户。

我会使用Send-MailMessage发送电子邮件,因为它比您的解决方案更容易使用:'

这将发送一封包含log.txt文件作为附件的电子邮件:

$username = "noreply@clement.business"
$password = "passwordforemailaddress" | ConvertTo-SecureString
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password

$parms = @{
    From = "noreply@clement.business"
    To = "email@gmail.com"
    Attachment = "C:\folder\log.txt"
    Subject = "Daily Logs"
    Body = "Your body text here"
    SMTPServer = "ns0.ovh.net"
    SMTPPort = "587"
    Credential = $credentials
}
Send-MailMessage @params

这些参数将使用log.txt作为电子邮件正文:

$parms = @{
    From = "noreply@clement.business"
    To = "email@gmail.com"
    Body = Get-Content "C:\folder\log.txt"
    Subject = "Daily Logs"
    SMTPServer = "ns0.ovh.net"
    SMTPPort = "587"
    Credential = $credentials
}