3

I need to send list box items to email. Code is running fine, but when it gets to Send method it fails to send 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);
        }
    }
Nemanja
  • 73
  • 8
  • What is the error you're receiving? – Alexander Leonov Apr 29 '17 at 18:28
  • Please share the stack trace – degant Apr 29 '17 at 18:29
  • 2
    pretty sure smtp.live.com is going to want some credentials, see [How to add smtp hotmail account to send mail](http://stackoverflow.com/questions/9851319/how-to-add-smtp-hotmail-account-to-send-mail) – Alex K. Apr 29 '17 at 18:30
  • @AlexanderLeonov Could not send email - error: Failure sending mail. – Nemanja Apr 29 '17 at 18:36
  • @degant http://prntscr.com/f2am1v – Nemanja Apr 29 '17 at 18:36
  • 2
    @Nemanja, I'm inclined to agree with AlexK, please read about how to get authenticated on SMTP server first, because nowadays I just can't imagine any publicly available SMTP server that does not require some kind of authentication, so you will need it anyway. If this doesn't help, at least it will be clear that the problem is not in credentials... but I'm pretty sure it is. – Alexander Leonov Apr 29 '17 at 18:44
  • @AlexanderLeonov Yeah i check'd that code and it was very helpful, thank you guys. – Nemanja Apr 29 '17 at 19:05

1 Answers1

1

You can write list in file and send it as 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 on begining of code:

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

You can choose smpt host adress and port if you want.

Jeidoz
  • 54
  • 9