无法使用ASP.NET发送电子邮件

时间:2015-05-07 19:49:39

标签: c# asp.net email smtp smtpclient

我的项目需要发送电子邮件,但我无法发送。我不知道为什么。上个月我可以发送,但今天我不能。

string url = Request.Url.AbsoluteUri;
string hyperlink = "<a href='" + url + "'>" + url + "</a>";
NetworkCredential loginInfo = new NetworkCredential("***examplemail***", "myPassword");
MailMessage msg = new MailMessage();
msg.From = new MailAddress("***examplemail***");
msg.To.Add(new MailAddress("***ToEmail***"));
msg.Bcc.Add(new MailAddress("***examplemail***"));
msg.Subject = "TEST";
msg.Body = "Hi, TEST Send E-mail";
msg.IsBodyHtml = false;
SmtpClient client = new SmtpClient("smtp.gmail.com", 995); // tried 25 587 and 995
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);

**它没有任何错误,但我也没有发送。

2 个答案:

答案 0 :(得分:0)

我不会在这里使用端口号。

SmtpClient client = new SmtpClient("smtp.gmail.com", 995); // tried 25 587 and 995

这种方式发送。我试过,它有效。我说它应该看起来像

SmtpClient client = new SmtpClient("smtp.gmail.com");

如果在编写代码时检查SmtpClient的构造函数,您将看到它有一个重载。

答案 1 :(得分:-1)

试试这个:

using System.Net;
using System.Net.Mail;

namespace consSendMail
{
    class Program
    {
        static void Main(string[] args)
        {
            SmtpClient smtpClient = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                Credentials = new NetworkCredential("yourmail", "yourpassword")
            };

            var mailMessage = new MailMessage();
            mailMessage.Subject = "Subject";
            mailMessage.From = new MailAddress("yourmail", "yourname");
            mailMessage.IsBodyHtml = true;
            mailMessage.Body = "your message";            
            mailMessage.To.Add( new MailAddress("destinationemail") );

            smtpClient.Send(mailMessage);

            mailMessage.Dispose();
            smtpClient.Dispose();
        }
    }
}