我想添加一個新的電子郵件確認方法到我的MVC網站,所以每當用戶註冊他將收到確認電子郵件。問題是,不管我用什麼樣的代碼(我發現在GitHub上的一些不同的選項),我總是得到同樣的錯誤:ASP.NET使用SMTP與SMTP錯誤:「連接嘗試失敗,因爲連接方沒有正確迴應..」
"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 66.102.1.108:587".
我使用的代碼是:
using ATMProject.Models;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Security.Policy;
using System.Web;
using System.Web.Mvc;
namespace ATMProject.Services
{
public class SendEmailConfirmation
{
public string email { get; set; }
public string fullname { get; set; }
public string username { get; set; }
public string random { get; set; }
public SendEmailConfirmation(string Email, string Fullname,
string Username, string Random)
{
email = Email;
fullname = Fullname;
username = Username;
random = Random;
}
public void sendEmail()
{
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new
System.Net.NetworkCredential("[email protected]", "123456");
smtp.EnableSsl = true;
MailMessage msg = new MailMessage();
#region Msg
msg.Subject = "Hello " + fullname;
msg.Body = "Hello " + fullname +
"Thanks for Registering in Budgetize...Your Account Details are given below:";
msg.Body += "<tr>";
msg.Body += "<td>User Name :" + username + "</td>";
msg.Body += "</tr>";
msg.Body += "<tr>";
msg.Body += "<td>Activation Number :" + random + "</td>";
msg.Body += "</tr>";
msg.Body += "<tr>";
msg.Body += "<td>Thanking</td><td>Team Budgetize</td>";
msg.Body += "</tr>";
#endregion
string toAddress = email; // Add Recepient address
msg.To.Add(toAddress);
string fromAddress = "\"Budgetize \" <[email protected]>";
msg.From = new MailAddress(fromAddress);
msg.IsBodyHtml = true;
try {smtp.Send(msg);}
catch (Exception ex) {throw;}
}
}
}
我希望你沒有把你真正的passoword放在那裏 – Steve