2011-02-27 148 views
3

我需要發送包含異常詳細信息(黃色屏幕死亡)的郵件作爲附件。用C#發送郵件附件郵件

我能得到YSOD如下:

string YSODmarkup = lastErrorWrapper.GetHtmlErrorMessage(); 
if (!string.IsNullOrEmpty(YSODmarkup)) 
{ 
    Attachment YSOD = Attachment.CreateAttachmentFromString(YSODmarkup, "YSOD.htm"); 
    mm.Attachments.Add(YSOD); 
} 

mmMailMessage類型,但郵件沒有發送。

這裏

System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage("from", "to", "Exception-Details", htmlEmail.ToString()); 

用於將郵件主體結合。

之後只添加附件。 刪除附件時,會發送郵件。

任何人都可以幫我嗎?


按照從阿爾文先生和Paul先生的評論正在更新以下

 string YSODmarkup = Ex_Details.GetHtmlErrorMessage(); 
     string p = System.IO.Directory.GetCurrentDirectory(); 
     p = p + "\\trial.txt"; 
     StreamWriter sw = new StreamWriter(p); 
     sw.WriteLine(YSODmarkup); 
     sw.Close(); 
     Attachment a = new Attachment(p);  

     if (!string.IsNullOrEmpty(YSODmarkup)) 
     { 
      Attachment YSOD = Attachment.CreateAttachmentFromString(YSODmarkup, "YSOD.html"); 
      System.Net.Mail.Attachment(server.mappath("C:\\Documents and Settings\\user\\Desktop\\xml.docx")); 

      MyMailMessage.Attachments.Add(a); 

     } 

這裏我重視的內容到一個文本文件,並試圖相同。所以郵件沒有發送。發送包含HTML標籤的郵件是否有問題?因爲我能夠附加一個正常的文本文件。

+0

你需要發佈完整的郵件發送代碼,你沒有發送部分在這裏。你怎麼知道它沒有發送?它會崩潰嗎? – 2011-02-27 11:06:12

+0

另外,您是否確定它不會因爲您要發送的特定附件而被阻止?你有沒有嘗試過將一個普通的字符串作爲一個.txt文件? – 2011-02-27 14:54:05

+0

我已編輯帖子請更新。非常感謝 – Anjana 2011-02-27 20:50:06

回答

4
namespace SendAttachmentMail 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var myAddress = new MailAddress("[email protected]","James Peckham"); 
      MailMessage message = new MailMessage(myAddress, myAddress); 
      message.Body = "Hello"; 
      message.Attachments.Add(new Attachment(@"Test.txt")); 
      var client = new YahooMailClient(); 
      client.Send(message); 
     } 
    } 
    public class YahooMailClient : SmtpClient 
    { 
     public YahooMailClient() 
      : base("smtp.mail.yahoo.com", 25) 
     { 
      Credentials = new YahooCredentials(); 
     } 
    } 
    public class YahooCredentials : ICredentialsByHost 
    { 
     public NetworkCredential GetCredential(string host, int port, string authenticationType) 
     { 
      return new NetworkCredential("[email protected]", "mypwd"); 
     } 
    } 
} 
+1

哦yea或:message.Attachments.Add(Attachment.CreateAttachmentFromString(「MyContent」,「test.txt」)); – JDPeckham 2011-02-27 21:17:01

+0

非常感謝您的幫助。這工作我的應用程序 – Anjana 2011-03-01 13:34:11