2011-02-27 155 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