2016-08-04 43 views
2

我收到了錯誤「服務器不支持安全連接」與我的代碼如下。服務器不支持在C#中的安全連接

  SmtpClient smtp = new SmtpClient(); 
      MailMessage mail = new MailMessage(); 
      mail.From = new MailAddress("*****@gmail.com"); 
      mail.To.Add(recieverId); 
      mail.Subject = "Invoice Copy and Delivery Confirmation for booksap.com Order " + orderno + ". Please Share Feedback."; 
      System.Net.Mail.Attachment attachment; 
      attachment = new System.Net.Mail.Attachment(Server.MapPath("OrderMail\\Invoice.pdf")); 
      mail.Attachments.Add(attachment); 
      MailBody = "We are pleased to inform that the following items in your order " + orderno + " have been placed. This completes your order. Thank you for shopping! "; 
      StreamReader reader = new StreamReader(Server.MapPath("~/MailHTMLPage.htm")); 
      string readFile = reader.ReadToEnd(); 
      string myString = ""; 
      myString = readFile; 
      myString = myString.Replace("$$Name$$", ContactPersonName); 
      myString = myString.Replace("$$MailBody$$", MailBody); 
      mail.Body = myString.ToString(); 
      mail.IsBodyHtml = true; 
      mail.Priority = MailPriority.High; 
      smtp.Host = "smtp.gmail.com"; 
      smtp.EnableSsl = true; 
      smtp.UseDefaultCredentials = true; 
      smtp.Port = 587; 
      smtp.Credentials = new System.Net.NetworkCredential("*******@gmail.com", "*******"); 
      smtp.Send(mail); 
      mail.Dispose(); 
      mail = null; 

我該如何解決這個問題? 如果我設置

EnabledSsl = false 

它會返回錯誤:SMTP服務器要求安全連接或客戶端未通過身份驗證。服務器響應是:5.7.1客戶端未通過身份驗證。

+0

我認爲Gmail的SMTP是不工作的少安全連接。剛剛嘗試過sendgrid或搜索其他電子郵件服務 –

+0

好吧,先生,我會盡力回覆給你。 – Abhishek

回答

0

該錯誤消息通常引起以下操作之一:

  • 不正確的連接設置,諸如用於 的固定或非固定連接

  • 不正確的憑證指定了錯誤的端口。我會驗證用戶名和密碼
    組合,以確保憑據是正確的。

,如果它是確定的,我認爲你必須設置DeliveryMethod = SmtpDeliveryMethod.Network

只是試試這個..

using System.Net; 
using System.Net.Mail; 

var fromAddress = new MailAddress("[email protected]", "From Name"); 
var toAddress = new MailAddress("[email protected]", "To Name"); 
const string fromPassword = "fromPassword"; 
const string subject = "Subject"; 
const string body = "Body"; 

var smtp = new SmtpClient 
      { 
       Host = "smtp.gmail.com", 
       Port = 587, 
       EnableSsl = true, 
       DeliveryMethod = SmtpDeliveryMethod.Network, 
       UseDefaultCredentials = false, 
       Credentials = new NetworkCredential(fromAddress.Address, fromPassword) 
      }; 
using (var message = new MailMessage(fromAddress, toAddress) 
        { 
         Subject = subject, 
         Body = body 
        }) 
{ 
    smtp.Send(message); 
} 

也不夠安全的應用 Go to the "Less secure apps" section in My Account.

更改帳戶訪問試用選項2:

+0

感謝您的回覆@Nalaka先生,我的連接字符串和用戶名密碼是正確的,因爲我在我的另一個應用程序中使用相同的東西,它工作正常,但目前它給出了問題。 – Abhishek

+0

先生,我在哪裏設置DeliveryMethod – Abhishek

+0

smtp.DeliveryMethod = SmtpDeliveryMethod.Network;添加之前smtp.Send(郵件); – Nalaka

0

@Abhishek是的。你寫的代碼會將消息發送到服務器,只是檢查你的郵箱(***@gmail.com),但谷歌的SMTP客戶端阻止你進入他們的域名。 你必須讓你的Gmail不那麼安全,你需要允許登錄嘗試。 enter image description here

檢查您的郵箱,您應該從Gmail域獲得此類回覆,並且您需要使您的帳戶安全性降低。 enter image description here

我認爲它有關的驗證碼和漫遊防止這種記錄,誰知道人可以扔在這一些輕things.Make帳戶少suspicious.Hope這有助於

相關問題