简体   繁体   中英

Email Sender App with Images on Local Machine

I have a small email application that lets a user build a message from checkboxes and then send the message as an email. I am trying to add an image to the email, ie a logo or signature. The application worked fine but as I was reseaching getting the images into the email, I found that I should be using System.Net.Mail instead of Interop. So I changed my email class to the code below. Now I'm not recieving the emails. I'm assumeing this is because the code is set-up for a server and I just want to run this on my local machine. This is just something I'm playing with to help me understand some concepts so real world use is not going to be a factor. I just want to be able to test my little program on my Outlook email account locally. My code is as follows...

using System;
using System.Net.Mail;
using System.Net.Mime;

namespace Email_Notifier  
{
    public class EmailSender:Notification
    {
        string emailRecipient = System.Configuration.ConfigurationManager.AppSettings    ["emailRecipient"];

        public void SendMail(string message)
        {
            try
            {
                string strMailContent = message;
                string fromAddress = "MyApp@gmail.com";
                string toAddress = emailRecipient;
                string contentId = "image1";
                string path = (@"C:Libraries/Pictures/Logo.gif");  

                MailMessage mailMessage = new MailMessage(fromAddress, toAddress);
                mailMessage.Subject = "Email Notification";

                LinkedResource logo = new LinkedResource( path , MediaTypeNames.Image.Gif);
                logo.ContentId = "Logo";
                // HTML formatting for logo
                AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><body><img src=cid:Logo/><br></body></html>" + strMailContent, null, MediaTypeNames.Text.Html);
                av1.LinkedResources.Add(logo);

                mailMessage.AlternateViews.Add(av1);
                mailMessage.IsBodyHtml = true;
                SmtpClient mailSender = new SmtpClient("localhost"); 
                mailSender.Send(mailMessage);
            }

            catch (Exception e)
            {
                Console.WriteLine("Problem with email execution. Exception caught: ", e);
            }
            return;
        }
    }
}

You specify mailSender = new SmtpClient("localhost"); .

Have you set up an SMTP server on your local machine? If not, you will need to do so to use SmtpClient . Otherwise, specify a hostname other than localhost , perhaps using your Gmail account as specified here , bearing in mind you will need to configure it for authentication and SSL.

See also the documentation for SmtpClient .

There are also one or two other issues I can see with your code - but let's deal with these after getting simple mail sending working.

if your problem is that you don't have a smtp server you are talking to on your machine, you will see an exception rather than just silently not having your email delivered (which i believe is what you are seeing given that you did not list an exception, but not completely sure). it could be that you are successfully delivering to your local smtp server, but it is failing to send the mail on. if you have the iis smtp server installed and that is what you are trying to use to send the mail, you can see failed email messages in the subdirectories of C:\\Inetpub\\mailroot. there is a badmail subdirectory that should have failed deliveries.

if you are on windows 7, it doesn't support the iis smtp server. an alternative for having an smtp server on your machine that doesn't actually deliver the email but does show you everything that has come through for any email address is http://smtp4dev.codeplex.com/ . even if i do have the iis smtp server available in my operating system, i prefer this for development needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM