0

I am working on a website which uses ASP.NET and C#. It takes user input including email through asp.net and then sends email to that email using C#. Actually it send scheduled report to people. But the email is not being sent.

I have also tried setting usedefaultnetworkcredential=false but it did not work

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;
        }
DerStarkeBaer
  • 669
  • 8
  • 28
  • 3
    Do you get any error? – Selim Yildiz Oct 21 '19 at 05:39
  • 3
    Could you please provide us with error message? – So_oP Oct 21 '19 at 05:42
  • For your sake, please don't put what seem to be actual credentials in your question. – AsheraH Oct 21 '19 at 06:36
  • Each hosting providers have their own settings, you may need to contact your hosting provider about the settings that you need to use. You can read some tutorial here http://dotnet4europeanhosting.hostforlife.eu/post/European-ASPNET-45-Hosting-HostForLIFEeu-Send-Bulk-Email-Using-ASPNET-45.aspx – Douglas Thomas Oct 21 '19 at 07:14

1 Answers1

0

Try to change the port to 25 or 587

Look at these resources to send email step by step:

  1. How to send email in ASP.NET C#
  2. https://www.a2hosting.com/kb/a2-hosting-products/windows-hosting/using-asp-net-to-send-e-mail-messages
ali
  • 175
  • 1
  • 4
  • 21