Asp.net-Mvc-4
如何在 asp.net mvc 應用程序中使用 Gmail SMTP 發送電子郵件?
每當他/她在我的網站上註冊時,我想向他/她發送一封郵件。
我為此創建了我的 gmail 帳戶,我嘗試了許多來自網路的範例,但我還無法發送電子郵件。
請在這方面幫助我。
謝謝,維姬
我在網站https://askgif.com上找到了一篇關於使用 C# 使用 Gmail SMTP 的非常好的文章,所以我與您分享:https ://askgif.com/blog/122/seding-email-using-gmail-smtp -在-asp-net-mvc-應用程序/
創建 Gmail 類包括所有需要的數據類型和成員函式,如下所示
public class GMailer { public static string GmailUsername { get; set; } public static string GmailPassword { get; set; } public static string GmailHost { get; set; } public static int GmailPort { get; set; } public static bool GmailSSL { get; set; } public string ToEmail { get; set; } public string Subject { get; set; } public string Body { get; set; } public bool IsHtml { get; set; } static GMailer() { GmailHost = "smtp.gmail.com"; GmailPort = 25; // Gmail can use ports 25, 465 & 587; but must be 25 for medium trust environment. GmailSSL = true; } public void Send() { SmtpClient smtp = new SmtpClient(); smtp.Host = GmailHost; smtp.Port = GmailPort; smtp.EnableSsl = GmailSSL; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.UseDefaultCredentials = false; smtp.Credentials = new NetworkCredential(GmailUsername, GmailPassword); using (var message = new MailMessage(GmailUsername, ToEmail)) { message.Subject = Subject; message.Body = Body; message.IsBodyHtml = IsHtml; smtp.Send(message); } } }然後,只要您想將電子郵件發送到所需的電子郵件帳戶,只需使用以下程式碼即可。
GMailer.GmailUsername = "youremailid@gmail.com"; GMailer.GmailPassword = "YourPassword"; GMailer mailer = new GMailer(); mailer.ToEmail = "sumitchourasia91@gmail.com"; mailer.Subject = "Verify your email id"; mailer.Body = "Thanks for Registering your account.<br> please verify your email id by clicking the link <br> <a href='youraccount.com/verifycode=12323232'>verify</a>"; mailer.IsHtml = true; mailer.Send();希望這會幫助你。如果這對您有幫助,請標記為答案。