2010-11-10 254 views
2

我已经开发了Windows应用程序。现在我需要通过Web Service发送一封电子邮件(附件功能)。我怎样才能做到这一点?发送电子邮件通过WebService

另外我需要在'n'前通知电子邮件。 ('n'天是用户控制的功能)

让我知道是否有任何意见。

谢谢。

+0

你开始试图自己做这个吗?到目前为止,你有什么? – leppie 2010-11-10 08:59:26

+0

我搜索了相同的,但不是很成功:-( – 2010-11-10 09:07:23

+0

为什么你需要一个web服务来实现这一目标?你已经使用一个? – 2010-11-10 09:11:45

回答

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为此鳕鱼工作。

+0

嘿感谢代码..但我可以知道如何做到这一点在Web服务和访问相同的WinForm应用程序 – 2010-11-10 13:28:27

+0

那么首先你有开发您的Web服务并创建2个Web方法。将文件上传到承载Web服务的计算机上的tmp文件夹的一种方法。 2ed你需要调用“发送”函数与已上传的文件列表(使用服务器的tmp文件夹的根目录)。 – 2010-11-10 13:40:26

+0

谢谢@stefan:让我在我的最后尝试这个,如果有任何障碍,我会发表评论..! – 2010-11-10 14:11:39