开发者

Send Automated Email through Windows Service that has an embedded image using C#

I already have a C# windows service that we use internally to monitor a directory on our network and when it detects a change sends off an email using our internal SMTP server to the specified groups of people.

Now I need to embedd an image in that automated email. I understand that I need to create an AlternateView and a Linked Resource and use the Linked Resource's cID in the AlternateView, correct.

What I do not understand is where do I put the image? Should I add it to my service project and set Copy to Output Directory = Copy Always? If so, how would I then access when creating my LinkedResource? Also, where do I put the image on the Server hosting the Service?

Here is what I have so far but it doesn't seem to work. I don't get any errors, that I am aware of, but I do not get an email either. I am guessing it is looking for the image but that I do not have it in the correct location.

// This event is called when an object(file,folder) is created in the srcPath
    void WatcherCreated(object source , FileSystemEventArgs e)
    {
        var folderName = e.Name;
        var folderPath = e.FullPath;

        MailMessage mail = new MailMessage();

        mail.From = new MailAddress("FolderMonitor@myWork.org");
        mail.To.Add("PSchaller@myWork.org");

        mail.Subject = "New Enrollment for " + folderName;

        AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is the plain text view", null,
                                                                              "text/html");

        AlternateView htmlView =
            AlternateView.CreateAlternateViewFromString("Here is an embedded image.开发者_Python百科 <img src=cid:enrollProcessID>",
                                                        null, "text/html");

        LinkedResource imageResourceLink = new LinkedResource("C:\\FolderMonitorService\\EnrollmentProcess.jpg")
                                               {ContentId = "enrollProcessID"};

        htmlView.LinkedResources.Add(imageResourceLink);

        mail.AlternateViews.Add(plainView);
        mail.AlternateViews.Add(htmlView);

        var smtp = new SmtpClient("internalSMTP");
        smtp.Send(mail);
    }


Actually, you're misinterpreting the error here.. Your code is absolutely fine as far as I can see.. Just to double check it, I copied it in a simple app, updated the local path to an image and ran it - I received the e-mail immediately..

Also, if I give it a wrong path to a file - it will throw an exception immediately, stating that the file is not found :)

If you don't receive the image in your mailbox it might be that the spam filters are filtering it.. I'm ruling out the smtp server settings here, because you say that sending a plaintext only version works :)

UPD: What I do recommend from personal experience is changing the encoding to UTF8. This was one of the criteria why my mail messages were removed by spam filters.. Other than that- your code looks fine, and I don't see any reason for it not to work.. It does work on my side after all :)


Checkout the following articles:

  • Embed images in a mail message using C#
  • Sending Email Using Embedded Images
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜