gpt4 book ai didi

c# - 通过 WebService 发送电子邮件

转载 作者:太空宇宙 更新时间:2023-11-03 14:25:39 25 4
gpt4 key购买 nike

我开发了 Windows 应用程序。现在我需要通过 Web 服务发送电子邮件(包括附件功能)。我该怎么做?

我还需要在“n”天之前通知电子邮件。 ('n' 天是由用户控制的功能)

如果有任何意见,请告诉我。

谢谢。

最佳答案

public bool Send(string toAddress, string subject, string body, bool isHtml, List<string> files)
{
try
{
MailMessage mailMsg = new MailMessage();

mailMsg.To = toAddress;
mailMsg.Headers.Add("From", string.Format("{0} <{1}>", senderName, senderAddress));
mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = server;
mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = port;
mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

if (enableAuth)
{
mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = userName;
mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = password;
}

if (enableSsl)
{
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
}

if (isHtml)
{
mailMsg.BodyFormat = MailFormat.Html;
}

mailMsg.BodyEncoding = Encoding.UTF8;
mailMsg.Subject = subject;
mailMsg.Body = body;

for (int i = 0; i < files.Count; i++)
{
mailMsg.Attachments.Add(new MailAttachment(files[i]));
}
SmtpMail.SmtpServer = server;
SmtpMail.Send(mailMsg);

return true;
}
catch (Exception ex)
{
this.errorMsg = ex.Message;
return false;
}
}

请注意,您必须使用 System.Web.Mail 才能使此 cod 正常工作。

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

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