Использование.NET SmtpClient для отправки почты изменяет имя От
Я написал метод для отправки почты с вложениями и аутентификации. Все работает отлично, кроме From
адрес электронной почты изменяется в полученном сообщении.
Например,
foo@example.com
Меняется на,
-unknown-@example.com
Класс, который я написал, выглядит следующим образом:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Mail;
using System.IO;
class Email {
public MailAddress From { get; set; }
public string Host { get; set; }
public int Port { get; set; }
public Credentials SMTPAuth { get; set; }
public class Credentials {
public string Username { get; set; }
public string Password { get; set; }
}
public Email() {
SMTPAuth = new Credentials();
}
public List<Attachment> Attachments(params string[] file) {
List<Attachment> r = new List<Attachment>();
foreach ( string f in file ) {
if ( File.Exists( f ) ) {
r.Add(new Attachment(f));
}
}
return r;
}
public async Task<bool> SendMail( string Subject, string Message, MailAddressCollection To, MailPriority Priority, List<Attachment> Attachments ) {
bool result = false;
using ( SmtpClient s=new SmtpClient() ) {
NetworkCredential auth=new NetworkCredential( SMTPAuth.Username, SMTPAuth.Password );
s.Host=Host;
s.Port = Port;
s.UseDefaultCredentials = false;
s.Credentials=auth;
using ( MailMessage m=new MailMessage() ) {
m.From = From;
m.Subject = Subject;
m.IsBodyHtml = true;
m.Priority = Priority;
m.Body = Message;
foreach ( MailAddress t in To ) {
m.To.Add( t );
}
foreach(Attachment a in Attachments) {
m.Attachments.Add(a);
}
try {
await s.SendMailAsync(m);
result = true;
} catch ( SmtpFailedRecipientException ef ) {
} catch ( SmtpException es ) {
} catch ( Exception e ) {
}
}
return result;
}
}
}
И способ, которым я в настоящее время использую это (тестирование), является следующим:
private async void button2_Click( object sender, EventArgs e ) {
Email m = new Email();
m.SMTPAuth.Username = "foo@example.com";
m.SMTPAuth.Password="mypassword";
m.Host = "mail.example.com";
m.Port = 366;
m.From = new System.Net.Mail.MailAddress("foo@example.com","Company Name - NoReply");
System.Net.Mail.MailAddressCollection c = new System.Net.Mail.MailAddressCollection();
c.Add("some.account@gmail.com");
List<Attachment> a = m.Attachments( @"D:\code.png" );
bool sent = await m.SendMail( "Testing", "<h1>Hello</h1>", c, System.Net.Mail.MailPriority.High, a );
this.Text = "Message Sent : " + sent.ToString();
}
Заголовки почты (из полученного сообщения)
From: "Company Name - NoReply" <-unknown-@example.com>
To: example@gmail.com, support@example.com
Reply-To: "Company Name - NoReply" <noreply@example.com>
X-Priority: 1
Priority: urgent
Importance: high
Date: 1 May 2015 21:15:29 -0400
Subject: Testing
Content-Type: multipart/mixed;
boundary=--boundary_0_7310d152-dff2-4139-9db1-2558b3e70e73
X-ACL-Warn: {
X-AntiAbuse: This header was added to track abuse, please include it with any abuse report
X-AntiAbuse: Primary Hostname - mail.example.com
X-AntiAbuse: Original Domain - gmail.com
X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse: Sender Address Domain - example.com
X-Get-Message-Sender-Via: mail.example.com: acl_c_relayhosts_text_entry: -unknown-@example.com|example.com
X-From-Rewrite: rewritten was: [noreply@example.com], actual sender does not match
Как я могу предотвратить изменение строки, предшествующей символу @ в моем адресе электронной почты, на -unknown-
? - и почему он это делает?
1 ответ
void Send(string email, string subject, string body)
{
SmtpClient client = new SmtpClient(_Host, _Port);
client.Timeout = _Timeout;
client.EnableSsl = _Ssl;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(_Username, _Password);
MailMessage message = new MailMessage("No Reply <noreply@mysite.com>", email, subject, body);
message.BodyEncoding = UTF8Encoding.UTF8;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
message.Sender = new MailAddress("noreply@mysite.com", "No Reply");
lock (this)
{
for (var count = 0; count < _MaxTryCount; ++count)
{
try
{
client.Send(message);
return;
}
catch (SmtpFailedRecipientsException)
{
return;
}
catch (SmtpException)
{
Thread.Sleep(_Timeout);
}
catch (Exception)
{
return;
}
}
}
}