麻烦与波兰字符MIME格式的电子邮件发送
本文关键字:电子邮件 格式 MIME 字符 麻烦 | 更新日期: 2023-09-27 18:07:05
我试图通过使用交付方法SmtpDeliveryMethod.Network发送邮件(消息正文包含波兰字符)。我使用MIME格式,套接字,IPHostEntry和IPEndPoint。我的页眉是这样的:
Header.Append("MIME-Version: 1.0'r'n");
Header.Append("Content-type: text/plain; charset=ISO-8859-2;'r'n");
Header.Append("Content-transfer-encoding: quoted-printable'r'n");
当我试图发送邮件时,我仍然得到问号而不是我键入的字符。有人能帮帮我吗?我把全班都包括在内。
using System;
using System.Net;
using System.Net.Mail;
using System.Net.Sockets;
using System.Text;
public class SmtpDirect
{
/// <summary>
/// Get / Set the name of the SMTP mail server
/// </summary>
public static string SmtpServer = "kappa.it.com.ru";
private enum SMTPResponse : int
{
CONNECT_SUCCESS = 220,
GENERIC_SUCCESS = 250,
DATA_SUCCESS = 354,
QUIT_SUCCESS = 221
}
public static bool Send(MailMessage message)
{
IPHostEntry IPhst = Dns.GetHostEntry(SmtpServer);
IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 25);
Socket s = new Socket(endPt.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
s.Connect(endPt);
if (!Check_Response(s, SMTPResponse.CONNECT_SUCCESS))
{
s.Close();
return false;
}
Senddata(s, string.Format("HELO {0}'r'n", Dns.GetHostName()));
if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
{
s.Close();
return false;
}
Senddata(s, string.Format("MAIL From: {0}'r'n", message.From));
if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
{
s.Close();
return false;
}
string _To = message.To.ToString();
string[] Tos = _To.Split(new char[] { ';' });
foreach (string To in Tos)
{
Senddata(s, string.Format("RCPT TO: {0}'r'n", To));
if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
{
s.Close();
return false;
}
}
StringBuilder Header = new StringBuilder();
Header.Append("From: " + message.From + "'r'n");
Tos = message.To.ToString().Split(new char[] { ';' });
Header.Append("To: ");
for (int i = 0; i < Tos.Length; i++)
{
Header.Append(i > 0 ? "," : "");
Header.Append(Tos[i]);
}
Header.Append("'r'n");
if (message.CC != null)
{
Tos = message.CC.ToString().Split(new char[] { ';' });
Header.Append("CC: ");
for (int i = 0; i < Tos.Length; i++)
{
Header.Append(i > 0 ? "," : "");
Header.Append(Tos[i]);
}
Header.Append("'r'n");
}
Header.Append("Date: ");
Header.Append(DateTime.Now.ToString("ddd, d M y H:m:s z"));
Header.Append("'r'n");
Header.Append("Subject: " + message.Subject + "'r'n");
Header.Append("X-Mailer: SMTPDirect v1'r'n");
string MsgBody = message.Body;
if (!MsgBody.EndsWith("'r'n"))
MsgBody += "'r'n";
Header.Append("MIME-Version: 1.0'r'n");
Header.Append("Content-type: text/plain; charset=ISO-8859-2;'r'n");
Header.Append("Content-transfer-encoding: quoted-printable'r'n");
Header.Append("'r'n");
Header.Append("This is a multi-part message in MIME format.'r'n");
StringBuilder sb = new StringBuilder();
sb.Append("--unique-boundary-1'r'n");
sb.Append("Content-type: text/plain; charset=ISO-8859-2'r'n");
sb.Append("Content-transfer-encoding: quoted-printable'r'n");
sb.Append("'r'n");
sb.Append(MsgBody + "'r'n");
sb.Append("'r'n");
MsgBody = sb.ToString();
Senddata(s, ("DATA'r'n"));
if (!Check_Response(s, SMTPResponse.DATA_SUCCESS))
{
s.Close();
return false;
}
Header.Append("'r'n");
Header.Append(MsgBody);
Header.Append(".'r'n");
Header.Append("'r'n");
Header.Append("'r'n");
Senddata(s, Header.ToString());
if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
{
s.Close();
return false;
}
Senddata(s, "QUIT'r'n");
Check_Response(s, SMTPResponse.QUIT_SUCCESS);
s.Close();
return true;
}
private static void Senddata(Socket s, string msg)
{
byte[] _msg = Encoding.ASCII.GetBytes(msg);
s.Send(_msg, 0, _msg.Length, SocketFlags.None);
}
private static bool Check_Response(Socket s, SMTPResponse response_expected)
{
string sResponse;
int response;
byte[] bytes = new byte[1024];
while (s.Available == 0)
{
System.Threading.Thread.Sleep(100);
}
s.Receive(bytes, 0, s.Available, SocketFlags.None);
sResponse = Encoding.ASCII.GetString(bytes);
response = Convert.ToInt32(sResponse.Substring(0, 3));
if (response != (int)response_expected)
return false;
return true;
}
}
,我用这个:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
namespace FakeSMTP
{
class TestClass
{
public static void Main()
{
MailMessage mail = new MailMessage("guesswho@hi.com.pl", "guesswho@xyz.pl");
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "kappa.it.com.ru"";
client.Port = 69;
mail.Subject = "Zgłoszenie z systemu OZ <<547-gf45d>>";
mail.Body = "TUTAJ PODAJE CAŁE ZGŁOSZENIE Z SYSTEMU - można odpowiadać z Outlooka.";
SmtpDirect.Send(mail);
}
}
}
没关系。
byte[] _msg = Encoding.UTF8.GetBytes(msg);
帮助