0

Hello all can anyone please explain what would the UserName and Password be? I believe that those are asking for my gmail's username and password right? (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.

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 = "[email protected]",
                    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;
    }
}
1
  • getting down vote of something I don't know? Commented Apr 29, 2015 at 18:52

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

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.

Comments

2

Yes, it's the gmail username and password.
Remember to create a application specific password and use that instead of the actual gmail password


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("[email protected]");
    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("[email protected]", "gmailpassword");
    smtpClient.Credentials = credentials;
    smtpClient.EnableSsl = true;
    return smtpClient.SendMailAsync(msg);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.