c# - C#发送带附件的邮件

标签 c# .net email mailmessage

我需要发送一封包含异常详细信息(死亡蓝屏)的邮件作为附件。

我可以通过以下方式获得 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());

用于绑定(bind)邮件正文。

此后仅添加附件。 删除附件时,会发送邮件。

谁能帮帮我?


根据 Albin 先生和 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 标签的邮件是否有任何问题。因为我能够附加一个普通的文本文件。

最佳答案

namespace SendAttachmentMail
{
    class Program
    {
        static void Main(string[] args)
        {
            var myAddress = new MailAddress("jhered@yahoo.com","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("jhered@yahoo.com", "mypwd");
        }
    }
}

关于c# - C#发送带附件的邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5132801/

相关文章:

c# - 循环 10 次添加输入,在 C# 中使用 for 和 do while 循环?

c# - 在匿名类型数组中包含空值/默认值的更简洁方法?

c# - 如何判断一个值是被复制还是被引用?

VBA 在电子邮件正文中插入链接而不使用 HTML 正文?

php邮件多个收件人

c# - 返回 json 数据表未收到数据

c# - 根据多个条件外部连接两个数据表

c# - XmlSerializer 在反序列化时抛出异常

android - 从 HTML 电子邮件中删除默认的 Android 填充

c# - 无法将 ref enum 转换为 ref dynamic?