簡體   English   中英

無法在 C# 中發送 email

[英]unable to send email in C#

我正在使用 ASP.NET 和 C# 的網站上工作。 It takes user input including email through asp.net and then sends email to that email using C#. 實際上它會向人們發送預定的報告。 但是沒有發送 email。

我也嘗試過設置usedefaultnetworkcredential=false但它沒有用

internal static bool SendEmailNetMail(string toEmail, string subject, string body, MemoryStream ms, string fileName = "attachment.xlx", bool includeLogo = false)
    {


        toEmail = toEmail.Replace(";", ",");
        toEmail = toEmail + ",saeed.bhatti@ver-sys.com";
        var fromAddress = new MailAddress("vinspectreports@ver-sys.net", "Scheduled Reports");
        // var toAddress = new MailAddress(toEmail, "Valued Customer");
        MailAddressCollection addresses = new MailAddressCollection();
        addresses.Add(toEmail);


        var smtpClient = new SmtpClient() //;
        /* not Moving the Email credentials to web.config file*/
        {
            Host = "mail.ver-sys.net",
            Port = 1025,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            Credentials = new NetworkCredential("vinspectreports@ver-sys.net", "password"),
            Timeout = 900000

        };
        smtpClient.UseDefaultCredentials = false;

        using (var message = new MailMessage()
        {
            From = fromAddress,

            Subject = subject,
            Body = body,
            IsBodyHtml = true
        })
        {

            message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            message.To.Add(toEmail);
            try
            {
                //Attachment attachment = new Attachment("test.xls");
                if (ms != null)
                {


                    System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("application/vnd.ms-excel");
                    System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
                    attach.ContentDisposition.FileName = fileName;

                    message.Attachments.Add(attach);

                }
               // MessageBox.Show("mail Send");
                smtpClient.Send(message);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return false;
            }
            return true;
        }
  1. 打開您的 gmail 帳戶,然后單擊您的用戶名,您將看到 Google 帳戶。
    然后 go 到“安全”並打開“不太安全的應用程序訪問”。

  2. 試試這個代碼

    MailMessage mail = new MailMessage(); mail.From = new MailAddress("xyz@gmail.com", "admin"); mail.To.Add(new MailAddress("abc@gmail.com")); mail.CC.Add(new MailAddress("abcd@gmail.com")); mail.Subject = "Test"; mail.Body = "adfdsfasdfdsfdsafdfssf"; mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess; SmtpClient client = new SmtpClient(); client.Host = "smtp.gmail.com"; client.EnableSsl = true; NetworkCredential nc = new NetworkCredential(); nc.UserName = "xyz@gmail.com"; nc.Password = "1234"; client.UseDefaultCredentials = true; client.Credentials = nc; client.Port = 587; client.Send(mail); client.DeliveryMethod = SmtpDeliveryMethod.Network;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM