2010-11-10 252 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