gpt4 book ai didi

c# - 如何通过电子邮件发送数据

转载 作者:行者123 更新时间:2023-11-30 17:33:34 24 4
gpt4 key购买 nike

我需要将列表框项目发送到电子邮件。代码运行良好,但是当它到达 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);
}
}

最佳答案

您可以在文件中写入列表并将其作为附件发送(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 主机地址和端口。

关于c# - 如何通过电子邮件发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43699122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com