[英]C# Windows Form Application - Send email using gmail smtp
[英]Cant use gmail smtp to send email via c# form app
我在c#.net表單應用程序中編寫了此代碼以發送電子郵件。 通過替換smtp服務器名稱,代碼可與yahoo,hotmail,gmx一起使用,但不適用於gmail,
try
{
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
var mail = new MailMessage();
mail.From = new MailAddress(youremail.Text);
mail.To.Add(txtreceiver.Text);
mail.Subject = txtsubject.Text;
mail.IsBodyHtml = true;
mail.Body = txtbody.Text;
SmtpServer.Port = 465;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential(youremail.Text, yourpass.Text);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("Sent sucessfully..! \n If Email is not found in inbox check junk ");
}
catch (Exception s)
{
MessageBox.Show("Failled To Send Mail..!");
}
首先,必須使用端口587,如注釋中建議的@ user1666620。
然后,您還需要允許“安全性較低”的設備訪問該GMail帳戶。 單擊您的帳戶頭像,然后單擊“我的帳戶”->“登錄和安全”->“已連接的應用程序和網站”。 在該頁面底部,切換“允許安全性較低的應用程序”選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.