无法使用C#发送电子邮件

时间:2019-10-21 05:36:26

标签: c# .net email

我正在使用ASP.NET和C#的网站上工作。它通过asp.net接收用户输入(包括电子邮件),然后使用C#将电子邮件发送到该电子邮件。实际上,它会将预定的报告发送给人们。但是电子邮件未发送。

我也尝试设置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 个答案:

答案 0 :(得分:0)