【问题标题】:How to send data trough email如何通过电子邮件发送数据
【发布时间】:2017-04-30 02:45:39
【问题描述】:

我需要将列表框项目发送到电子邮件。代码运行良好,但是当它到达 Send 方法时却发送失败。

    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);
        }
    }

【问题讨论】:

  • 您收到的错误是什么?
  • 请分享堆栈跟踪
  • 很确定 smtp.live.com 需要一些凭据,请参阅 How to add smtp hotmail account to send mail
  • @AlexanderLeonov 无法发送电子邮件 - 错误:发送邮件失败。
  • @Nemanja,我倾向于同意 AlexK,请先阅读有关如何在 SMTP 服务器上进行身份验证的信息,因为现在我无法想象任何不需要某些公开可用的 SMTP 服务器一种身份验证,所以无论如何你都需要它。如果这没有帮助,至少很明显问题不在于凭据......但我很确定它是。

标签: c# asp.net email listbox


【解决方案1】:

您可以将列表写入文件并作为附件发送(gmail 示例):

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;
        }
    }

并在代码开头写下:

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

您可以根据需要选择 smpt 主机地址和端口。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-10
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    相关资源
    最近更新 更多