从.Net App发送电子邮件(带有附件),而无需打开桌面邮件软件或浏览器? [英] Send Email (with Attachments) from .Net App without opening Desktop Mail Software or Browser?

查看:115
本文介绍了从.Net App发送电子邮件(带有附件),而无需打开桌面邮件软件或浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个WPF(C#)应用程序,该应用程序从用户那里获取反馈并通过电子邮件发送给他们.

I am writing a WPF (C#) application which takes feedback from user and send them through e-mail.

我希望我的程序将此邮件发送到固定地址,而不使用任何桌面邮件软件(Outlook Express或Microsoft Outlook或Windows Mail或任何其他邮件)&无需打开任何浏览器窗口.

I want my program to send this mail to a fixed address without using any Desktop Mail Software (Outlook Express or Microsoft Outlook or Windows Mail or any other) & without opening any browser window.

事实上,我想将其发送给未被发现的人.这意味着用户不应该知道有关发送邮件的信息.(这是一项可选要求,可以忽略).

In fact i want to send them undetected. That means user should not know about sending mails. (This is an optional requirement which can be ignored).

有人告诉我该怎么做.预先感谢.

Anybody tell me how to do this. Thanks in advance.

推荐答案

您绝对应该尝试使用

using System.Net.Mail;

这是您可以用来执行此操作的方法:

This is a method you can use to do this:

    /// <summary>
    /// Send an Email
    /// </summary>
    /// <param name="host">Example: smtp.gmail.com</param>
    /// <param name="port">Port to send email</param>
    /// <param name="from">Example: Email@gmail.com</param>
    /// <param name="password">Password</param>
    /// <param name="toList">List of people to send to</param>
    /// <param name="subject">Subject of email</param>
    /// <param name="messsage">Meddage of emial</param>
    /// <param name="deliveryMethod">Deliever type</param>
    /// <param name="isHtml">Is email HTML</param>
    /// <param name="useSSL">Is email SSL</param>
    /// <param name="ccList">List of people to cc</param>
    /// <param name="atachmentList">List of attachment files</param>
    public void SendMessage(string host, int port, string from, string password, List<string> toList, string subject, string messsage,
        SmtpDeliveryMethod deliveryMethod, bool isHtml, bool useSSL, List<string> ccList, List<string> atachmentList)
    {
        try
        {
            SmtpClient smtpClient = new SmtpClient(host);
            smtpClient.DeliveryMethod = deliveryMethod;
            smtpClient.Port = port;
            smtpClient.EnableSsl = useSSL;
            if (!string.IsNullOrEmpty(password))
                smtpClient.Credentials = new NetworkCredential(from, password);

            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(from);
            mailMessage.Subject = subject;
            mailMessage.IsBodyHtml = isHtml;
            mailMessage.Body = messsage;

            if (toList != null)
            {
                for (int i = 0; i < toList.Count; i++)
                {
                    if (!string.IsNullOrEmpty(toList[i]))
                        mailMessage.To.Add(toList[i]);
                }
            }

            if (ccList != null)
            {
                for (int i = 0; i < ccList.Count; i++)
                {
                    if (!string.IsNullOrEmpty(ccList[i]))
                        mailMessage.CC.Add(ccList[i]);
                }
            }

            if (atachmentList != null)
            {
                for (int i = 0; i < atachmentList.Count; i++)
                {
                    if (!string.IsNullOrEmpty(atachmentList[i]))
                        mailMessage.Attachments.Add(new Attachment(atachmentList[i]));
                }
            }

            try
            {
                smtpClient.Send(mailMessage);
            }

            catch
            {
            }
        }
        catch
        {
        }
    }

我只是通过尝试捕获所有内容,并进行了最小限度的错误检查.悬停,这是您希望找到的解决方案.

I just through a try-catch around everything and did minimal error checking. Hovere, this is the solution you are looking form I hope.

这篇关于从.Net App发送电子邮件(带有附件),而无需打开桌面邮件软件或浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆