簡體   English   中英

使用.NET SmtpClient發送郵件會更改發件人名稱

[英]Using .NET SmtpClient to Send mail alters the From name

我已經編寫了一種發送帶有附件和身份驗證的郵件的方法。 一切工作正常,除了“ 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- -為什么要這樣做?

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;
                }
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM