我想通過gmail發送自動生成的密碼給用戶,但它不工作。 我試過在很多論壇上搜索,這是我使用的代碼。在每個論壇上給予的差不多,仍然不起作用。郵件發件人不工作
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Drawing;
using System.Net;
using System.Net.Mail;
/// <summary>
/// Summary description for GmailSender
/// </summary>
public class GmailSender
{
public GmailSender()
{
//
// TODO: Add constructor logic here
//
}
public static bool SendMail(string gMailAccount, string password, string to, string subject, string message)
{
try
{
NetworkCredential loginInfo = new NetworkCredential("[email protected]", "mypassword");
MailMessage msg = new MailMessage();
msg.From = new MailAddress("[email protected]");
msg.To.Add(new MailAddress(to));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);
return true;
}
catch (Exception)
{
return false;
}
}
}
任何人都可以幫我解決這個問題嗎?
你得到的捕捉異常? –
「不工作」還不足以繼續。請更詳細地描述行爲。 – zimdanen
嘗試添加client.Port = 587; – mrrobot