gpt4 book ai didi

c# - 通过 SMTP 服务器以编程方式发送电子邮件

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

尝试通过 SMTP 服务器发送电子邮件,但我在 smtp.Send(mail); 代码片段中遇到无法描述的错误。

服务器端,中继 IP 地址看起来是正确的。为我所缺少的东西挠头。

MailMessage mail = new MailMessage();
mail.To.Add(txtEmail.Text);

mail.From = new MailAddress("no-reply@company.us");
mail.Subject = "Thank you for your submission...";
mail.Body = "This is where the body text goes";
mail.IsBodyHtml = false;

SmtpClient smtp = new SmtpClient();
smtp.Host = "mailex.company.us";
smtp.Credentials = new System.Net.NetworkCredential
("AdminName", "************");

smtp.EnableSsl = false;


if (fileuploadResume.HasFile)
{
mail.Attachments.Add(new Attachment(fileuploadResume.PostedFile.InputStream,
fileuploadResume.FileName));
}

smtp.Send(mail);

最佳答案

尝试在发送之前添加 smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

作为引用,这是我的标准邮件功能:

public void sendMail(MailMessage msg)
{
string username = "username"; //email address or domain user for exchange authentication
string password = "password"; //password
SmtpClient mClient = new SmtpClient();
mClient.Host = "mailex.company.us";
mClient.Credentials = new NetworkCredential(username, password);
mClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mClient.Timeout = 100000;
mClient.Send(msg);
}

通常这样称呼:

MailMessage msg = new MailMessage();
msg.From = new MailAddress("fromAddr");
msg.To.Add(anAddr);

if (File.Exists(fullExportPath))
{
Attachment mailAttachment = new Attachment(fullExportPath); //attach
msg.Attachments.Add(mailAttachment);
msg.Subject = "Subj";
msg.IsBodyHtml = true;
msg.BodyEncoding = Encoding.ASCII;
msg.Body = "Body";
sendMail(msg);
}
else
{
//handle missing attachments
}

关于c# - 通过 SMTP 服务器以编程方式发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16198030/

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