使用命令行发送邮件

发布于 2024-10-20 00:31:40 字数 159 浏览 6 评论 0原文

任何人都可以帮助我,我们需要通过命令行从 Window 服务器发送邮件。由于安全问题,我们无法使用 Blat,也不想安装 Exchange 资源工具包。我们有自己的邮件交换服务器,我们可以使用它。

可能是任何可以运行来访问我们的 SMTP 邮件服务器的批处理。

提前致谢。

Can anyone help me out, we need to send mail through command line from Window server. We cannot use Blat as per security issue and neither do we want to install Exchange resource kit. We have our own Mail exchange server we can make use of it.

May be if any batch which could be run to access our SMTP mail server.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情深缘浅 2024-10-27 00:31:40

在 Windows 中从命令行发送邮件:-
将其保存在文本文件中,并将该文件命名为 sendmail.ps1

$EmailFrom = "[email protected]"

$EmailTo = "[email protected]"

$Subject = "The subject of your email"

$Body = "What do you want your email to say"

$SMTPServer = "smtp.gmail.com"

$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)

$SMTPClient.EnableSsl = $true

$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("gmail_username", "password");

$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

-不要忘记输入有效的电子邮件地址和密码。

现在打开 CMD 并编写此命令

Powershell -ExecutionPolicy ByPass -文件 C:\sendmail.ps1

瞧!邮件已发送!!

To send mail from command line in windows:-
Save this in a text file and name that file something like sendmail.ps1

$EmailFrom = "[email protected]"

$EmailTo = "[email protected]"

$Subject = "The subject of your email"

$Body = "What do you want your email to say"

$SMTPServer = "smtp.gmail.com"

$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)

$SMTPClient.EnableSsl = $true

$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("gmail_username", "password");

$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

-Don't forget to put the valid email address and password.

Now open CMD and write this command

Powershell -ExecutionPolicy ByPass -File C:\sendmail.ps1

Voila! mail sent!!

混浊又暗下来 2024-10-27 00:31:40

假设您不想安装任何 SMTP 客户端,只想使用 Windows 和批处理文件,则可以使用 Telnet 连接到 smtp 服务器的端口 25 并手动发送命令。
以下是如何执行此操作的示例
无论如何,我个人更喜欢安装一些命令行 SMTP 客户端,例如 Blat 或 Bmail,而不是陷入直接与 SMTP 交互的麻烦。

Assuming you don't want to install any SMTP client whatsoever, and just want to use windows and perhaps batch files, you could use Telnet to connect to port 25 of your smtp server and send the commands manually.
Here is an example of how to do this.
Anyway, I personally would prefer to install some command line SMTP client such as Blat or Bmail, instead of going into the hassle of directly interacting with SMTP.

胡渣熟男 2024-10-27 00:31:40

我在没有使用任何 SMTP 客户端的情况下解决了这个问题。我使用 Windows Powershell 脚本发送邮件,效果很好。

请检查以下链接

http://blogs.msdn.com/b/powershell/archive/2009/10/30/sending-automated-emails-with-send- mailmessage-convertto-html-and-the-powershellpack-s-taskscheduler-module.aspx

编码愉快!

I got this resolved without using any SMTP clients. I used Windows Powershell script to send mail and it is working very well.

Please check the below link

http://blogs.msdn.com/b/powershell/archive/2009/10/30/sending-automated-emails-with-send-mailmessage-convertto-html-and-the-powershellpack-s-taskscheduler-module.aspx

Happy coding !!

无名指的心愿 2024-10-27 00:31:40

我发现如果不涉及身份验证并且没有程序可以/不应该安装或可以假设存在,那么通常很好的一个非常好的解决方案如下,应该在Ubuntu上运行和其他 Linux 平台类似(您可以使用 ; 命令分隔符将所有内容放在一行中,并删除 echo 带引号的字符串中的 \ 字符) :

set sender="<[email protected]>"
set recipient="<[email protected]>"
set subj="testsubj"
set body="testbody"
set srv="mysmtpsrv.com"
set port="25"
set crlf="\x0D\x0A"

echo "EHLO man${crlf}\
MAIL FROM: ${sender}${crlf}\
RCPT TO: ${recipient}${crlf}\
data${crlf}\
Subject: ${subj}${crlf}${crlf}\
${body}\
${crlf}.${crlf}"\
|\
nc -Ci 1 ${srv} ${port}

(像这样使用确保您以 Unix 风格保存文件(在 echo "...\ 反斜杠后面只会添加一个 \x0A 字符)。
否则,只需删除反斜杠和换行符即可将所有内容放在一行上并使其工作,但视觉结构较差。)

a very nice solution I found generically good if no authentication is involved and no programs can/should be installed or can be assumed to exist is the following, which should work on Ubuntu and other Linux platforms alike (you can put everything in one line using the ; command delimiter and removing the \ chars in the echo quoted string):

set sender="<[email protected]>"
set recipient="<[email protected]>"
set subj="testsubj"
set body="testbody"
set srv="mysmtpsrv.com"
set port="25"
set crlf="\x0D\x0A"

echo "EHLO man${crlf}\
MAIL FROM: ${sender}${crlf}\
RCPT TO: ${recipient}${crlf}\
data${crlf}\
Subject: ${subj}${crlf}${crlf}\
${body}\
${crlf}.${crlf}"\
|\
nc -Ci 1 ${srv} ${port}

(Using like this make sure you are saving the file in Unix style (only a \x0A character will be added after the echo "...\ backslashes).
Otherwise just remove the backslashes and the newline which puts everything on a line and makes it work, but less visually structured.)

滿滿的愛 2024-10-27 00:31:40

HowToGeek 演示了一个运行良好的 Windows PowerShell 脚本,网址为 如何在 Windows 中无需额外软件即可从命令行发送电子邮件

方法如下:
首先,定义变量:

$EmailFrom =“[电子邮件受保护]
$EmailTo =“theRecipient'[电子邮件受保护]
$Subject =“你的主题”
$Body =“一些文本”
$SMTPServer =“smtp.gmail.com”
$SMTPClient = 新对象 Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“yourGmailUsername”, “password”);

然后,您使用此命令发送邮件:

$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

您需要一个有效的 Gmail 帐户才能作为 Gmail 用户进行身份验证。

HowToGeek demonstrates a Windows PowerShell script that works very well at How To Send Email From the Command Line in Windows Without Extra Software

Here is the method:
First you're defining the variables:

$EmailFrom = “[email protected]
$EmailTo = “theRecipient'[email protected]
$Subject = “your subject”
$Body = “some text”
$SMTPServer = “smtp.gmail.com”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“yourGmailUsername”, “password”);

Then, you're using this command to send the mail:

$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

You'll need a valid Gmail account in order to authenticate as a Gmail user.

痴情 2024-10-27 00:31:40

这是从 Gmail 地址发送邮件的示例,对于其他服务器,您需要更改 -smtp 和 -port

$securepass = ConvertTo-SecureString -String $yourpassword -A -F

$credential=new-object PSCredential $yourusername,$ securepass

send-mailmessage -from "[电子邮件受保护]" - 至 $recipient -subject "subject" -body "message" -smtpserver smtp.gmail.com -port 587 -usessl -credential $credential -encoding utf8

以下是可以从 Gmail 发送邮件的脚本:
https://gallery.technet.microsoft.com/scriptcenter/PC-Utilities -Downloader-355e5bfe

This is an example to send mail from your Gmail address, for other servers you need to change the -smtp and -port

$securepass = ConvertTo-SecureString -String $yourpassword -A -F

$credential=new-object PSCredential $yourusername,$securepass

send-mailmessage -from "[email protected]" -to $recipient -subject "subject" -body "message" -smtpserver smtp.gmail.com -port 587 -usessl -credential $credential -encoding utf8

Here is the script that can send mail from your Gmail:
https://gallery.technet.microsoft.com/scriptcenter/PC-Utilities-Downloader-355e5bfe

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文