简体   繁体   English

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

[英]Cannot send email with ASP.NET

My Project need to send an e-mail but I cannot send it. 我的项目需要发送一封电子邮件,但我无法发送。 I don't know why. 我不知道为什么 Last month I can send but today I cannot. 上个月我可以发送,但今天不能。

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);

** It didn't have any error but I didn't send too. **它没有任何错误,但我也没有发送。

I wouldn't use the port number in here. 我不会在这里使用端口号。

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

This way it sends. 这样发送。 I tried and it worked. 我尝试了,它起作用了。 I am saying that it should look like 我是说它看起来像

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

If you check SmtpClient's constructor while writing the code you will see that it has one overload. 如果在编写代码时检查SmtpClient的构造函数,您将看到它具有一个重载。

Try this: 尝试这个:

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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM