简体   繁体   English

对在ASP.NET MVC中发送电子邮件感到困惑

[英]Confused about sending email in ASP.NET MVC

Hello all can anyone please explain what would the UserName and Password be? 您好,任何人都可以解释一下UserNamePassword是什么吗? I believe that those are asking for my gmail's username and password right? 我相信那些人在问我的Gmail用户名和密码对吗? (I'm the one who will always receive emails) I am trying to implement the email service for my contact form. (我是将始终接收电子邮件的人)我正在尝试为我的联系表实施电子邮件服务。 Sorry I'm pretty new to asp.net mvc. 对不起,我对asp.net mvc相当陌生。

public class MailService : IMailService
{
    public string SendMail(string from, string to, string subject, string body)
    {
        var errorMessage = "";
        try
        {
            var msg = new MailMessage(from, to, subject, body);
            using (var smtp = new SmtpClient())
            {
                var credential = new NetworkCredential
                {
                    UserName = "myemail@yahoo.com",
                    Password = "password"
                };
                smtp.Credentials = credential;
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.Send(msg);
            }
        }
        catch (Exception ex)
        {
            errorMessage = ex.Message;
        }
        return errorMessage;
    }
}

Yes. 是。 The Username and Password are the ones required by your host. 用户名和密码是主机所需的用户名和密码。 In this case the credentials you need to login at smtp.gmail.com 在这种情况下,您需要登录smtp.gmail.com的凭据

Yes, you need to authenticate using the login information just as if you were setting up an email client to send emails. 是的,您需要使用登录信息进行身份验证,就像设置电子邮件客户端来发送电子邮件一样。

SmtpClient.Credentials Property SmtpClient.Credentials属性

Gets or sets the credentials used to authenticate the sender . 获取或设置用于验证发件人的凭据。
.... ....
Some SMTP servers require that the client be authenticated before the server will send e-mail on its behalf. 某些SMTP服务器要求对客户端进行身份验证,然后服务器才能代表该客户端发送电子邮件。

Yes, it's the gmail username and password. 是的,这是gmail用户名和密码。
Remember to create a application specific password and use that instead of the actual gmail password 记住要创建一个应用程序专用密码,并使用该密码代替实际的Gmail密码


Found the following code in one of my projects: 在我的一个项目中找到以下代码:

static Task sendMail(IdentityMessage message)
{
    string text = string.Format("Please confirm your account by pasting this link on your browser\'s address bar:\n\n{0}", message.Body);
    string html = "Please confirm your account by clicking <a href=\"" + message.Body + "\">this link</a><br/>";

    MailMessage msg = new MailMessage();
    msg.From = new MailAddress("id@gmail.com");
    msg.To.Add(new MailAddress(message.Destination));
    msg.Subject = message.Subject;
    msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
    msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));

    SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", Convert.ToInt32(587));
    System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("id@gmail.com", "gmailpassword");
    smtpClient.Credentials = credentials;
    smtpClient.EnableSsl = true;
    return smtpClient.SendMailAsync(msg);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM