如何在 asp.net mvc 应用程序中使用 Gmail SMTP 发送电子邮件? [英] How can I send email using Gmail SMTP in asp.net mvc application?

查看:19
本文介绍了如何在 asp.net mvc 应用程序中使用 Gmail SMTP 发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当用户在我的网站上注册时,我想向他/她发送一封邮件.

I want to send one mail to user whenever he/she register at my website.

我为此创建了我的 gmail 帐户,我尝试了许多来自网络的示例,但我还无法发送电子邮件.

I have created my gmail account for that, I've tried many samples from net but i'm not able to sent email yet.

请在这方面帮助我.

谢谢,维姬

推荐答案

我在一个网站上发现了一篇很好的文章https://askgif.com 关于在 C# 中使用 Gmail SMTP,所以我与您分享:https://askgif.com/blog/122/seding-email-using-gmail-smtp-in-asp-net-mvc-application/

I found a very good article on a website https://askgif.com about using Gmail SMTP with C#, so am sharing with you : https://askgif.com/blog/122/seding-email-using-gmail-smtp-in-asp-net-mvc-application/

创建 Gmail 类包含所有需要的数据类型和成员函数,如下所示

Create Gmail Class comprises of all needed data type and member function as below

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

然后只需使用以下代码将电子邮件发送到所需的电子邮件帐户.

Then just used the following code wherever you want to send the email to the required email account.

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();

希望这会对您有所帮助.如果这对您有帮助,请标记为答案.

Hope this will help you. Mark as answer if this helps you.

这篇关于如何在 asp.net mvc 应用程序中使用 Gmail SMTP 发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆