簡體   English   中英

C#:發送郵件失敗

[英]C#: Failure Sending Mail

我已經嘗試了多種方法,例如:

  • 使用不同的版本,端口,打開或關閉SSL,...
  • 其他郵件服務提供商
  • 在我的Gmail帳戶設置中設置“允許不太安全的應用”

題:

  • 如何解決此錯誤並成功發送附件?
  • 是什么導致此錯誤?
  • 是因為端口可能被阻塞了嗎?

錯誤:

Exception thrown: 'System.IO.IOException' in System.dll
Exception thrown: 'System.Net.Mail.SmtpException' in System.dll
System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
   at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)
   at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
   at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
   at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpClient.GetConnection()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   --- End of inner exception stack trace ---
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at TreeView.TreeView.<Email>d__28.MoveNext() in C:\Users\Alexander\Documents\Visual Studio 2015\Projects\TreeView\TreeView\TreeView\TreeView.cs:line 365

碼:

public void Email()
{
    try
    {
        SmtpClient client = new SmtpClient()
        {
            Host = @"smtp.gmail.com",
            Port = 465,
            Credentials = new NetworkCredential(@"mailid@gmail.com", @"Password"),
            UseDefaultCredentials = false,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            EnableSsl = true
        };

        MailMessage message = new MailMessage()
        {
            From = new MailAddress(@"mailid@gmail.com"),
            Subject = @"Test",
            Body = @"Test",
            Priority = MailPriority.Normal
        };

        message.To.Add(@"mailid@example.com");
        Attachment file = new Attachment(@"C:\Users\Alexander\Documents\File.txt");
        message.Attachments.Add(file);

        client.Send(message);
    }
    catch { }
}

對於gmail我有

SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";

client.Port = 587;

client.EnableSsl = true;
client.Credentials = new NetworkCredential("mygmailusername@gmail.com", "mygmailpassword");
                ////email.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network ;

PS您可能需要啟用此功能:

https://support.google.com/accounts/answer/6010255?hl=zh_CN

附加:

下載此工具:

https://www.microsoft.com/zh-cn/download/details.aspx?id=24009

並查看是否可以查詢該端口/地址

我得到以下結果:

=============================================

 *Starting portqry.exe -n smtp.gmail.com -e 587 -p TCP ...
Querying target system called:
 smtp.gmail.com
Attempting to resolve name to IP address...
Name resolved to 74.125.29.109
querying...
TCP port 587 (unknown service): LISTENING
portqry.exe -n smtp.gmail.com -e 587 -p TCP exits with return code 0x00000000.*

設置UseDefaultCredentials將始終將Credentials設置為null

因此,必須先設置UseDefaultCredentials = true/false然后再設置Credentials

參考: https : //stackoverflow.com/a/27896975/5075227

暫無
暫無

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

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