Sending emails with attachment in C #

I need to send mail, including exception information (yellow screen of death) as an attachment.

I could get YSOD as follows:

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

mmhas type MailMessage, but mail is not sent.

Here

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

used to bind the mail body.

After that, only the application is added. When you delete an attachment, mail is sent.

Can anyone help me out?


In accordance with the comments of Mr. Albin and Mr. Paul, I am updating the following

        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);

        }  

Here I attached the contents to a text file and tried the same thing. Therefore, the mail was not sent. Is there a problem with sending emails containing HTML tags in it? Because I was able to include a plain text file.

+3
source share
1 answer
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");
        }
    }
}
+4
source

Source: https://habr.com/ru/post/1795034/


All Articles