c# - C#如何使用tcp连接发送电子邮件而无需身份验证
问题描述
我正在尝试使用 C# 中的 TCP 连接将电子邮件发送到 smtp 服务器(本例中为 google)。我得到的响应是需要身份验证。
5.5.1 需要验证。了解详情\n5.5.1 https://support.google.com/mail/?p=WantAuthError h66sm663716vke.21 - gsmtp
没有身份验证。我不知道要向其发送电子邮件的帐户的用户名和密码。
我能够找到的所有其他示例都使用预构建的 SMTP 服务器并构建邮件消息。
当有人需要重置密码或我需要发送系统消息或其他任何内容时,我只想能够向任何电子邮件帐户发送电子邮件。
这是我的代码:
TcpClient tcpclient = new TcpClient();
tcpclient.Connect("smtp.gmail.com", 465);
//not sure if I need port 465 or 587.
// implicit SSL is always used on SMTP port 465
System.Net.Security.SslStream sslstream = new System.Net.Security.SslStream(tcpclient.GetStream());
sslstream.AuthenticateAsClient("smtp.gmail.com");
writer = new StreamWriter(sslstream);
reader = new StreamReader(sslstream);
string replyText = string.Empty;
string[] capabilities = null;
// read the server's initial greeting
readResponse(220);
// identify myself and get the server's capabilities
if (sendCommand("EHLO myserver.com", ref replyText) == 250)
{
// parse capabilities
capabilities = replyText.Split(new Char[] { '\n' });
}
else
{
// EHLO not supported, have to use HELO instead, but then
// the server's capabilities are unknown...
capabilities = new string[] { };
sendCommand("HELO myserver.com", 250);
}
// check for pipelining support... (OPTIONAL!!!)
if (Array.IndexOf(capabilities, "PIPELINING") != -1)
{
// can pipeline...
// send all commands first without reading responses in between
writer.WriteLine("MAIL FROM:<" + "myserver@myserver.com" + ">");
writer.WriteLine("RCPT TO:<" + "anyemail@gmail.com" + ">");
writer.WriteLine("DATA");
writer.Flush();
// now read the responses...
Exception e = null;
// MAIL FROM
int replyCode = readResponse(ref replyText);
if (replyCode != 250)
e = new SmtpCmdFailedException(replyCode, replyText);
// RCPT TO
replyCode = readResponse(ref replyText);
if ((replyCode != 250) && (replyCode != 251) && (e == null))
e = new SmtpCmdFailedException(replyCode, replyText);
// DATA
replyCode = readResponse(ref replyText);
if (replyCode == 354)
{
// DATA accepted, must send email followed by "."
writer.WriteLine("Subject: Email test");
writer.WriteLine("Test 1 2 3");
writer.WriteLine(".");
writer.Flush();
// read the response
replyCode = readResponse(ref replyText);
if ((replyCode != 250) && (e == null))
e = new SmtpCmdFailedException(replyCode, replyText);
}
else
{
// DATA rejected, do not send email
if (e == null)
e = new SmtpCmdFailedException(replyCode, replyText);
}
if (e != null)
{
// if any command failed, reset the session
sendCommand("RSET");
throw e;
}
}
else
{
// not pipelining, MUST read each response before sending the next command...
sendCommand("MAIL FROM:<" + "myserver@myserver.com" + ">", 250);
try
{
sendCommand("RCPT TO:<" + "anyemail@gmail.com" + ">", 250, 251);
sendCommand("DATA", 354);
writer.WriteLine("Subject: Email test");
writer.WriteLine("");
writer.WriteLine("Test 1 2 3");
writer.Flush();
sendCommand(".", 250);
}
catch (SmtpCmdFailedException e)
{
// if any command failed, reset the session
sendCommand("RSET");
throw;
}
}
// all done
sendCommand("QUIT", 221);
解决方案
您将需要一个 DNS 库,它允许您查找收件人域的 MX 记录,然后连接到该 SMTP 服务器地址。
推荐阅读
- sql - 如何比较列中的两个逗号分隔值,并在 SQL 中只返回匹配的值
- vb.net - 如何通过定义将实例公开为属性的类来扩展派生类
- terraform - Local-exec 销毁触发器 - 忽略对谷歌访问令牌的更改
- python - tkinter 选项菜单不会为通过 .add_command(label='New Item') 创建的选项执行回调函数
- discord.js - 如何禁止用户ping某人?
- intellij-idea - 如何将bitbucket与intellij集成?
- python - Python:2n x 2n 矩阵的逆矩阵
- bash - 来自 bash 脚本的 Ubuntu 20.04.2 Bluetoothctl SCAN
- api - laravel API 的请求正文中存在的任何额外字段
- amazon-web-services - Cloudformation Elasticbeanstalk 指定共享负载均衡器的目标组