简体   繁体   English

我可以在C#项目中加入SMTP电子邮件“服务”吗?

[英]SMTP email “Service” I can include in my C# project?

I've written several programs that send email from C#. 我编写了一些程序,这些程序从C#发送电子邮件。 This works great in winXP, but I find it breaks in Win7. 这在winXP中效果很好,但是我发现它在Win7中无法使用。 My understanding is that even though the SMTP server I'm referencing is on another computer, the sending computer needs to have the SMTP service installed (and win7 does not). 我的理解是,即使我引用的SMTP服务器在另一台计算机上,发送计算机也需要安装SMTP服务(而win7则没有)。

I know its possible to install a third party SMTP server, but then I'd need to do that on every computer running my programs. 我知道可以安装第三方SMTP服务器,但是随后我需要在运行程序的每台计算机上进行安装。 Instead, I'd like to include a temporary SMTP server in my project that I can use entirely from code to do the same job. 相反,我想在项目中包括一个临时SMTP服务器,我可以完全使用其代码执行相同的工作。 Does anyone know of a library (or sample code) on how I can include a temporary SMTP server in my project? 有人知道我如何在项目中包括一个临时SMTP服务器的库(或示例代码)吗?

Here is my code: 这是我的代码:

public static void sendEmail(String[] recipients, String sender, String subject, String body, String[] attachments)
    {
            MailMessage message;

            try
            {
                message = new MailMessage(sender, recipients[0]);
            }
            catch (Exception)
            {
                return;
            }

            foreach (String s in recipients)
            {
                if (!message.To.Contains(new MailAddress(s)))
                    message.To.Add(s);
            }

            message.From = new MailAddress(sender);
            message.Subject = subject;
            message.Body = body;
            message.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient("PRIVATE.PRIVATE.PRIVATE", 25);
            smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
            //smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

            smtp.UseDefaultCredentials = true;

            if (attachments.Length > 0)
            {
                foreach (String a in attachments)
                {
                    message.Attachments.Add(new Attachment(a));
                }
            }
            try
            {
                smtp.SendAsync(message, null);

To send emails from c#, you do not need a local SMTP service. 发送C#中的电子邮件,你不需要本地SMTP服务。 You just need the System.Net.Mail library. 您只需要System.Net.Mail库。 Using a remote SMTP server (possibly one with valid PTR settings and not one in your network to avoid being regarded as a spammer) should definitely suffice. 使用远程SMTP服务器(可能是一台具有有效PTR设置的服务器,而不是网络中的一台服务器,以避免被视为垃圾邮件发送者)绝对足够。

It may be a credentials issue. 这可能是凭据问题。 Change SendAsync to Send to see if you are getting any exceptions. 将SendAsync更改为Send,以查看是否遇到任何异常。 Or add a handler for the Async invocation 或为异步调用添加处理程序

smtp.SendCompleted += delegate(object s, System.ComponentModel.AsyncCompletedEventArgs    e)
{
    if (e.Error != null)
     {
        System.Diagnostics.Trace.TraceError(e.Error.ToString());
     }
};

Following changes to your code works for me in Win7 在Win7中对代码进行以下更改对我有用

SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
//smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(GoogleUserEmail, GooglePassword);
smtp.EnableSsl = true;

// smtp.UseDefaultCredentials = true;

if (attachments != null && attachments.Length > 0)
        {
            foreach (String a in attachments)
            {
                message.Attachments.Add(new Attachment(a));
            }
        }
        try
        {
            smtp.Send(message);
        }

I have never found an embeddable SMTP server, but both of these are close and you could probably modify them to fit your needs. 我从未找到可嵌入的SMTP服务器,但是两者都很接近,您可能可以对其进行修改以满足您的需求。

http://www.codeproject.com/KB/IP/smtppop3mailserver.aspx http://www.codeproject.com/KB/IP/smtppop3mailserver.aspx

http://www.ericdaugherty.com/dev/cses/developers.html http://www.ericdaugherty.com/dev/cses/developers.html

I'm going to keep looking because this is also something I'd find useful. 我将继续寻找,因为这也是我发现有用的东西。 I'll post more if I find any. 如果发现任何问题,我都会发布更多。

Have you tried specifying SMTP settings in an App.config file? 您是否尝试过在App.config文件中指定SMTP设置? Something like: 就像是:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.net>
        <mailSettings>
            <smtp deliveryMethod="Network">
                <specifiedPickupDirectory pickupDirectoryLocation="C:\tmp"/>
                <network host="smtp.example.com"/>
            </smtp>
        </mailSettings>
    </system.net>
</configuration>

If you change deliveryMethod="SpecifiedPickupDirectory" then it'll just write a file representing the email that would be sent to the directory you specify. 如果您更改了deliveryMethod="SpecifiedPickupDirectory"则它将只编写一个表示将发送到您指定目录的电子邮件的文件。

Hope this helps. 希望这可以帮助。

If you just use an unconfigured service to send your emails you will definitely end up in the SPAM folder due to reverse DNS and SPF checks failing. 如果您仅使用未配置的服务来发送电子邮件,则肯定会由于反DNS和SPF检查失败而最终进入SPAM文件夹。 So you'll want to configure your server properly. 因此,您需要正确配置服务器。 Alternatively you can use a 3rd party service like Elastic Email . 或者,您可以使用第三方服务,例如Elastic Email Here is Elastic Email's sample code which uses HTTP to send the mail: 这是Elastic Email的示例代码,该示例代码使用HTTP发送邮件:

    public static string SendEmail(string to, string subject, string bodyText, string bodyHtml, string from, string fromName)
    {

        WebClient client = new WebClient();
        NameValueCollection values = new NameValueCollection();
        values.Add("username", USERNAME);
        values.Add("api_key", API_KEY);
        values.Add("from", from);
        values.Add("from_name", fromName);
        values.Add("subject", subject);
        if (bodyHtml != null)
            values.Add("body_html", bodyHtml);
        if (bodyText != null)
            values.Add("body_text", bodyText);
        values.Add("to", to);

        byte[] response = client.UploadValues("https://api.elasticemail.com/mailer/send", values);
        return Encoding.UTF8.GetString(response);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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