c# - 电子邮件未送达
问题描述
我正在使用 System.Net.Mail 库通过远程桌面连接发送电子邮件。如果地址是我公司的地址,则可以正确发送电子邮件,但对于所有其他地址,则不会发送电子邮件。我没有收到任何异常或错误,他们只是永远无法到达地址,或者去垃圾邮件/垃圾邮件。当我同步我的“已发送”文件夹时,会出现电子邮件。
我用来发送电子邮件的代码如下
var smtpClient = new System.Net.Mail.SmtpClient(hostName)
{
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network
};
smtpClient.Credentials = new NetworkCredential(userName, password);
smtpClient.SendAndSaveMessageToIMAP(mailMessage, hostName, port, userName, password, "INBOX.Sent");
和以下
static System.IO.StreamWriter sw = null;
static System.Net.Sockets.TcpClient tcpc = null;
static System.Net.Security.SslStream ssl = null;
static string path;
static int bytes = -1;
static byte[] buffer;
static System.Text.StringBuilder sb = new System.Text.StringBuilder();
static byte[] dummy;
private static readonly bool IsRunningInDotNetFourPointFive = SendMethod.GetParameters().Length == 3;
private static readonly System.Reflection.BindingFlags Flags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic;
private static readonly System.Type MailWriter = typeof(System.Net.Mail.SmtpClient).Assembly.GetType("System.Net.Mail.MailWriter");
private static readonly System.Reflection.ConstructorInfo MailWriterConstructor = MailWriter.GetConstructor(Flags, null, new[] { typeof(System.IO.Stream) }, null);
private static readonly System.Reflection.MethodInfo CloseMethod = MailWriter.GetMethod("Close", Flags);
private static readonly System.Reflection.MethodInfo SendMethod = typeof(System.Net.Mail.MailMessage).GetMethod("Send", Flags);
public static void SendAndSaveMessageToIMAP(this System.Net.Mail.SmtpClient self, System.Net.Mail.MailMessage mailMessage, string imapServer, int imapPort, string userName, string password, string sentFolderName)
{
try
{
path = System.Environment.CurrentDirectory + "\\emailresponse.txt";
if (System.IO.File.Exists(path))
System.IO.File.Delete(path);
sw = new System.IO.StreamWriter(System.IO.File.Create(path));
tcpc = new System.Net.Sockets.TcpClient(imapServer, imapPort);
ssl = new System.Net.Security.SslStream(tcpc.GetStream());
try
{
ssl.AuthenticateAsClient(imapServer);
}
catch (Exception ex)
{
throw;
}
SendCommandAndReceiveResponse("");
SendCommandAndReceiveResponse(string.Format("$ LOGIN {1} {2} {0}", System.Environment.NewLine, userName, password));
using (var m = mailMessage.RawMessage())
{
m.Position = 0;
var sr = new System.IO.StreamReader(m);
var myStr = sr.ReadToEnd();
SendCommandAndReceiveResponse(string.Format("$ APPEND {1} (\\Seen) {{{2}}}{0}", System.Environment.NewLine, sentFolderName, myStr.Length));
SendCommandAndReceiveResponse(string.Format("{1}{0}", System.Environment.NewLine, myStr));
}
SendCommandAndReceiveResponse(string.Format("$ LOGOUT{0}", System.Environment.NewLine));
}
catch (System.Exception ex)
{
System.Diagnostics.Debug.WriteLine("error: " + ex.Message);
}
finally
{
if (sw != null)
{
sw.Close();
sw.Dispose();
}
if (ssl != null)
{
ssl.Close();
ssl.Dispose();
}
if (tcpc != null)
{
tcpc.Close();
}
}
self.Send(mailMessage);
}
private static void SendCommandAndReceiveResponse(string command)
{
try
{
if (command != "")
{
if (tcpc.Connected)
{
dummy = System.Text.Encoding.ASCII.GetBytes(command);
ssl.Write(dummy, 0, dummy.Length);
}
else
{
throw new System.ApplicationException("TCP CONNECTION DISCONNECTED");
}
}
ssl.Flush();
buffer = new byte[2048];
bytes = ssl.Read(buffer, 0, 2048);
sb.Append(System.Text.Encoding.ASCII.GetString(buffer));
sw.WriteLine(sb.ToString());
sb = new System.Text.StringBuilder();
}
catch (System.Exception ex)
{
throw new System.ApplicationException(ex.Message);
}
}
public static System.IO.MemoryStream RawMessage(this System.Net.Mail.MailMessage self)
{
var result = new System.IO.MemoryStream();
var mailWriter = MailWriterConstructor.Invoke(new object[] { result });
SendMethod.Invoke(self, Flags, null, IsRunningInDotNetFourPointFive ? new[] { mailWriter, true, true } : new[] { mailWriter, true }, null);
result = new System.IO.MemoryStream(result.ToArray());
CloseMethod.Invoke(mailWriter, Flags, null, new object[] { }, null);
return result;
}
任何帮助将不胜感激。
解决方案
推荐阅读
- eslint - ESLint:已定义的全局扩展仍未定义(no-undef)
- c# - 使用 C# 在 Sharepoint 中管理用户和权限的基本问题
- swift - 是否可以在不使用 SceneKit 中的“allowsCameraControl”的情况下获得节点的平移惯性旋转?
- java - 为什么/如果实例未实现 Cloneable,Object.clone() 在哪里抛出 CloneNotSupportedException
- google-chrome - “SameSite=none”不适用于 iframe
- python - 如何在分类模型中对单个图像进行预测?
- router - 如何在 shell 中找出网关 IP?
- database - 如何保护电子应用程序中的数据库密码?
- android - 我怎样才能使小吃栏位于导航栏上方?(y轴)
- javascript - 调整屏幕大小后,HTML 中的嵌套元素不会改变其位置