我一直在尝试使用以下方法设置电子邮件身份验证:
OAuthWebSecurity.RegisterGoogleClient();
WebMail.SmtpServer = "stmp.gmail.com";
WebMail.SmtpPort = 465;
WebMail.EnableSsl = true;
WebMail.UserName = "username@gmail.com";
WebMail.Password = "mypassword";
WebMail.From = "username@gmail.com";
在发送丢失密码说明的页面上,以下代码调用上述设置。但是,它给我一条消息,表示电子邮件已经发送但没有任何内容发送到我的收件箱,我做错了吗?
bool passwordSent = false;
var resetToken = "";
var email = Request.Form["email"] ?? Request.QueryString["email"];
// Setup validation
Validation.RequireField("email", "The email address field is required.");
if (IsPost) {
AntiForgery.Validate();
// validate email
bool isValid = true;
if (Validation.IsValid()) {
if (WebSecurity.GetUserId(email) > -1 && WebSecurity.IsConfirmed(email)) {
resetToken = WebSecurity.GeneratePasswordResetToken(email); // Optionally specify an
expiration date for the token
} else {
passwordSent = true; // We don't want to disclose that the user does not exist.
isValid = false;
}
}
if (isValid) {
var hostUrl = Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
var resetUrl = hostUrl + VirtualPathUtility.ToAbsolute("~/Account/PasswordReset?resetToken=" + HttpUtility.UrlEncode(resetToken));
WebMail.Send(
to: email,
subject: "Please reset your password",
body: "Use this password reset token to reset your password. The token is: " + resetToken + @". Visit <a href=""" + HttpUtility.HtmlAttributeEncode(resetUrl) + @""">" + resetUrl + "</a> to reset your password."
);
passwordSent = true;
}
}
}
<hgroup class="title">
<h1>@Page.Title.</h1>
<h2>Use the form below to reset your password.</h2>
</hgroup>
@if (!WebMail.SmtpServer.IsEmpty()) {
<p>
We will send password reset instructions to the email address associated with your account.
</p>
if (passwordSent) {
<p class="message-success">
Instructions to reset your password have been sent to the specified email address.
</p>
}
<form method="post">
@AntiForgery.GetHtml()
@Html.ValidationSummary(excludeFieldErrors: true)
<fieldset>
<legend>Password Reset Instructions Form</legend>
<ol>
<li class="email">
<label for="email" @if (!ModelState.IsValidField("email")) {<text>class="error-label"</text>}>Email address</label>
<input type="text" id="email" name="email" value="@email" disabled="@passwordSent" @Validation.For("email") />
@Html.ValidationMessage("email")
</li>
</ol>
<p class="form-actions">
<input type="submit" value="Send instructions" disabled="@passwordSent" />
</p>
</fieldset>
</form>
} else {
<p class="message-info">
Password recovery is disabled for this website because the SMTP server is
not configured correctly. Please contact the owner of this site to reset
your password.
</p>
}
我已阅读此问题:ASP WebMail: Set up SMTP authentication?但我不知道如何使表单使用该页面上的设置而不是Webmatrix提供的设置。
答案 0 :(得分:0)
在线
WebMail.SmtpServer = "stmp.gmail.com";
应该是&#34; smtp.gmail.com&#34; ?