【问题标题】:Send Email by WebService通过 WebService 发送电子邮件
【发布时间】:2011-05-07 18:12:23
【问题描述】:

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

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

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

谢谢。

【问题讨论】:

  • 您是否开始尝试自己做这件事?到目前为止你有什么?
  • 我也搜索过,但没那么成功:-(
  • 为什么需要网络服务来实现这一点?你已经用过了吗?
  • 不,我没有创建 Web 服务。需要它以便不需要静态传递 SMTP 凭据。

标签: c# asp.net visual-studio winforms smtp


【解决方案1】:
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 工作。

【讨论】:

  • 嘿,谢谢你的代码.. 但我能知道如何在 Web 服务中执行此操作并在 WinForm 应用程序中访问它吗
  • 首先您必须开发您的 Web 服务并创建 2 个 Web 方法。一种将文件上传到托管 Web 服务的计算机上的 tmp 文件夹的方法。 2ed您需要使用已上传的文件列表(使用服务器的tmp文件夹的根目录)调用“发送”功能。
  • 感谢@stefan:让我在最后尝试一下,如果有任何障碍会发表评论..!
猜你喜欢
  • 1970-01-01
  • 2012-01-07
  • 2012-04-27
  • 2016-07-04
  • 2018-09-19
  • 2021-03-23
  • 2017-05-16
  • 2011-07-14
相关资源
最近更新 更多