在 C# 中发送 html 电子邮件而不中断 TCP 连接的最佳方式
Best way to send html email in C# without disrupting the TCP connection
我正在开发一个使用 TCP 连接连接到服务器的应用程序。服务器是 VPS 上的 C# 控制台应用程序 运行 并且有数以千计的客户端(Android 应用程序)将连接到此服务器并且每个客户端都会发送数十个每秒发送到服务器的数据包数,服务器响应每个数据包。所以这是服务器上代码的一部分:
using System;
using System.Net.Mail;
using System.Threading;
namespace Server
{
class Program
{
private static Thread thread;
static void Main(string[] args)
{
thread = new Thread(new ThreadStart(ConsoleThread));
thread.Start();
// Initialize TCP and start listening for packages from clients
}
private static void ConsoleThread()
{
while (true)
{
}
}
public static void SendTestMail()
{
// What is the best way to do this
SendMail("target@example.com", "Whatever", "<p>This is a test message.</p>");
}
public static void SendMail(string to, string subject, string html)
{
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
MailMessage mail = null;
try
{
mail = new MailMessage("xxxx@gmail.com", to, subject, html);
mail.IsBodyHtml = true;
smtpClient.Port = 587;
smtpClient.Credentials = new System.Net.NetworkCredential("username", "password");
smtpClient.EnableSsl = true;
smtpClient.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (mail != null)
{
mail.Dispose();
}
}
}
}
}
有时服务器应该向客户端发送邮件。我想知道发送邮件是否有可能中断或延迟服务器响应客户端?如果是,那么您对如何调用 SendMail 方法有何建议?这是我能想到的:
public static void SendTestMail()
{
Thread mailThread = new Thread(() => SendMail("target@example.com", "Whatever", "<p>This is a test message.</p>"));
mailThread.Start();
}
另外我想知道你对整个事情的看法。在虚拟专用服务器和大量客户端之间使用 TCP 是个好主意吗(只有整数和字符串变量会在它们之间传输)?例如一个 VPS 具有以下规格可以支持多少个客户端?
CPU:4 个 vCore
内存:4GB
硬盘:50GB NVMe
SmtpClient 支持 SendAsync(),因此发送电子邮件不会阻塞您的线程。
对于一般问题 - 您能否通过单个连接测量应用程序的资源使用情况?例如如果您 运行 它在您的开发环境中并与客户端连接,那么使用了多少 CPU 和 RAM?
我正在开发一个使用 TCP 连接连接到服务器的应用程序。服务器是 VPS 上的 C# 控制台应用程序 运行 并且有数以千计的客户端(Android 应用程序)将连接到此服务器并且每个客户端都会发送数十个每秒发送到服务器的数据包数,服务器响应每个数据包。所以这是服务器上代码的一部分:
using System;
using System.Net.Mail;
using System.Threading;
namespace Server
{
class Program
{
private static Thread thread;
static void Main(string[] args)
{
thread = new Thread(new ThreadStart(ConsoleThread));
thread.Start();
// Initialize TCP and start listening for packages from clients
}
private static void ConsoleThread()
{
while (true)
{
}
}
public static void SendTestMail()
{
// What is the best way to do this
SendMail("target@example.com", "Whatever", "<p>This is a test message.</p>");
}
public static void SendMail(string to, string subject, string html)
{
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
MailMessage mail = null;
try
{
mail = new MailMessage("xxxx@gmail.com", to, subject, html);
mail.IsBodyHtml = true;
smtpClient.Port = 587;
smtpClient.Credentials = new System.Net.NetworkCredential("username", "password");
smtpClient.EnableSsl = true;
smtpClient.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (mail != null)
{
mail.Dispose();
}
}
}
}
}
有时服务器应该向客户端发送邮件。我想知道发送邮件是否有可能中断或延迟服务器响应客户端?如果是,那么您对如何调用 SendMail 方法有何建议?这是我能想到的:
public static void SendTestMail()
{
Thread mailThread = new Thread(() => SendMail("target@example.com", "Whatever", "<p>This is a test message.</p>"));
mailThread.Start();
}
另外我想知道你对整个事情的看法。在虚拟专用服务器和大量客户端之间使用 TCP 是个好主意吗(只有整数和字符串变量会在它们之间传输)?例如一个 VPS 具有以下规格可以支持多少个客户端? CPU:4 个 vCore 内存:4GB 硬盘:50GB NVMe
SmtpClient 支持 SendAsync(),因此发送电子邮件不会阻塞您的线程。 对于一般问题 - 您能否通过单个连接测量应用程序的资源使用情况?例如如果您 运行 它在您的开发环境中并与客户端连接,那么使用了多少 CPU 和 RAM?