How to send data via email

I need to email list items. The code works fine, but when it receives a dispatch method, it doesn't dispatch it.

    protected void ProcessButton_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.To.Add("someone@live.com");
            mailMessage.From = new MailAddress("myAdress@live.com");
            mailMessage.Subject = "ASP.NET e-mail test";
            mailMessage.Body = OrderListBox.Items.ToString();
            SmtpClient smtpClient = new SmtpClient("smtp.live.com");
            smtpClient.Send(mailMessage);
            Response.Write("E-mail sent!");
        }
        catch (Exception ex)
        {
            Response.Write("Could not send email - error: " + ex.Message);
        }
    }

      

+3


source to share


1 answer


You can write the list in a file and send it as an attachment (gmail example):

protected bool ProcessButton_Click(object sender, EventArgs e)
    {
        SmtpClient client = new SmtpClient();
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.Credentials = new NetworkCredential("myEmail@gmail.com", "password");
        //if you have double verification on gmail, then generate and write App Password
        client.EnableSsl = true;

        MailMessage message = new MailMessage(new MailAddress("myEmail@gmail.com"),
            new MailAddress(receiverEmail));
        message.Subject = "Title";
        message.Body = $"Text";

        // Attach file
        Attachment attachment;
        attachment = new Attachment("D:\list.txt");
        message.Attachments.Add(attachment);

        try
        {
            client.Send(message);
            // ALL OK
            return true;
        }
        catch
        {
            //Have problem
            return false;
        }
    }

      

and write this at the start of your code:



using System.Net;
using System.Net.Mail;

      

You can choose the address and port of the smpt node if you like.

+1


source







All Articles