簡體   English   中英

無法使用ASP.NET發送電子郵件

[英]Cannot send email with ASP.NET

我的項目需要發送一封電子郵件,但我無法發送。 我不知道為什么 上個月我可以發送,但今天不能。

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

**它沒有任何錯誤,但我也沒有發送。

我不會在這里使用端口號。

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

這樣發送。 我嘗試了,它起作用了。 我是說它看起來像

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

如果在編寫代碼時檢查SmtpClient的構造函數,您將看到它具有一個重載。

嘗試這個:

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